summaryrefslogtreecommitdiffstats
path: root/src/stateless/cp/trex_stateless.cpp
blob: 72762e260ef57142e20707d1d592f09192431537 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 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 <trex_stateless.h>
#include <trex_stateless_port.h>

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

using namespace std;

/***********************************************************
 * Trex stateless object
 * 
 **********************************************************/
TrexStateless::TrexStateless() {
    m_is_configured = false;
}


/**
 * configure the singleton stateless object
 * 
 */
void TrexStateless::configure(const TrexStatelessCfg &cfg) {

    TrexStateless& instance = get_instance_internal();

    /* check status */
    if (instance.m_is_configured) {
        throw TrexException("re-configuration of stateless object is not allowed");
    }

    /* create RPC servers */

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

    /* configure ports */

    instance.m_port_count = cfg.m_port_count;

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

    /* cores */
    instance.m_dp_core_count = cfg.m_dp_core_count;
    for (int i = 0; i < instance.m_dp_core_count; i++) {
        instance.m_dp_cores.push_back(new TrexStatelessDpCore(i));
    }

    /* done */
    instance.m_is_configured = true;
}

/**
 * starts the control plane side
 * 
 */
void
TrexStateless::launch_control_plane() {
    //std::cout << "\n on control/master core \n";

    /* pin this process to the current running CPU
       any new thread will be called on the same CPU
       (control plane restriction)
     */
    cpu_set_t mask;
    CPU_ZERO(&mask);
    CPU_SET(sched_getcpu(), &mask);
    sched_setaffinity(0, sizeof(mask), &mask);

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

void
TrexStateless::launch_on_dp_core(uint8_t core_id) {
    m_dp_cores[core_id - 1]->run();
}

/**
 * destroy the singleton and release all memory
 * 
 * @author imarom (08-Oct-15)
 */
void
TrexStateless::destroy() {
    TrexStateless& instance = get_instance_internal();

    if (!instance.m_is_configured) {
        return;
    }

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

    /* stops the RPC server */
    instance.m_rpc_server->stop();
    delete instance.m_rpc_server;

    instance.m_rpc_server = NULL;

    /* done */
    instance.m_is_configured = false;
}

/**
 * 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_dp_core_count;
}

void 
TrexStateless::update_stats() {

    /* update CPU util.
      TODO
     */
    m_stats.m_stats.m_cpu_util = 0;

    /* for every port update and accumulate */
    for (uint8_t i = 0; i < m_port_count; i++) {
        m_ports[i]->update_stats();

        const TrexPortStats & port_stats = m_ports[i]->get_stats();

        m_stats.m_stats.m_tx_bps += port_stats.m_stats.m_tx_bps;
        m_stats.m_stats.m_rx_bps += port_stats.m_stats.m_rx_bps;

        m_stats.m_stats.m_tx_pps += port_stats.m_stats.m_tx_pps;
        m_stats.m_stats.m_rx_pps += port_stats.m_stats.m_rx_pps;

        m_stats.m_stats.m_total_tx_pkts += port_stats.m_stats.m_total_tx_pkts;
        m_stats.m_stats.m_total_rx_pkts += port_stats.m_stats.m_total_rx_pkts;

        m_stats.m_stats.m_total_tx_bytes += port_stats.m_stats.m_total_tx_bytes;
        m_stats.m_stats.m_total_rx_bytes += port_stats.m_stats.m_total_rx_bytes;

        m_stats.m_stats.m_tx_rx_errors += port_stats.m_stats.m_tx_rx_errors;
    }
}

void
TrexStateless::encode_stats(Json::Value &global) {

    global["cpu_util"] = m_stats.m_stats.m_cpu_util;

    global["tx_bps"]   = m_stats.m_stats.m_tx_bps;
    global["rx_bps"]   = m_stats.m_stats.m_rx_bps;

    global["tx_pps"]   = m_stats.m_stats.m_tx_pps;
    global["rx_pps"]   = m_stats.m_stats.m_rx_pps;

    global["total_tx_pkts"] = Json::Value::UInt64(m_stats.m_stats.m_total_tx_pkts);
    global["total_rx_pkts"] = Json::Value::UInt64(m_stats.m_stats.m_total_rx_pkts);

    global["total_tx_bytes"] = Json::Value::UInt64(m_stats.m_stats.m_total_tx_bytes);
    global["total_rx_bytes"] = Json::Value::UInt64(m_stats.m_stats.m_total_rx_bytes);

    global["tx_rx_errors"]    = Json::Value::UInt64(m_stats.m_stats.m_tx_rx_errors);

    for (uint8_t i = 0; i < m_port_count; i++) {
        std::stringstream ss;

        ss << "port " << i;
        Json::Value &port_section = global[ss.str()];

        m_ports[i]->encode_stats(port_section);
    }
}