iavf: new driver using new dev infra
[vpp.git] / src / plugins / dev_iavf / tx_node.c
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright (c) 2023 Cisco Systems, Inc.
3  */
4
5 #include <vlib/vlib.h>
6 #include <vppinfra/ring.h>
7 #include <vppinfra/vector/ip_csum.h>
8
9 #include <vnet/dev/dev.h>
10 #include <vnet/ethernet/ethernet.h>
11 #include <vnet/ip/ip4_packet.h>
12 #include <vnet/ip/ip6_packet.h>
13 #include <vnet/udp/udp_packet.h>
14 #include <vnet/tcp/tcp_packet.h>
15
16 #include <dev_iavf/iavf.h>
17
18 static_always_inline u8
19 iavf_tx_desc_get_dtyp (iavf_tx_desc_t *d)
20 {
21   return d->qword[1] & 0x0f;
22 }
23
24 struct iavf_ip4_psh
25 {
26   u32 src;
27   u32 dst;
28   u8 zero;
29   u8 proto;
30   u16 l4len;
31 };
32
33 struct iavf_ip6_psh
34 {
35   ip6_address_t src;
36   ip6_address_t dst;
37   u32 l4len;
38   u32 proto;
39 };
40
41 static_always_inline u64
42 iavf_tx_prepare_cksum (vlib_buffer_t *b, u8 is_tso)
43 {
44   u64 flags = 0;
45   if (!is_tso && !(b->flags & VNET_BUFFER_F_OFFLOAD))
46     return 0;
47
48   vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags;
49   u32 is_tcp = is_tso || oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM;
50   u32 is_udp = !is_tso && oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM;
51
52   if (!is_tcp && !is_udp)
53     return 0;
54
55   u32 is_ip4 = b->flags & VNET_BUFFER_F_IS_IP4;
56   u32 is_ip6 = b->flags & VNET_BUFFER_F_IS_IP6;
57
58   ASSERT (!(is_tcp && is_udp));
59   ASSERT (is_ip4 || is_ip6);
60   i16 l2_hdr_offset = b->current_data;
61   i16 l3_hdr_offset = vnet_buffer (b)->l3_hdr_offset;
62   i16 l4_hdr_offset = vnet_buffer (b)->l4_hdr_offset;
63   u16 l2_len = l3_hdr_offset - l2_hdr_offset;
64   u16 l3_len = l4_hdr_offset - l3_hdr_offset;
65   ip4_header_t *ip4 = (void *) (b->data + l3_hdr_offset);
66   ip6_header_t *ip6 = (void *) (b->data + l3_hdr_offset);
67   tcp_header_t *tcp = (void *) (b->data + l4_hdr_offset);
68   udp_header_t *udp = (void *) (b->data + l4_hdr_offset);
69   u16 l4_len = is_tcp ? tcp_header_bytes (tcp) : sizeof (udp_header_t);
70   u16 sum = 0;
71
72   flags |= IAVF_TXD_OFFSET_MACLEN (l2_len) | IAVF_TXD_OFFSET_IPLEN (l3_len) |
73            IAVF_TXD_OFFSET_L4LEN (l4_len);
74   flags |= is_ip4 ? IAVF_TXD_CMD_IIPT_IPV4 : IAVF_TXD_CMD_IIPT_IPV6;
75   flags |= is_tcp ? IAVF_TXD_CMD_L4T_TCP : IAVF_TXD_CMD_L4T_UDP;
76
77   if (is_ip4)
78     ip4->checksum = 0;
79
80   if (is_tso)
81     {
82       if (is_ip4)
83         ip4->length = 0;
84       else
85         ip6->payload_length = 0;
86     }
87
88   if (is_ip4)
89     {
90       struct iavf_ip4_psh psh = { 0 };
91       psh.src = ip4->src_address.as_u32;
92       psh.dst = ip4->dst_address.as_u32;
93       psh.proto = ip4->protocol;
94       psh.l4len = is_tso ?
95                           0 :
96                           clib_host_to_net_u16 (clib_net_to_host_u16 (ip4->length) -
97                                           (l4_hdr_offset - l3_hdr_offset));
98       sum = ~clib_ip_csum ((u8 *) &psh, sizeof (psh));
99     }
100   else
101     {
102       struct iavf_ip6_psh psh = { 0 };
103       psh.src = ip6->src_address;
104       psh.dst = ip6->dst_address;
105       psh.proto = clib_host_to_net_u32 ((u32) ip6->protocol);
106       psh.l4len = is_tso ? 0 : ip6->payload_length;
107       sum = ~clib_ip_csum ((u8 *) &psh, sizeof (psh));
108     }
109
110   if (is_tcp)
111     tcp->checksum = sum;
112   else
113     udp->checksum = sum;
114   return flags;
115 }
116
117 static_always_inline u32
118 iavf_tx_fill_ctx_desc (vlib_main_t *vm, vnet_dev_tx_queue_t *txq,
119                        iavf_tx_desc_t *d, vlib_buffer_t *b)
120 {
121   iavf_txq_t *atq = vnet_dev_get_tx_queue_data (txq);
122   vlib_buffer_t *ctx_ph;
123   u32 *bi = atq->ph_bufs;
124
125 next:
126   ctx_ph = vlib_get_buffer (vm, bi[0]);
127   if (PREDICT_FALSE (ctx_ph->ref_count == 255))
128     {
129       bi++;
130       goto next;
131     }
132
133   /* Acquire a reference on the placeholder buffer */
134   ctx_ph->ref_count++;
135
136   u16 l234hdr_sz = vnet_buffer (b)->l4_hdr_offset - b->current_data +
137                    vnet_buffer2 (b)->gso_l4_hdr_sz;
138   u16 tlen = vlib_buffer_length_in_chain (vm, b) - l234hdr_sz;
139   d[0].qword[0] = 0;
140   d[0].qword[1] = IAVF_TXD_DTYP_CTX | IAVF_TXD_CTX_CMD_TSO |
141                   IAVF_TXD_CTX_SEG_MSS (vnet_buffer2 (b)->gso_size) |
142                   IAVF_TXD_CTX_SEG_TLEN (tlen);
143   return bi[0];
144 }
145
146 static_always_inline void
147 iavf_tx_copy_desc (iavf_tx_desc_t *d, iavf_tx_desc_t *s, u32 n_descs)
148 {
149 #if defined CLIB_HAVE_VEC512
150   while (n_descs >= 8)
151     {
152       u64x8u *dv = (u64x8u *) d;
153       u64x8u *sv = (u64x8u *) s;
154
155       dv[0] = sv[0];
156       dv[1] = sv[1];
157
158       /* next */
159       d += 8;
160       s += 8;
161       n_descs -= 8;
162     }
163 #elif defined CLIB_HAVE_VEC256
164   while (n_descs >= 4)
165     {
166       u64x4u *dv = (u64x4u *) d;
167       u64x4u *sv = (u64x4u *) s;
168
169       dv[0] = sv[0];
170       dv[1] = sv[1];
171
172       /* next */
173       d += 4;
174       s += 4;
175       n_descs -= 4;
176     }
177 #elif defined CLIB_HAVE_VEC128
178   while (n_descs >= 2)
179     {
180       u64x2u *dv = (u64x2u *) d;
181       u64x2u *sv = (u64x2u *) s;
182
183       dv[0] = sv[0];
184       dv[1] = sv[1];
185
186       /* next */
187       d += 2;
188       s += 2;
189       n_descs -= 2;
190     }
191 #endif
192   while (n_descs)
193     {
194       d[0].qword[0] = s[0].qword[0];
195       d[0].qword[1] = s[0].qword[1];
196       d++;
197       s++;
198       n_descs--;
199     }
200 }
201
202 static_always_inline void
203 iavf_tx_fill_data_desc (vlib_main_t *vm, iavf_tx_desc_t *d, vlib_buffer_t *b,
204                         u64 cmd, int use_va_dma)
205 {
206   if (use_va_dma)
207     d->qword[0] = vlib_buffer_get_current_va (b);
208   else
209     d->qword[0] = vlib_buffer_get_current_pa (vm, b);
210   d->qword[1] = (((u64) b->current_length) << 34 | cmd | IAVF_TXD_CMD_RSV);
211 }
212 static_always_inline u16
213 iavf_tx_prepare (vlib_main_t *vm, vlib_node_runtime_t *node,
214                  vnet_dev_tx_queue_t *txq, u32 *buffers, u32 n_packets,
215                  u16 *n_enq_descs, int use_va_dma)
216 {
217   iavf_txq_t *atq = vnet_dev_get_tx_queue_data (txq);
218   const u64 cmd_eop = IAVF_TXD_CMD_EOP;
219   u16 n_free_desc, n_desc_left, n_packets_left = n_packets;
220 #if defined CLIB_HAVE_VEC512
221   vlib_buffer_t *b[8];
222 #else
223   vlib_buffer_t *b[4];
224 #endif
225   iavf_tx_desc_t *d = atq->tmp_descs;
226   u32 *tb = atq->tmp_bufs;
227
228   n_free_desc = n_desc_left = txq->size - atq->n_enqueued - 8;
229
230   if (n_desc_left == 0)
231     return 0;
232
233   while (n_packets_left && n_desc_left)
234     {
235 #if defined CLIB_HAVE_VEC512
236       u32 flags;
237       u64x8 or_flags_vec512;
238       u64x8 flags_mask_vec512;
239 #else
240       u32 flags, or_flags;
241 #endif
242
243 #if defined CLIB_HAVE_VEC512
244       if (n_packets_left < 8 || n_desc_left < 8)
245 #else
246       if (n_packets_left < 8 || n_desc_left < 4)
247 #endif
248         goto one_by_one;
249
250 #if defined CLIB_HAVE_VEC512
251       u64x8 base_ptr = u64x8_splat (vm->buffer_main->buffer_mem_start);
252       u32x8 buf_indices = u32x8_load_unaligned (buffers);
253
254       *(u64x8 *) &b = base_ptr + u64x8_from_u32x8 (
255                                    buf_indices << CLIB_LOG2_CACHE_LINE_BYTES);
256
257       or_flags_vec512 = u64x8_i64gather (u64x8_load_unaligned (b), 0, 1);
258 #else
259       vlib_prefetch_buffer_with_index (vm, buffers[4], LOAD);
260       vlib_prefetch_buffer_with_index (vm, buffers[5], LOAD);
261       vlib_prefetch_buffer_with_index (vm, buffers[6], LOAD);
262       vlib_prefetch_buffer_with_index (vm, buffers[7], LOAD);
263
264       b[0] = vlib_get_buffer (vm, buffers[0]);
265       b[1] = vlib_get_buffer (vm, buffers[1]);
266       b[2] = vlib_get_buffer (vm, buffers[2]);
267       b[3] = vlib_get_buffer (vm, buffers[3]);
268
269       or_flags = b[0]->flags | b[1]->flags | b[2]->flags | b[3]->flags;
270 #endif
271
272 #if defined CLIB_HAVE_VEC512
273       flags_mask_vec512 = u64x8_splat (
274         VLIB_BUFFER_NEXT_PRESENT | VNET_BUFFER_F_OFFLOAD | VNET_BUFFER_F_GSO);
275       if (PREDICT_FALSE (
276             !u64x8_is_all_zero (or_flags_vec512 & flags_mask_vec512)))
277 #else
278       if (PREDICT_FALSE (or_flags &
279                          (VLIB_BUFFER_NEXT_PRESENT | VNET_BUFFER_F_OFFLOAD |
280                           VNET_BUFFER_F_GSO)))
281 #endif
282         goto one_by_one;
283
284 #if defined CLIB_HAVE_VEC512
285       vlib_buffer_copy_indices (tb, buffers, 8);
286       iavf_tx_fill_data_desc (vm, d + 0, b[0], cmd_eop, use_va_dma);
287       iavf_tx_fill_data_desc (vm, d + 1, b[1], cmd_eop, use_va_dma);
288       iavf_tx_fill_data_desc (vm, d + 2, b[2], cmd_eop, use_va_dma);
289       iavf_tx_fill_data_desc (vm, d + 3, b[3], cmd_eop, use_va_dma);
290       iavf_tx_fill_data_desc (vm, d + 4, b[4], cmd_eop, use_va_dma);
291       iavf_tx_fill_data_desc (vm, d + 5, b[5], cmd_eop, use_va_dma);
292       iavf_tx_fill_data_desc (vm, d + 6, b[6], cmd_eop, use_va_dma);
293       iavf_tx_fill_data_desc (vm, d + 7, b[7], cmd_eop, use_va_dma);
294
295       buffers += 8;
296       n_packets_left -= 8;
297       n_desc_left -= 8;
298       d += 8;
299       tb += 8;
300 #else
301       vlib_buffer_copy_indices (tb, buffers, 4);
302
303       iavf_tx_fill_data_desc (vm, d + 0, b[0], cmd_eop, use_va_dma);
304       iavf_tx_fill_data_desc (vm, d + 1, b[1], cmd_eop, use_va_dma);
305       iavf_tx_fill_data_desc (vm, d + 2, b[2], cmd_eop, use_va_dma);
306       iavf_tx_fill_data_desc (vm, d + 3, b[3], cmd_eop, use_va_dma);
307
308       buffers += 4;
309       n_packets_left -= 4;
310       n_desc_left -= 4;
311       d += 4;
312       tb += 4;
313 #endif
314
315       continue;
316
317     one_by_one:
318       tb[0] = buffers[0];
319       b[0] = vlib_get_buffer (vm, buffers[0]);
320       flags = b[0]->flags;
321
322       /* No chained buffers or TSO case */
323       if (PREDICT_TRUE (
324             (flags & (VLIB_BUFFER_NEXT_PRESENT | VNET_BUFFER_F_GSO)) == 0))
325         {
326           u64 cmd = cmd_eop;
327
328           if (PREDICT_FALSE (flags & VNET_BUFFER_F_OFFLOAD))
329             cmd |= iavf_tx_prepare_cksum (b[0], 0 /* is_tso */);
330
331           iavf_tx_fill_data_desc (vm, d, b[0], cmd, use_va_dma);
332         }
333       else
334         {
335           u16 n_desc_needed = 1;
336           u64 cmd = 0;
337
338           if (flags & VLIB_BUFFER_NEXT_PRESENT)
339             {
340               vlib_buffer_t *next = vlib_get_buffer (vm, b[0]->next_buffer);
341               n_desc_needed = 2;
342               while (next->flags & VLIB_BUFFER_NEXT_PRESENT)
343                 {
344                   next = vlib_get_buffer (vm, next->next_buffer);
345                   n_desc_needed++;
346                 }
347             }
348
349           if (flags & VNET_BUFFER_F_GSO)
350             {
351               n_desc_needed++;
352             }
353           else if (PREDICT_FALSE (n_desc_needed > 8))
354             {
355               vlib_buffer_free_one (vm, buffers[0]);
356               vlib_error_count (vm, node->node_index,
357                                 IAVF_TX_NODE_CTR_SEG_SZ_EXCEEDED, 1);
358               n_packets_left -= 1;
359               buffers += 1;
360               continue;
361             }
362
363           if (PREDICT_FALSE (n_desc_left < n_desc_needed))
364             break;
365
366           if (flags & VNET_BUFFER_F_GSO)
367             {
368               /* Enqueue a context descriptor */
369               tb[1] = tb[0];
370               tb[0] = iavf_tx_fill_ctx_desc (vm, txq, d, b[0]);
371               n_desc_left -= 1;
372               d += 1;
373               tb += 1;
374               cmd = iavf_tx_prepare_cksum (b[0], 1 /* is_tso */);
375             }
376           else if (flags & VNET_BUFFER_F_OFFLOAD)
377             {
378               cmd = iavf_tx_prepare_cksum (b[0], 0 /* is_tso */);
379             }
380
381           /* Deal with chain buffer if present */
382           while (b[0]->flags & VLIB_BUFFER_NEXT_PRESENT)
383             {
384               iavf_tx_fill_data_desc (vm, d, b[0], cmd, use_va_dma);
385
386               n_desc_left -= 1;
387               d += 1;
388               tb += 1;
389
390               tb[0] = b[0]->next_buffer;
391               b[0] = vlib_get_buffer (vm, b[0]->next_buffer);
392             }
393
394           iavf_tx_fill_data_desc (vm, d, b[0], cmd_eop | cmd, use_va_dma);
395         }
396
397       buffers += 1;
398       n_packets_left -= 1;
399       n_desc_left -= 1;
400       d += 1;
401       tb += 1;
402     }
403
404   *n_enq_descs = n_free_desc - n_desc_left;
405   return n_packets - n_packets_left;
406 }
407
408 VNET_DEV_NODE_FN (iavf_tx_node)
409 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
410 {
411   vnet_dev_tx_node_runtime_t *rt = vnet_dev_get_tx_node_runtime (node);
412   vnet_dev_tx_queue_t *txq = rt->tx_queue;
413   vnet_dev_port_t *port = txq->port;
414   vnet_dev_t *dev = port->dev;
415   iavf_txq_t *atq = vnet_dev_get_tx_queue_data (txq);
416   u16 next;
417   u16 mask = txq->size - 1;
418   u32 *buffers = vlib_frame_vector_args (frame);
419   u16 n_enq, n_left, n_desc, *slot;
420   u16 n_retry = 2;
421
422   n_left = frame->n_vectors;
423
424   vnet_dev_tx_queue_lock_if_needed (txq);
425
426 retry:
427   next = atq->next;
428   /* release consumed bufs */
429   if (atq->n_enqueued)
430     {
431       i32 complete_slot = -1;
432       while (1)
433         {
434           u16 *slot = clib_ring_get_first (atq->rs_slots);
435
436           if (slot == 0)
437             break;
438
439           if (iavf_tx_desc_get_dtyp (atq->descs + slot[0]) != 0x0F)
440             break;
441
442           complete_slot = slot[0];
443
444           clib_ring_deq (atq->rs_slots);
445         }
446
447       if (complete_slot >= 0)
448         {
449           u16 first, mask, n_free;
450           mask = txq->size - 1;
451           first = (atq->next - atq->n_enqueued) & mask;
452           n_free = (complete_slot + 1 - first) & mask;
453
454           atq->n_enqueued -= n_free;
455           vlib_buffer_free_from_ring_no_next (vm, atq->buffer_indices, first,
456                                               txq->size, n_free);
457         }
458     }
459
460   n_desc = 0;
461   if (dev->va_dma)
462     n_enq = iavf_tx_prepare (vm, node, txq, buffers, n_left, &n_desc, 1);
463   else
464     n_enq = iavf_tx_prepare (vm, node, txq, buffers, n_left, &n_desc, 0);
465
466   if (n_desc)
467     {
468       if (PREDICT_TRUE (next + n_desc <= txq->size))
469         {
470           /* no wrap */
471           iavf_tx_copy_desc (atq->descs + next, atq->tmp_descs, n_desc);
472           vlib_buffer_copy_indices (atq->buffer_indices + next, atq->tmp_bufs,
473                                     n_desc);
474         }
475       else
476         {
477           /* wrap */
478           u32 n_not_wrap = txq->size - next;
479           iavf_tx_copy_desc (atq->descs + next, atq->tmp_descs, n_not_wrap);
480           iavf_tx_copy_desc (atq->descs, atq->tmp_descs + n_not_wrap,
481                              n_desc - n_not_wrap);
482           vlib_buffer_copy_indices (atq->buffer_indices + next, atq->tmp_bufs,
483                                     n_not_wrap);
484           vlib_buffer_copy_indices (atq->buffer_indices,
485                                     atq->tmp_bufs + n_not_wrap,
486                                     n_desc - n_not_wrap);
487         }
488
489       next += n_desc;
490       if ((slot = clib_ring_enq (atq->rs_slots)))
491         {
492           u16 rs_slot = slot[0] = (next - 1) & mask;
493           atq->descs[rs_slot].qword[1] |= IAVF_TXD_CMD_RS;
494         }
495
496       atq->next = next & mask;
497       __atomic_store_n (atq->qtx_tail, atq->next, __ATOMIC_RELEASE);
498       atq->n_enqueued += n_desc;
499       n_left -= n_enq;
500     }
501
502   if (n_left)
503     {
504       buffers += n_enq;
505
506       if (n_retry--)
507         goto retry;
508
509       vlib_buffer_free (vm, buffers, n_left);
510       vlib_error_count (vm, node->node_index, IAVF_TX_NODE_CTR_NO_FREE_SLOTS,
511                         n_left);
512     }
513
514   vnet_dev_tx_queue_unlock_if_needed (txq);
515
516   return frame->n_vectors - n_left;
517 }