trex_stateless_dp_core.cpp revision 9a820782
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    m_state = TrexStatelessDpCore::STATE_TRANSMITTING;
93
94    /* keep track */
95    m_active_nodes.push_back(node);
96
97    /* schedule */
98    m_core->m_node_gen.add_node((CGenNode *)node);
99}
100
101void
102TrexStatelessDpCore::start_traffic(TrexStreamsCompiledObj *obj) {
103    for (auto single_stream : obj->get_objects()) {
104        add_cont_stream(single_stream.m_pps, single_stream.m_pkt, single_stream.m_pkt_len);
105    }
106}
107
108void
109TrexStatelessDpCore::stop_traffic() {
110    /* we cannot remove nodes not from the top of the queue so
111       for every active node - make sure next time
112       the scheduler invokes it, it will be free */
113    for (auto node : m_active_nodes) {
114        node->m_is_stream_active = 0;
115    }
116    m_active_nodes.clear();
117
118    m_state = STATE_IDLE;
119}
120
121/**
122 * handle a message from CP to DP
123 *
124 */
125void
126TrexStatelessDpCore::handle_cp_msg(TrexStatelessCpToDpMsgBase *msg) {
127    msg->handle(this);
128    delete msg;
129}
130
131