summaryrefslogtreecommitdiffstats
path: root/src/stateless/dp/trex_stateless_dp_core.cpp
blob: f64686d4a60727a9acf3266c3cb45bcc25d81802 (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
/*
 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_dp_core.h>
#include <trex_stateless_messaging.h>
#include <trex_streams_compiler.h>

#include <bp_sim.h>

typedef struct dp_node_extended_info_ {
    double next_time_offset;
} dp_node_extended_info_st;

TrexStatelessDpCore::TrexStatelessDpCore(uint8_t thread_id, CFlowGenListPerThread *core) {
    m_thread_id = thread_id;
    m_core = core;

    m_state = STATE_IDLE;

    CMessagingManager * cp_dp = CMsgIns::Ins()->getCpDp();

    m_ring_from_cp = cp_dp->getRingCpToDp(thread_id);
    m_ring_to_cp   = cp_dp->getRingDpToCp(thread_id);
}

void
TrexStatelessDpCore::start() {

    /* creates a maintenace job using the scheduler */
    CGenNode * node_sync = m_core->create_node() ;
    node_sync->m_type = CGenNode::FLOW_SYNC;
    node_sync->m_time = m_core->m_cur_time_sec + SYNC_TIME_OUT;
    m_core->m_node_gen.add_node(node_sync);

    double old_offset = 0.0;
    m_core->m_node_gen.flush_file(100000000, 0.0, false, m_core, old_offset);

}

void
TrexStatelessDpCore::handle_pkt_event(CGenNode *node) {

    /* if port has stopped - no transmition */
    if (m_state == STATE_IDLE) {
        return;
    }

    m_core->m_node_gen.m_v_if->send_node(node);

    CGenNodeStateless *node_sl = (CGenNodeStateless *)node;

    /* in case of continues */
    dp_node_extended_info_st *opaque = (dp_node_extended_info_st *)node_sl->get_opaque_storage();
    node->m_time += opaque->next_time_offset;

    /* insert a new event */
    m_core->m_node_gen.m_p_queue.push(node);
}

void
TrexStatelessDpCore::add_cont_stream(double pps, const uint8_t *pkt, uint16_t pkt_len) {
    CGenNodeStateless *node = m_core->create_node_sl();

    /* add periodic */
    node->m_type = CGenNode::STATELESS_PKT;
    node->m_time = m_core->m_cur_time_sec + 0.0 /* STREAM ISG */;
    node->m_flags = 0; 

    /* set socket id */
    node->set_socket_id(m_core->m_node_gen.m_socket_id);

    /* build a mbuf from a packet */
    uint16_t pkt_size = pkt_len;
    const uint8_t *stream_pkt = pkt;

    dp_node_extended_info_st *opaque = (dp_node_extended_info_st *)node->get_opaque_storage();
    opaque->next_time_offset = 1.0 / pps;

    /* allocate const mbuf */
    rte_mbuf_t *m = CGlobalInfo::pktmbuf_alloc(node->get_socket_id(), pkt_size);
    assert(m);

    char *p = rte_pktmbuf_append(m, pkt_size);
    assert(p);
    /* copy the packet */
    memcpy(p,stream_pkt,pkt_size);

    /* set dir 0 or 1 client or server */
    pkt_dir_t dir = 0;
    node->set_mbuf_cache_dir(dir);

    /* TBD repace the mac if req we should add flag  */
    m_core->m_node_gen.m_v_if->update_mac_addr_from_global_cfg(dir, m);

    /* set the packet as a readonly */
    node->set_cache_mbuf(m);

    m_state = TrexStatelessDpCore::STATE_TRANSMITTING;

    m_core->m_node_gen.add_node((CGenNode *)node);
}

void
TrexStatelessDpCore::start_traffic(TrexStreamsCompiledObj *obj) {
    for (auto single_stream : obj->get_objects()) {
        add_cont_stream(single_stream.m_pps, single_stream.m_pkt, single_stream.m_pkt_len);
    }
}

void
TrexStatelessDpCore::stop_traffic() {
    m_state = STATE_IDLE;
}

/**
 * handle a message from CP to DP
 * 
 */
void 
TrexStatelessDpCore::handle_cp_msg(TrexStatelessCpToDpMsgBase *msg) {
    msg->handle(this);
    delete msg;
}