c8b2e19bfae0be5d457ad4661052321dd9564324
[dpdk.git] / lib / librte_port / rte_port_ras.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <string.h>
5
6 #include <rte_ether.h>
7 #include <rte_ip_frag.h>
8 #include <rte_cycles.h>
9 #include <rte_log.h>
10
11 #include "rte_port_ras.h"
12
13 #ifndef RTE_PORT_RAS_N_BUCKETS
14 #define RTE_PORT_RAS_N_BUCKETS                                 4094
15 #endif
16
17 #ifndef RTE_PORT_RAS_N_ENTRIES_PER_BUCKET
18 #define RTE_PORT_RAS_N_ENTRIES_PER_BUCKET                      8
19 #endif
20
21 #ifndef RTE_PORT_RAS_N_ENTRIES
22 #define RTE_PORT_RAS_N_ENTRIES (RTE_PORT_RAS_N_BUCKETS * RTE_PORT_RAS_N_ENTRIES_PER_BUCKET)
23 #endif
24
25 #ifdef RTE_PORT_STATS_COLLECT
26
27 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(port, val) \
28         port->stats.n_pkts_in += val
29 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(port, val) \
30         port->stats.n_pkts_drop += val
31
32 #else
33
34 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(port, val)
35 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(port, val)
36
37 #endif
38
39 struct rte_port_ring_writer_ras;
40
41 typedef void (*ras_op)(
42                 struct rte_port_ring_writer_ras *p,
43                 struct rte_mbuf *pkt);
44
45 static void
46 process_ipv4(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt);
47 static void
48 process_ipv6(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt);
49
50 struct rte_port_ring_writer_ras {
51         struct rte_port_out_stats stats;
52
53         struct rte_mbuf *tx_buf[RTE_PORT_IN_BURST_SIZE_MAX];
54         struct rte_ring *ring;
55         uint32_t tx_burst_sz;
56         uint32_t tx_buf_count;
57         struct rte_ip_frag_tbl *frag_tbl;
58         struct rte_ip_frag_death_row death_row;
59
60         ras_op f_ras;
61 };
62
63 static void *
64 rte_port_ring_writer_ras_create(void *params, int socket_id, int is_ipv4)
65 {
66         struct rte_port_ring_writer_ras_params *conf =
67                         params;
68         struct rte_port_ring_writer_ras *port;
69         uint64_t frag_cycles;
70
71         /* Check input parameters */
72         if (conf == NULL) {
73                 RTE_LOG(ERR, PORT, "%s: Parameter conf is NULL\n", __func__);
74                 return NULL;
75         }
76         if (conf->ring == NULL) {
77                 RTE_LOG(ERR, PORT, "%s: Parameter ring is NULL\n", __func__);
78                 return NULL;
79         }
80         if ((conf->tx_burst_sz == 0) ||
81             (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX)) {
82                 RTE_LOG(ERR, PORT, "%s: Parameter tx_burst_sz is invalid\n",
83                         __func__);
84                 return NULL;
85         }
86
87         /* Memory allocation */
88         port = rte_zmalloc_socket("PORT", sizeof(*port),
89                         RTE_CACHE_LINE_SIZE, socket_id);
90         if (port == NULL) {
91                 RTE_LOG(ERR, PORT, "%s: Failed to allocate socket\n", __func__);
92                 return NULL;
93         }
94
95         /* Create fragmentation table */
96         frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S * MS_PER_S;
97         frag_cycles *= 100;
98
99         port->frag_tbl = rte_ip_frag_table_create(
100                 RTE_PORT_RAS_N_BUCKETS,
101                 RTE_PORT_RAS_N_ENTRIES_PER_BUCKET,
102                 RTE_PORT_RAS_N_ENTRIES,
103                 frag_cycles,
104                 socket_id);
105
106         if (port->frag_tbl == NULL) {
107                 RTE_LOG(ERR, PORT, "%s: rte_ip_frag_table_create failed\n",
108                         __func__);
109                 rte_free(port);
110                 return NULL;
111         }
112
113         /* Initialization */
114         port->ring = conf->ring;
115         port->tx_burst_sz = conf->tx_burst_sz;
116         port->tx_buf_count = 0;
117
118         port->f_ras = (is_ipv4 == 1) ? process_ipv4 : process_ipv6;
119
120         return port;
121 }
122
123 static void *
124 rte_port_ring_writer_ipv4_ras_create(void *params, int socket_id)
125 {
126         return rte_port_ring_writer_ras_create(params, socket_id, 1);
127 }
128
129 static void *
130 rte_port_ring_writer_ipv6_ras_create(void *params, int socket_id)
131 {
132         return rte_port_ring_writer_ras_create(params, socket_id, 0);
133 }
134
135 static inline void
136 send_burst(struct rte_port_ring_writer_ras *p)
137 {
138         uint32_t nb_tx;
139
140         nb_tx = rte_ring_sp_enqueue_burst(p->ring, (void **)p->tx_buf,
141                         p->tx_buf_count, NULL);
142
143         RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(p, p->tx_buf_count - nb_tx);
144         for ( ; nb_tx < p->tx_buf_count; nb_tx++)
145                 rte_pktmbuf_free(p->tx_buf[nb_tx]);
146
147         p->tx_buf_count = 0;
148 }
149
150 static void
151 process_ipv4(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt)
152 {
153         /* Assume there is no ethernet header */
154         struct ipv4_hdr *pkt_hdr = rte_pktmbuf_mtod(pkt, struct ipv4_hdr *);
155
156         /* Get "More fragments" flag and fragment offset */
157         uint16_t frag_field = rte_be_to_cpu_16(pkt_hdr->fragment_offset);
158         uint16_t frag_offset = (uint16_t)(frag_field & IPV4_HDR_OFFSET_MASK);
159         uint16_t frag_flag = (uint16_t)(frag_field & IPV4_HDR_MF_FLAG);
160
161         /* If it is a fragmented packet, then try to reassemble */
162         if ((frag_flag == 0) && (frag_offset == 0))
163                 p->tx_buf[p->tx_buf_count++] = pkt;
164         else {
165                 struct rte_mbuf *mo;
166                 struct rte_ip_frag_tbl *tbl = p->frag_tbl;
167                 struct rte_ip_frag_death_row *dr = &p->death_row;
168
169                 pkt->l3_len = sizeof(*pkt_hdr);
170
171                 /* Process this fragment */
172                 mo = rte_ipv4_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(),
173                                 pkt_hdr);
174                 if (mo != NULL)
175                         p->tx_buf[p->tx_buf_count++] = mo;
176
177                 rte_ip_frag_free_death_row(&p->death_row, 3);
178         }
179 }
180
181 static void
182 process_ipv6(struct rte_port_ring_writer_ras *p, struct rte_mbuf *pkt)
183 {
184         /* Assume there is no ethernet header */
185         struct ipv6_hdr *pkt_hdr = rte_pktmbuf_mtod(pkt, struct ipv6_hdr *);
186
187         struct ipv6_extension_fragment *frag_hdr;
188         uint16_t frag_data = 0;
189         frag_hdr = rte_ipv6_frag_get_ipv6_fragment_header(pkt_hdr);
190         if (frag_hdr != NULL)
191                 frag_data = rte_be_to_cpu_16(frag_hdr->frag_data);
192
193         /* If it is a fragmented packet, then try to reassemble */
194         if ((frag_data & RTE_IPV6_FRAG_USED_MASK) == 0)
195                 p->tx_buf[p->tx_buf_count++] = pkt;
196         else {
197                 struct rte_mbuf *mo;
198                 struct rte_ip_frag_tbl *tbl = p->frag_tbl;
199                 struct rte_ip_frag_death_row *dr = &p->death_row;
200
201                 pkt->l3_len = sizeof(*pkt_hdr) + sizeof(*frag_hdr);
202
203                 /* Process this fragment */
204                 mo = rte_ipv6_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(), pkt_hdr,
205                                 frag_hdr);
206                 if (mo != NULL)
207                         p->tx_buf[p->tx_buf_count++] = mo;
208
209                 rte_ip_frag_free_death_row(&p->death_row, 3);
210         }
211 }
212
213 static int
214 rte_port_ring_writer_ras_tx(void *port, struct rte_mbuf *pkt)
215 {
216         struct rte_port_ring_writer_ras *p =
217                         port;
218
219         RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
220         p->f_ras(p, pkt);
221         if (p->tx_buf_count >= p->tx_burst_sz)
222                 send_burst(p);
223
224         return 0;
225 }
226
227 static int
228 rte_port_ring_writer_ras_tx_bulk(void *port,
229                 struct rte_mbuf **pkts,
230                 uint64_t pkts_mask)
231 {
232         struct rte_port_ring_writer_ras *p =
233                         port;
234
235         if ((pkts_mask & (pkts_mask + 1)) == 0) {
236                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
237                 uint32_t i;
238
239                 for (i = 0; i < n_pkts; i++) {
240                         struct rte_mbuf *pkt = pkts[i];
241
242                         RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
243                         p->f_ras(p, pkt);
244                         if (p->tx_buf_count >= p->tx_burst_sz)
245                                 send_burst(p);
246                 }
247         } else {
248                 for ( ; pkts_mask; ) {
249                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
250                         uint64_t pkt_mask = 1LLU << pkt_index;
251                         struct rte_mbuf *pkt = pkts[pkt_index];
252
253                         RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p, 1);
254                         p->f_ras(p, pkt);
255                         if (p->tx_buf_count >= p->tx_burst_sz)
256                                 send_burst(p);
257
258                         pkts_mask &= ~pkt_mask;
259                 }
260         }
261
262         return 0;
263 }
264
265 static int
266 rte_port_ring_writer_ras_flush(void *port)
267 {
268         struct rte_port_ring_writer_ras *p =
269                         port;
270
271         if (p->tx_buf_count > 0)
272                 send_burst(p);
273
274         return 0;
275 }
276
277 static int
278 rte_port_ring_writer_ras_free(void *port)
279 {
280         struct rte_port_ring_writer_ras *p =
281                         port;
282
283         if (port == NULL) {
284                 RTE_LOG(ERR, PORT, "%s: Parameter port is NULL\n", __func__);
285                 return -1;
286         }
287
288         rte_port_ring_writer_ras_flush(port);
289         rte_ip_frag_table_destroy(p->frag_tbl);
290         rte_free(port);
291
292         return 0;
293 }
294
295 static int
296 rte_port_ras_writer_stats_read(void *port,
297                 struct rte_port_out_stats *stats, int clear)
298 {
299         struct rte_port_ring_writer_ras *p =
300                 port;
301
302         if (stats != NULL)
303                 memcpy(stats, &p->stats, sizeof(p->stats));
304
305         if (clear)
306                 memset(&p->stats, 0, sizeof(p->stats));
307
308         return 0;
309 }
310
311 /*
312  * Summary of port operations
313  */
314 struct rte_port_out_ops rte_port_ring_writer_ipv4_ras_ops = {
315         .f_create = rte_port_ring_writer_ipv4_ras_create,
316         .f_free = rte_port_ring_writer_ras_free,
317         .f_tx = rte_port_ring_writer_ras_tx,
318         .f_tx_bulk = rte_port_ring_writer_ras_tx_bulk,
319         .f_flush = rte_port_ring_writer_ras_flush,
320         .f_stats = rte_port_ras_writer_stats_read,
321 };
322
323 struct rte_port_out_ops rte_port_ring_writer_ipv6_ras_ops = {
324         .f_create = rte_port_ring_writer_ipv6_ras_create,
325         .f_free = rte_port_ring_writer_ras_free,
326         .f_tx = rte_port_ring_writer_ras_tx,
327         .f_tx_bulk = rte_port_ring_writer_ras_tx_bulk,
328         .f_flush = rte_port_ring_writer_ras_flush,
329         .f_stats = rte_port_ras_writer_stats_read,
330 };