xref: /dpdk/examples/l2fwd-event/l2fwd_common.h (revision 68a03efeed657e6e05f281479b33b51102797e15)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4 
5 #ifndef __L2FWD_COMMON_H__
6 #define __L2FWD_COMMON_H__
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdint.h>
12 #include <inttypes.h>
13 #include <sys/types.h>
14 #include <sys/queue.h>
15 #include <netinet/in.h>
16 #include <setjmp.h>
17 #include <stdarg.h>
18 #include <ctype.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <signal.h>
22 #include <stdbool.h>
23 
24 #include <rte_common.h>
25 #include <rte_malloc.h>
26 #include <rte_memory.h>
27 #include <rte_memcpy.h>
28 #include <rte_eal.h>
29 #include <rte_launch.h>
30 #include <rte_atomic.h>
31 #include <rte_cycles.h>
32 #include <rte_prefetch.h>
33 #include <rte_lcore.h>
34 #include <rte_per_lcore.h>
35 #include <rte_branch_prediction.h>
36 #include <rte_interrupts.h>
37 #include <rte_random.h>
38 #include <rte_debug.h>
39 #include <rte_ether.h>
40 #include <rte_ethdev.h>
41 #include <rte_eventdev.h>
42 #include <rte_mempool.h>
43 #include <rte_mbuf.h>
44 #include <rte_spinlock.h>
45 
46 #define MAX_PKT_BURST 32
47 #define MAX_RX_QUEUE_PER_LCORE 16
48 #define MAX_TX_QUEUE_PER_PORT 16
49 
50 #define RTE_TEST_RX_DESC_DEFAULT 1024
51 #define RTE_TEST_TX_DESC_DEFAULT 1024
52 
53 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
54 #define MEMPOOL_CACHE_SIZE 256
55 
56 #define DEFAULT_TIMER_PERIOD	10 /* default period is 10 seconds */
57 #define MAX_TIMER_PERIOD	86400 /* 1 day max */
58 
59 /* Per-port statistics struct */
60 struct l2fwd_port_statistics {
61 	uint64_t dropped;
62 	uint64_t tx;
63 	uint64_t rx;
64 } __rte_cache_aligned;
65 
66 struct l2fwd_resources {
67 	volatile uint8_t force_quit;
68 	uint8_t event_mode;
69 	uint8_t sched_type;
70 	uint8_t mac_updating;
71 	uint8_t rx_queue_per_lcore;
72 	bool port_pairs;
73 	uint16_t nb_rxd;
74 	uint16_t nb_txd;
75 	uint32_t enabled_port_mask;
76 	uint64_t timer_period;
77 	struct rte_mempool *pktmbuf_pool;
78 	uint32_t dst_ports[RTE_MAX_ETHPORTS];
79 	struct rte_ether_addr eth_addr[RTE_MAX_ETHPORTS];
80 	struct l2fwd_port_statistics port_stats[RTE_MAX_ETHPORTS];
81 	void *evt_rsrc;
82 	void *poll_rsrc;
83 } __rte_cache_aligned;
84 
85 static __rte_always_inline void
86 l2fwd_mac_updating(struct rte_mbuf *m, uint32_t dest_port_id,
87 		   struct rte_ether_addr *addr)
88 {
89 	struct rte_ether_hdr *eth;
90 	void *tmp;
91 
92 	eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
93 
94 	/* 02:00:00:00:00:xx */
95 	tmp = &eth->d_addr.addr_bytes[0];
96 	*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_port_id << 40);
97 
98 	/* src addr */
99 	rte_ether_addr_copy(addr, &eth->s_addr);
100 }
101 
102 static __rte_always_inline struct l2fwd_resources *
103 l2fwd_get_rsrc(void)
104 {
105 	static const char name[RTE_MEMZONE_NAMESIZE] = "rsrc";
106 	const struct rte_memzone *mz;
107 
108 	mz = rte_memzone_lookup(name);
109 	if (mz != NULL)
110 		return mz->addr;
111 
112 	mz = rte_memzone_reserve(name, sizeof(struct l2fwd_resources), 0, 0);
113 	if (mz != NULL) {
114 		struct l2fwd_resources *rsrc = mz->addr;
115 
116 		memset(rsrc, 0, sizeof(struct l2fwd_resources));
117 		rsrc->mac_updating = true;
118 		rsrc->event_mode = true;
119 		rsrc->rx_queue_per_lcore = 1;
120 		rsrc->sched_type = RTE_SCHED_TYPE_ATOMIC;
121 		rsrc->timer_period = 10 * rte_get_timer_hz();
122 
123 		return mz->addr;
124 	}
125 
126 	rte_panic("Unable to allocate memory for l2fwd resources\n");
127 
128 	return NULL;
129 }
130 
131 int l2fwd_event_init_ports(struct l2fwd_resources *rsrc);
132 
133 #endif /* __L2FWD_COMMON_H__ */
134