summaryrefslogtreecommitdiffstats
path: root/src/spdk/dpdk/app/test-pmd/noisy_vnf.c
blob: 58c4ee9255665fd35ccac879e7dbdb78cfc50e17 (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
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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018 Red Hat Corp.
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <unistd.h>
#include <inttypes.h>

#include <sys/queue.h>
#include <sys/stat.h>

#include <rte_common.h>
#include <rte_log.h>
#include <rte_debug.h>
#include <rte_cycles.h>
#include <rte_memory.h>
#include <rte_launch.h>
#include <rte_eal.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
#include <rte_memcpy.h>
#include <rte_mempool.h>
#include <rte_mbuf.h>
#include <rte_ethdev.h>
#include <rte_flow.h>
#include <rte_malloc.h>

#include "testpmd.h"

struct noisy_config {
	struct rte_ring *f;
	uint64_t prev_time;
	char *vnf_mem;
	bool do_buffering;
	bool do_flush;
	bool do_sim;
};

struct noisy_config *noisy_cfg[RTE_MAX_ETHPORTS];

static inline void
do_write(char *vnf_mem)
{
	uint64_t i = rte_rand();
	uint64_t w = rte_rand();

	vnf_mem[i % ((noisy_lkup_mem_sz * 1024 * 1024) /
			RTE_CACHE_LINE_SIZE)] = w;
}

static inline void
do_read(char *vnf_mem)
{
	uint64_t i = rte_rand();
	uint64_t r;

	r = vnf_mem[i % ((noisy_lkup_mem_sz * 1024 * 1024) /
			RTE_CACHE_LINE_SIZE)];
	r++;
}

static inline void
do_readwrite(char *vnf_mem)
{
	do_read(vnf_mem);
	do_write(vnf_mem);
}

/*
 * Simulate route lookups as defined by commandline parameters
 */
static void
sim_memory_lookups(struct noisy_config *ncf, uint16_t nb_pkts)
{
	uint16_t i, j;

	if (!ncf->do_sim)
		return;

	for (i = 0; i < nb_pkts; i++) {
		for (j = 0; j < noisy_lkup_num_writes; j++)
			do_write(ncf->vnf_mem);
		for (j = 0; j < noisy_lkup_num_reads; j++)
			do_read(ncf->vnf_mem);
		for (j = 0; j < noisy_lkup_num_reads_writes; j++)
			do_readwrite(ncf->vnf_mem);
	}
}

static uint16_t
do_retry(uint16_t nb_rx, uint16_t nb_tx, struct rte_mbuf **pkts,
	 struct fwd_stream *fs)
{
	uint32_t retry = 0;

	while (nb_tx < nb_rx && retry++ < burst_tx_retry_num) {
		rte_delay_us(burst_tx_delay_time);
		nb_tx += rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
				&pkts[nb_tx], nb_rx - nb_tx);
	}

	return nb_tx;
}

static uint32_t
drop_pkts(struct rte_mbuf **pkts, uint16_t nb_rx, uint16_t nb_tx)
{
	if (nb_tx < nb_rx) {
		do {
			rte_pktmbuf_free(pkts[nb_tx]);
		} while (++nb_tx < nb_rx);
	}

	return nb_rx - nb_tx;
}

/*
 * Forwarding of packets in noisy VNF mode.  Forward packets but perform
 * memory operations first as specified on cmdline.
 *
 * Depending on which commandline parameters are specified we have
 * different cases to handle:
 *
 * 1. No FIFO size was given, so we don't do buffering of incoming
 *    packets.  This case is pretty much what iofwd does but in this case
 *    we also do simulation of memory accesses (depending on which
 *    parameters were specified for it).
 * 2. User wants do buffer packets in a FIFO and sent out overflowing
 *    packets.
 * 3. User wants a FIFO and specifies a time in ms to flush all packets
 *    out of the FIFO
 * 4. Cases 2 and 3 combined
 */
static void
pkt_burst_noisy_vnf(struct fwd_stream *fs)
{
	const uint64_t freq_khz = rte_get_timer_hz() / 1000;
	struct noisy_config *ncf = noisy_cfg[fs->rx_port];
	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
	struct rte_mbuf *tmp_pkts[MAX_PKT_BURST];
	uint16_t nb_deqd = 0;
	uint16_t nb_rx = 0;
	uint16_t nb_tx = 0;
	uint16_t nb_enqd;
	unsigned int fifo_free;
	uint64_t delta_ms;
	bool needs_flush = false;
	uint64_t now;

	nb_rx = rte_eth_rx_burst(fs->rx_port, fs->rx_queue,
			pkts_burst, nb_pkt_per_burst);
	if (unlikely(nb_rx == 0))
		goto flush;
	fs->rx_packets += nb_rx;

	if (!ncf->do_buffering) {
		sim_memory_lookups(ncf, nb_rx);
		nb_tx = rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
				pkts_burst, nb_rx);
		if (unlikely(nb_tx < nb_rx) && fs->retry_enabled)
			nb_tx += do_retry(nb_rx, nb_tx, pkts_burst, fs);
		fs->tx_packets += nb_tx;
		fs->fwd_dropped += drop_pkts(pkts_burst, nb_rx, nb_tx);
		return;
	}

	fifo_free = rte_ring_free_count(ncf->f);
	if (fifo_free >= nb_rx) {
		nb_enqd = rte_ring_enqueue_burst(ncf->f,
				(void **) pkts_burst, nb_rx, NULL);
		if (nb_enqd < nb_rx)
			fs->fwd_dropped += drop_pkts(pkts_burst,
						     nb_rx, nb_enqd);
	} else {
		nb_deqd = rte_ring_dequeue_burst(ncf->f,
				(void **) tmp_pkts, nb_rx, NULL);
		nb_enqd = rte_ring_enqueue_burst(ncf->f,
				(void **) pkts_burst, nb_deqd, NULL);
		if (nb_deqd > 0) {
			nb_tx = rte_eth_tx_burst(fs->tx_port,
					fs->tx_queue, tmp_pkts,
					nb_deqd);
			if (unlikely(nb_tx < nb_rx) && fs->retry_enabled)
				nb_tx += do_retry(nb_rx, nb_tx, tmp_pkts, fs);
			fs->fwd_dropped += drop_pkts(tmp_pkts, nb_deqd, nb_tx);
		}
	}

	sim_memory_lookups(ncf, nb_enqd);

flush:
	if (ncf->do_flush) {
		if (!ncf->prev_time)
			now = ncf->prev_time = rte_get_timer_cycles();
		else
			now = rte_get_timer_cycles();
		delta_ms = (now - ncf->prev_time) / freq_khz;
		needs_flush = delta_ms >= noisy_tx_sw_buf_flush_time &&
				noisy_tx_sw_buf_flush_time > 0 && !nb_tx;
	}
	while (needs_flush && !rte_ring_empty(ncf->f)) {
		unsigned int sent;
		nb_deqd = rte_ring_dequeue_burst(ncf->f, (void **)tmp_pkts,
				MAX_PKT_BURST, NULL);
		sent = rte_eth_tx_burst(fs->tx_port, fs->tx_queue,
					 tmp_pkts, nb_deqd);
		if (unlikely(sent < nb_deqd) && fs->retry_enabled)
			nb_tx += do_retry(nb_rx, nb_tx, tmp_pkts, fs);
		fs->fwd_dropped += drop_pkts(tmp_pkts, nb_deqd, sent);
		ncf->prev_time = rte_get_timer_cycles();
	}
}

#define NOISY_STRSIZE 256
#define NOISY_RING "noisy_ring_%d\n"

static void
noisy_fwd_end(portid_t pi)
{
	rte_ring_free(noisy_cfg[pi]->f);
	rte_free(noisy_cfg[pi]->vnf_mem);
	rte_free(noisy_cfg[pi]);
}

static void
noisy_fwd_begin(portid_t pi)
{
	struct noisy_config *n;
	char name[NOISY_STRSIZE];

	noisy_cfg[pi] = rte_zmalloc("testpmd noisy fifo and timers",
				sizeof(struct noisy_config),
				RTE_CACHE_LINE_SIZE);
	if (noisy_cfg[pi] == NULL) {
		rte_exit(EXIT_FAILURE,
			 "rte_zmalloc(%d) struct noisy_config) failed\n",
			 (int) pi);
	}
	n = noisy_cfg[pi];
	n->do_buffering = noisy_tx_sw_bufsz > 0;
	n->do_sim = noisy_lkup_num_writes + noisy_lkup_num_reads +
		    noisy_lkup_num_reads_writes;
	n->do_flush = noisy_tx_sw_buf_flush_time > 0;

	if (n->do_buffering) {
		snprintf(name, NOISY_STRSIZE, NOISY_RING, pi);
		n->f = rte_ring_create(name, noisy_tx_sw_bufsz,
				rte_socket_id(), 0);
		if (!n->f)
			rte_exit(EXIT_FAILURE,
				 "rte_ring_create(%d), size %d) failed\n",
				 (int) pi,
				 noisy_tx_sw_bufsz);
	}
	if (noisy_lkup_mem_sz > 0) {
		n->vnf_mem = (char *) rte_zmalloc("vnf sim memory",
				 noisy_lkup_mem_sz * 1024 * 1024,
				 RTE_CACHE_LINE_SIZE);
		if (!n->vnf_mem)
			rte_exit(EXIT_FAILURE,
			   "rte_zmalloc(%" PRIu64 ") for vnf memory) failed\n",
			   noisy_lkup_mem_sz);
	} else if (n->do_sim) {
		rte_exit(EXIT_FAILURE,
			 "--noisy-lkup-memory-size must be > 0\n");
	}
}

struct fwd_engine noisy_vnf_engine = {
	.fwd_mode_name  = "noisy",
	.port_fwd_begin = noisy_fwd_begin,
	.port_fwd_end   = noisy_fwd_end,
	.packet_fwd     = pkt_burst_noisy_vnf,
};