forked from sdnfv/openNetVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonvm_pkt_common.c
322 lines (278 loc) · 11.5 KB
/
onvm_pkt_common.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*********************************************************************
* openNetVM
* https://sdnfv.github.io
*
* BSD LICENSE
*
* Copyright(c)
* 2015-2019 George Washington University
* 2015-2019 University of California Riverside
* 2010-2019 Intel Corporation. All rights reserved.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
********************************************************************/
/******************************************************************************
onvm_pkt_common.c
This file contains all functions related to receiving or
transmitting packets.
******************************************************************************/
#include "onvm_pkt_common.h"
/**********************Internal Functions Prototypes**************************/
/*
* Function to enqueue a packet on one port's queue.
*
* Inputs : a pointer to the tx queue responsible
* the number of the port
* a pointer to the packet
*
*/
static inline void
onvm_pkt_enqueue_port(struct queue_mgr *tx_mgr, uint16_t port, struct rte_mbuf *buf);
/*
* Function to process a single packet.
*
* Inputs : a pointer to the tx queue responsible
* a pointer to the packet
* a pointer to the NF involved
*
*/
static inline void
onvm_pkt_process_next_action(struct queue_mgr *tx_mgr, struct rte_mbuf *pkt, struct onvm_nf *nf);
/*
* Helper function to drop a packet.
*
* Input : a pointer to the packet
*
* Ouput : an error code
*
*/
static int
onvm_pkt_drop(struct rte_mbuf *pkt);
/**********************************Interfaces*********************************/
void
onvm_pkt_process_tx_batch(struct queue_mgr *tx_mgr, struct rte_mbuf *pkts[], uint16_t tx_count, struct onvm_nf *nf) {
uint16_t i;
struct onvm_pkt_meta *meta;
struct packet_buf *out_buf;
if (tx_mgr == NULL || pkts == NULL || nf == NULL)
return;
for (i = 0; i < tx_count; i++) {
meta = (struct onvm_pkt_meta *)&(((struct rte_mbuf *)pkts[i])->udata64);
meta->src = nf->instance_id;
if (meta->action == ONVM_NF_ACTION_DROP) {
// if the packet is drop, then <return value> is 0
// and !<return value> is 1.
nf->stats.act_drop++;
nf->stats.tx += !onvm_pkt_drop(pkts[i]);
} else if (meta->action == ONVM_NF_ACTION_NEXT) {
/* TODO: Here we drop the packet : there will be a flow table
in the future to know what to do with the packet next */
nf->stats.act_next++;
onvm_pkt_process_next_action(tx_mgr, pkts[i], nf);
} else if (meta->action == ONVM_NF_ACTION_TONF) {
nf->stats.act_tonf++;
onvm_pkt_enqueue_nf(tx_mgr, meta->destination, pkts[i], nf);
} else if (meta->action == ONVM_NF_ACTION_OUT) {
if (tx_mgr->mgr_type_t != MGR) {
nf->stats.act_out++;
out_buf = tx_mgr->to_tx_buf;
out_buf->buffer[out_buf->count++] = pkts[i];
if (out_buf->count == PACKET_READ_SIZE) {
onvm_pkt_enqueue_tx_thread(out_buf, nf);
}
} else {
onvm_pkt_enqueue_port(tx_mgr, meta->destination, pkts[i]);
}
} else {
printf("ERROR invalid action : this shouldn't happen.\n");
onvm_pkt_drop(pkts[i]);
return;
}
}
}
void
onvm_pkt_flush_all_nfs(struct queue_mgr *tx_mgr, struct onvm_nf *source_nf) {
uint16_t i;
if (tx_mgr == NULL)
return;
for (i = 0; i < MAX_NFS; i++)
onvm_pkt_flush_nf_queue(tx_mgr, i, source_nf);
}
void
onvm_pkt_flush_nf_queue(struct queue_mgr *tx_mgr, uint16_t nf_id, struct onvm_nf *source_nf) {
uint16_t i;
struct onvm_nf *nf;
struct packet_buf *nf_buf;
if (tx_mgr == NULL)
return;
nf_buf = &tx_mgr->nf_rx_bufs[nf_id];
if (nf_buf->count == 0)
return;
nf = &nfs[nf_id];
// Ensure destination NF is running and ready to receive packets
if (!onvm_nf_is_valid(nf))
return;
if (rte_ring_enqueue_bulk(nf->rx_q, (void **)nf_buf->buffer, nf_buf->count, NULL) == 0) {
for (i = 0; i < nf_buf->count; i++) {
onvm_pkt_drop(nf_buf->buffer[i]);
}
nf->stats.rx_drop += nf_buf->count;
if (source_nf != NULL)
source_nf->stats.tx_drop += nf_buf->count;
} else {
nf->stats.rx += nf_buf->count;
if (source_nf != NULL)
source_nf->stats.tx += nf_buf->count;
}
nf_buf->count = 0;
}
void
onvm_pkt_enqueue_nf(struct queue_mgr *tx_mgr, uint16_t dst_service_id, struct rte_mbuf *pkt,
struct onvm_nf *source_nf) {
struct onvm_nf *nf;
uint16_t dst_instance_id;
struct packet_buf *nf_buf;
if (tx_mgr == NULL || pkt == NULL)
return;
// map service to instance and check one exists
dst_instance_id = onvm_sc_service_to_nf_map(dst_service_id, pkt);
if (dst_instance_id == 0) {
onvm_pkt_drop(pkt);
if (source_nf != NULL)
source_nf->stats.tx_drop++;
return;
}
// Ensure destination NF is running and ready to receive packets
nf = &nfs[dst_instance_id];
if (!onvm_nf_is_valid(nf)) {
onvm_pkt_drop(pkt);
if (source_nf != NULL)
source_nf->stats.tx_drop++;
return;
}
nf_buf = &tx_mgr->nf_rx_bufs[dst_instance_id];
nf_buf->buffer[nf_buf->count++] = pkt;
if (nf_buf->count == PACKET_READ_SIZE) {
onvm_pkt_flush_nf_queue(tx_mgr, dst_instance_id, source_nf);
}
}
void
onvm_pkt_flush_port_queue(struct queue_mgr *tx_mgr, uint16_t port) {
uint16_t i, sent;
volatile struct tx_stats *tx_stats;
struct packet_buf *port_buf;
if (tx_mgr == NULL || tx_mgr->mgr_type_t != MGR)
return;
port_buf = &tx_mgr->tx_thread_info->port_tx_bufs[port];
if (port_buf->count == 0)
return;
tx_stats = &(ports->tx_stats);
sent = rte_eth_tx_burst(port, tx_mgr->id, port_buf->buffer, port_buf->count);
if (unlikely(sent < port_buf->count)) {
for (i = sent; i < port_buf->count; i++) {
onvm_pkt_drop(port_buf->buffer[i]);
}
tx_stats->tx_drop[port] += (port_buf->count - sent);
}
tx_stats->tx[port] += sent;
port_buf->count = 0;
}
void
onvm_pkt_enqueue_tx_thread(struct packet_buf *pkt_buf, struct onvm_nf *nf) {
uint16_t i;
if (pkt_buf->count == 0)
return;
if (unlikely(pkt_buf->count > 0 &&
rte_ring_enqueue_bulk(nf->tx_q, (void **)pkt_buf->buffer, pkt_buf->count, NULL) == 0)) {
nf->stats.tx_drop += pkt_buf->count;
for (i = 0; i < pkt_buf->count; i++) {
rte_pktmbuf_free(pkt_buf->buffer[i]);
}
} else {
nf->stats.tx += pkt_buf->count;
}
pkt_buf->count = 0;
}
/****************************Internal functions*******************************/
inline static void
onvm_pkt_enqueue_port(struct queue_mgr *tx_mgr, uint16_t port, struct rte_mbuf *buf) {
struct packet_buf *port_buf;
if (tx_mgr == NULL || buf == NULL || !ports->init[port])
return;
port_buf = &tx_mgr->tx_thread_info->port_tx_bufs[port];
port_buf->buffer[port_buf->count++] = buf;
if (port_buf->count == PACKET_READ_SIZE) {
onvm_pkt_flush_port_queue(tx_mgr, port);
}
}
inline static void
onvm_pkt_process_next_action(struct queue_mgr *tx_mgr, struct rte_mbuf *pkt, struct onvm_nf *nf) {
if (tx_mgr == NULL || pkt == NULL || nf == NULL)
return;
struct onvm_flow_entry *flow_entry;
struct onvm_service_chain *sc;
struct onvm_pkt_meta *meta = onvm_get_pkt_meta(pkt);
int ret;
ret = onvm_flow_dir_get_pkt(pkt, &flow_entry);
if (ret >= 0) {
sc = flow_entry->sc;
meta->action = onvm_sc_next_action(sc, pkt);
meta->destination = onvm_sc_next_destination(sc, pkt);
} else {
meta->action = onvm_sc_next_action(default_chain, pkt);
meta->destination = onvm_sc_next_destination(default_chain, pkt);
}
switch (meta->action) {
case ONVM_NF_ACTION_DROP:
// if the packet is drop, then <return value> is 0
// and !<return value> is 1.
nf->stats.act_drop += !onvm_pkt_drop(pkt);
break;
case ONVM_NF_ACTION_TONF:
nf->stats.act_tonf++;
onvm_pkt_enqueue_nf(tx_mgr, meta->destination, pkt, nf);
break;
case ONVM_NF_ACTION_OUT:
nf->stats.act_out++;
onvm_pkt_enqueue_port(tx_mgr, meta->destination, pkt);
break;
default:
break;
}
(meta->chain_index)++;
}
/*******************************Helper function*******************************/
static int
onvm_pkt_drop(struct rte_mbuf *pkt) {
rte_pktmbuf_free(pkt);
if (pkt != NULL) {
return 1;
}
return 0;
}