trex_stateless_dp_core.cpp revision a1971ec3
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#include <trex_stateless_dp_core.h>
22#include <trex_stateless_messaging.h>
23#include <trex_streams_compiler.h>
24#include <trex_stream_node.h>
25
26#include <bp_sim.h>
27
28TrexStatelessDpCore::TrexStatelessDpCore(uint8_t thread_id, CFlowGenListPerThread *core) {
29    m_thread_id = thread_id;
30    m_core = core;
31
32    CMessagingManager * cp_dp = CMsgIns::Ins()->getCpDp();
33
34    m_ring_from_cp = cp_dp->getRingCpToDp(thread_id);
35    m_ring_to_cp   = cp_dp->getRingDpToCp(thread_id);
36
37    m_state = STATE_IDLE;
38}
39
40void
41TrexStatelessDpCore::start() {
42
43    /* creates a maintenace job using the scheduler */
44    CGenNode * node_sync = m_core->create_node() ;
45    node_sync->m_type = CGenNode::FLOW_SYNC;
46    node_sync->m_time = m_core->m_cur_time_sec + SYNC_TIME_OUT;
47    m_core->m_node_gen.add_node(node_sync);
48
49    double old_offset = 0.0;
50    m_core->m_node_gen.flush_file(100000000, 0.0, false, m_core, old_offset);
51
52}
53
54void
55TrexStatelessDpCore::add_cont_stream(double pps, const uint8_t *pkt, uint16_t pkt_len) {
56    CGenNodeStateless *node = m_core->create_node_sl();
57
58    /* add periodic */
59    node->m_type = CGenNode::STATELESS_PKT;
60    node->m_time = m_core->m_cur_time_sec + 0.0 /* STREAM ISG */;
61    node->m_flags = 0;
62
63    /* set socket id */
64    node->set_socket_id(m_core->m_node_gen.m_socket_id);
65
66    /* build a mbuf from a packet */
67    uint16_t pkt_size = pkt_len;
68    const uint8_t *stream_pkt = pkt;
69
70    node->m_next_time_offset = 1.0 / pps;
71    node->m_is_stream_active = 1;
72
73    /* allocate const mbuf */
74    rte_mbuf_t *m = CGlobalInfo::pktmbuf_alloc(node->get_socket_id(), pkt_size);
75    assert(m);
76
77    char *p = rte_pktmbuf_append(m, pkt_size);
78    assert(p);
79    /* copy the packet */
80    memcpy(p,stream_pkt,pkt_size);
81
82    /* set dir 0 or 1 client or server */
83    pkt_dir_t dir = 0;
84    node->set_mbuf_cache_dir(dir);
85
86    /* TBD repace the mac if req we should add flag  */
87    m_core->m_node_gen.m_v_if->update_mac_addr_from_global_cfg(dir, m);
88
89    /* set the packet as a readonly */
90    node->set_cache_mbuf(m);
91
92    /* keep track */
93    m_active_nodes.push_back(node);
94
95    /* schedule */
96    m_core->m_node_gen.add_node((CGenNode *)node);
97
98    m_state = TrexStatelessDpCore::STATE_TRANSMITTING;
99
100}
101
102void
103TrexStatelessDpCore::start_traffic(TrexStreamsCompiledObj *obj) {
104    for (auto single_stream : obj->get_objects()) {
105        add_cont_stream(single_stream.m_pps, single_stream.m_pkt, single_stream.m_pkt_len);
106    }
107}
108
109void
110TrexStatelessDpCore::stop_traffic(uint8_t port_id) {
111    /* we cannot remove nodes not from the top of the queue so
112       for every active node - make sure next time
113       the scheduler invokes it, it will be free */
114    for (auto node : m_active_nodes) {
115        if (node->m_port_id == port_id) {
116            node->m_is_stream_active = 0;
117        }
118    }
119
120    /* remove all the non active nodes */
121    auto pred = std::remove_if(m_active_nodes.begin(),
122                               m_active_nodes.end(),
123                               [](CGenNodeStateless *node) { return (!node->m_is_stream_active); });
124
125    m_active_nodes.erase(pred, m_active_nodes.end());
126
127    if (m_active_nodes.size() == 0) {
128        m_state = STATE_IDLE;
129    }
130
131}
132
133/**
134 * handle a message from CP to DP
135 *
136 */
137void
138TrexStatelessDpCore::handle_cp_msg(TrexStatelessCpToDpMsgBase *msg) {
139    msg->handle(this);
140    delete msg;
141}
142
143