summaryrefslogtreecommitdiffstats
path: root/src/stateless/cp/trex_stateless.cpp
blob: b42391f5791973b3a6812668dd3cd9554224e0bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 Itay Marom
 Cisco Systems, Inc.
*/

/*
Copyright (c) 2015-2015 Cisco Systems, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <iostream>
#include <unistd.h>

#include "trex_stateless.h"
#include "trex_stateless_port.h"
#include "trex_stateless_messaging.h"

using namespace std;

/***********************************************************
 * Trex stateless object
 * 
 **********************************************************/

/**
 * 
 */
TrexStateless::TrexStateless(const TrexStatelessCfg &cfg) {

    /* create RPC servers */

    /* set both servers to mutex each other */
    m_rpc_server = new TrexRpcServer(cfg.m_rpc_req_resp_cfg);
    m_rpc_server->set_verbose(cfg.m_rpc_server_verbose);

    /* configure ports */
    m_port_count = cfg.m_port_count;

    for (int i = 0; i < m_port_count; i++) {
        m_ports.push_back(new TrexStatelessPort(i, cfg.m_platform_api));
    }

    m_platform_api = cfg.m_platform_api;
    m_publisher    = cfg.m_publisher;

    /* API core version */
    const int API_VER_MAJOR = 3;
    const int API_VER_MINOR = 1;
    m_api_classes[APIClass::API_CLASS_TYPE_CORE].init(APIClass::API_CLASS_TYPE_CORE,
                                                      API_VER_MAJOR,
                                                      API_VER_MINOR);
}

/** 
 * release all memory 
 * 
 * @author imarom (08-Oct-15)
 */
TrexStateless::~TrexStateless() {

    shutdown();

    /* release memory for ports */
    for (auto port : m_ports) {
        delete port;
    }
    m_ports.clear();

    /* stops the RPC server */
    if (m_rpc_server) {
        delete m_rpc_server;
        m_rpc_server = NULL;
    }

    if (m_platform_api) {
        delete m_platform_api;
        m_platform_api = NULL;
    }
}

/**
* shutdown the server
*/
void TrexStateless::shutdown() {

    /* stop ports */
    for (TrexStatelessPort *port : m_ports) {
        /* safe to call stop even if not active */
        port->stop_traffic();
    }

    /* shutdown the RPC server */
    if (m_rpc_server) {
        m_rpc_server->stop();
    }
}

/**
 * starts the control plane side
 * 
 */
void
TrexStateless::launch_control_plane() {

    /* start RPC server */
    m_rpc_server->start();
}


/**
 * fetch a port by ID
 * 
 */
TrexStatelessPort * TrexStateless::get_port_by_id(uint8_t port_id) {
    if (port_id >= m_port_count) {
        throw TrexException("index out of range");
    }

    return m_ports[port_id];

}

uint8_t 
TrexStateless::get_port_count() {
    return m_port_count;
}

uint8_t 
TrexStateless::get_dp_core_count() {
    return m_platform_api->get_dp_core_count();
}

void
TrexStateless::send_msg_to_rx(TrexStatelessCpToRxMsgBase *msg) const {

    CNodeRing *ring = CMsgIns::Ins()->getCpRx()->getRingCpToDp(0);
    ring->Enqueue((CGenNode *)msg);
}