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_cycles.h>
31 #include <rte_prefetch.h>
32 #include <rte_lcore.h>
33 #include <rte_per_lcore.h>
34 #include <rte_branch_prediction.h>
35 #include <rte_interrupts.h>
36 #include <rte_random.h>
37 #include <rte_debug.h>
38 #include <rte_ether.h>
39 #include <rte_ethdev.h>
40 #include <rte_eventdev.h>
41 #include <rte_mempool.h>
42 #include <rte_mbuf.h>
43 #include <rte_spinlock.h>
44
45 #define MAX_PKT_BURST 32
46 #define MAX_RX_QUEUE_PER_LCORE 16
47 #define MAX_TX_QUEUE_PER_PORT 16
48
49 #define RX_DESC_DEFAULT 1024
50 #define TX_DESC_DEFAULT 1024
51
52 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
53 #define MEMPOOL_CACHE_SIZE 256
54
55 #define DEFAULT_TIMER_PERIOD 10 /* default period is 10 seconds */
56 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
57
58 #define VECTOR_SIZE_DEFAULT MAX_PKT_BURST
59 #define VECTOR_TMO_NS_DEFAULT 1E6 /* 1ms */
60
61 /* Per-port statistics struct */
62 struct __rte_cache_aligned l2fwd_port_statistics {
63 uint64_t dropped;
64 RTE_ATOMIC(uint64_t) tx;
65 RTE_ATOMIC(uint64_t) rx;
66 };
67
68 /* Event vector attributes */
69 struct l2fwd_event_vector_params {
70 uint8_t enabled;
71 uint16_t size;
72 uint64_t timeout_ns;
73 };
74
75 struct __rte_cache_aligned l2fwd_resources {
76 volatile uint8_t force_quit;
77 uint8_t event_mode;
78 uint8_t sched_type;
79 uint8_t mac_updating;
80 uint8_t rx_queue_per_lcore;
81 bool port_pairs;
82 uint16_t nb_rxd;
83 uint16_t nb_txd;
84 uint32_t enabled_port_mask;
85 uint64_t timer_period;
86 struct rte_mempool *pktmbuf_pool;
87 struct rte_mempool *evt_vec_pool;
88 uint32_t dst_ports[RTE_MAX_ETHPORTS];
89 struct rte_ether_addr eth_addr[RTE_MAX_ETHPORTS];
90 struct l2fwd_port_statistics port_stats[RTE_MAX_ETHPORTS];
91 struct l2fwd_event_vector_params evt_vec;
92 void *evt_rsrc;
93 void *poll_rsrc;
94 };
95
96 static __rte_always_inline void
l2fwd_mac_updating(struct rte_mbuf * m,uint32_t dest_port_id,struct rte_ether_addr * addr)97 l2fwd_mac_updating(struct rte_mbuf *m, uint32_t dest_port_id,
98 struct rte_ether_addr *addr)
99 {
100 struct rte_ether_hdr *eth;
101 void *tmp;
102
103 eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
104
105 /* 02:00:00:00:00:xx */
106 tmp = ð->dst_addr.addr_bytes[0];
107 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_port_id << 40);
108
109 /* src addr */
110 rte_ether_addr_copy(addr, ð->src_addr);
111 }
112
113 static __rte_always_inline struct l2fwd_resources *
l2fwd_get_rsrc(void)114 l2fwd_get_rsrc(void)
115 {
116 static const char name[RTE_MEMZONE_NAMESIZE] = "rsrc";
117 const struct rte_memzone *mz;
118
119 mz = rte_memzone_lookup(name);
120 if (mz != NULL)
121 return mz->addr;
122
123 mz = rte_memzone_reserve(name, sizeof(struct l2fwd_resources), 0, 0);
124 if (mz != NULL) {
125 struct l2fwd_resources *rsrc = mz->addr;
126
127 memset(rsrc, 0, sizeof(struct l2fwd_resources));
128 rsrc->mac_updating = true;
129 rsrc->event_mode = true;
130 rsrc->rx_queue_per_lcore = 1;
131 rsrc->sched_type = RTE_SCHED_TYPE_ATOMIC;
132 rsrc->timer_period = 10 * rte_get_timer_hz();
133
134 return mz->addr;
135 }
136
137 rte_panic("Unable to allocate memory for l2fwd resources\n");
138
139 return NULL;
140 }
141
142 int l2fwd_event_init_ports(struct l2fwd_resources *rsrc);
143 void l2fwd_event_worker_cleanup(uint8_t event_d_id, uint8_t port_id,
144 struct rte_event events[], uint16_t nb_enq,
145 uint16_t nb_deq, uint8_t is_vector);
146
147 #endif /* __L2FWD_COMMON_H__ */
148