trex_stateless.cpp revision ac2e93d4
1/*
2 Itay Marom
3 Cisco Systems, Inc.
4*/
5
6/*
7Copyright (c) 2015-2015 Cisco Systems, Inc.
8
9Licensed under the Apache License, Version 2.0 (the "License");
10you may not use this file except in compliance with the License.
11You may obtain a copy of the License at
12
13    http://www.apache.org/licenses/LICENSE-2.0
14
15Unless required by applicable law or agreed to in writing, software
16distributed under the License is distributed on an "AS IS" BASIS,
17WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18See the License for the specific language governing permissions and
19limitations under the License.
20*/
21
22#include <iostream>
23#include <unistd.h>
24
25#include "trex_stateless.h"
26#include "trex_stateless_port.h"
27#include "trex_stateless_messaging.h"
28
29using namespace std;
30
31/***********************************************************
32 * Trex stateless object
33 *
34 **********************************************************/
35
36/**
37 *
38 */
39TrexStateless::TrexStateless(const TrexStatelessCfg &cfg) {
40
41    /* create RPC servers */
42
43    /* set both servers to mutex each other */
44    m_rpc_server = new TrexRpcServer(cfg.m_rpc_req_resp_cfg);
45    m_rpc_server->set_verbose(cfg.m_rpc_server_verbose);
46
47    /* configure ports */
48    m_port_count = cfg.m_port_count;
49
50    for (int i = 0; i < m_port_count; i++) {
51        m_ports.push_back(new TrexStatelessPort(i, cfg.m_platform_api));
52    }
53
54    m_platform_api = cfg.m_platform_api;
55    m_publisher    = cfg.m_publisher;
56
57    /* API core version */
58    const int API_VER_MAJOR = 3;
59    const int API_VER_MINOR = 0;
60    m_api_classes[APIClass::API_CLASS_TYPE_CORE].init(APIClass::API_CLASS_TYPE_CORE,
61                                                      API_VER_MAJOR,
62                                                      API_VER_MINOR);
63}
64
65/**
66 * release all memory
67 *
68 * @author imarom (08-Oct-15)
69 */
70TrexStateless::~TrexStateless() {
71
72    shutdown();
73
74    /* release memory for ports */
75    for (auto port : m_ports) {
76        delete port;
77    }
78    m_ports.clear();
79
80    /* stops the RPC server */
81    if (m_rpc_server) {
82        delete m_rpc_server;
83        m_rpc_server = NULL;
84    }
85
86    if (m_platform_api) {
87        delete m_platform_api;
88        m_platform_api = NULL;
89    }
90}
91
92/**
93* shutdown the server
94*/
95void TrexStateless::shutdown() {
96
97    /* stop ports */
98    for (TrexStatelessPort *port : m_ports) {
99        /* safe to call stop even if not active */
100        port->stop_traffic();
101    }
102
103    /* shutdown the RPC server */
104    if (m_rpc_server) {
105        m_rpc_server->stop();
106    }
107}
108
109/**
110 * starts the control plane side
111 *
112 */
113void
114TrexStateless::launch_control_plane() {
115
116    /* start RPC server */
117    m_rpc_server->start();
118}
119
120
121/**
122 * fetch a port by ID
123 *
124 */
125TrexStatelessPort * TrexStateless::get_port_by_id(uint8_t port_id) {
126    if (port_id >= m_port_count) {
127        throw TrexException("index out of range");
128    }
129
130    return m_ports[port_id];
131
132}
133
134uint8_t
135TrexStateless::get_port_count() {
136    return m_port_count;
137}
138
139uint8_t
140TrexStateless::get_dp_core_count() {
141    return m_platform_api->get_dp_core_count();
142}
143
144void
145TrexStateless::send_msg_to_rx(TrexStatelessCpToRxMsgBase *msg) const {
146
147    CNodeRing *ring = CMsgIns::Ins()->getCpRx()->getRingCpToDp(0);
148    ring->Enqueue((CGenNode *)msg);
149}
150
151
152