vppinfra: new vectorized ip checksum functions incl. csum_and_copy
[vpp.git] / src / plugins / avf / output.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vlib/pci/pci.h>
21 #include <vppinfra/ring.h>
22 #include <vppinfra/vector/ip_csum.h>
23
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/ip/ip4_packet.h>
26 #include <vnet/ip/ip6_packet.h>
27 #include <vnet/udp/udp_packet.h>
28 #include <vnet/tcp/tcp_packet.h>
29
30 #include <vnet/devices/devices.h>
31
32 #include <avf/avf.h>
33
34 static_always_inline u8
35 avf_tx_desc_get_dtyp (avf_tx_desc_t * d)
36 {
37   return d->qword[1] & 0x0f;
38 }
39
40 struct avf_ip4_psh
41 {
42   u32 src;
43   u32 dst;
44   u8 zero;
45   u8 proto;
46   u16 l4len;
47 };
48
49 struct avf_ip6_psh
50 {
51   ip6_address_t src;
52   ip6_address_t dst;
53   u32 l4len;
54   u32 proto;
55 };
56
57 static_always_inline u64
58 avf_tx_prepare_cksum (vlib_buffer_t * b, u8 is_tso)
59 {
60   u64 flags = 0;
61   if (!is_tso && !(b->flags & VNET_BUFFER_F_OFFLOAD))
62     return 0;
63
64   vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags;
65   u32 is_tcp = is_tso || oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM;
66   u32 is_udp = !is_tso && oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM;
67
68   if (!is_tcp && !is_udp)
69     return 0;
70
71   u32 is_ip4 = b->flags & VNET_BUFFER_F_IS_IP4;
72   u32 is_ip6 = b->flags & VNET_BUFFER_F_IS_IP6;
73
74   ASSERT (!(is_tcp && is_udp));
75   ASSERT (is_ip4 || is_ip6);
76   i16 l2_hdr_offset = b->current_data;
77   i16 l3_hdr_offset = vnet_buffer (b)->l3_hdr_offset;
78   i16 l4_hdr_offset = vnet_buffer (b)->l4_hdr_offset;
79   u16 l2_len = l3_hdr_offset - l2_hdr_offset;
80   u16 l3_len = l4_hdr_offset - l3_hdr_offset;
81   ip4_header_t *ip4 = (void *) (b->data + l3_hdr_offset);
82   ip6_header_t *ip6 = (void *) (b->data + l3_hdr_offset);
83   tcp_header_t *tcp = (void *) (b->data + l4_hdr_offset);
84   udp_header_t *udp = (void *) (b->data + l4_hdr_offset);
85   u16 l4_len = is_tcp ? tcp_header_bytes (tcp) : sizeof (udp_header_t);
86   u16 sum = 0;
87
88   flags |= AVF_TXD_OFFSET_MACLEN (l2_len) |
89     AVF_TXD_OFFSET_IPLEN (l3_len) | AVF_TXD_OFFSET_L4LEN (l4_len);
90   flags |= is_ip4 ? AVF_TXD_CMD_IIPT_IPV4 : AVF_TXD_CMD_IIPT_IPV6;
91   flags |= is_tcp ? AVF_TXD_CMD_L4T_TCP : AVF_TXD_CMD_L4T_UDP;
92
93   if (is_ip4)
94     ip4->checksum = 0;
95
96   if (is_tso)
97     {
98       if (is_ip4)
99         ip4->length = 0;
100       else
101         ip6->payload_length = 0;
102     }
103
104       if (is_ip4)
105         {
106           struct avf_ip4_psh psh = { 0 };
107           psh.src = ip4->src_address.as_u32;
108           psh.dst = ip4->dst_address.as_u32;
109           psh.proto = ip4->protocol;
110           psh.l4len =
111             is_tso ? 0 :
112             clib_host_to_net_u16 (clib_net_to_host_u16 (ip4->length) -
113                                   (l4_hdr_offset - l3_hdr_offset));
114           sum = ~clib_ip_csum ((u8 *) &psh, sizeof (psh));
115         }
116       else
117         {
118           struct avf_ip6_psh psh = { 0 };
119           psh.src = ip6->src_address;
120           psh.dst = ip6->dst_address;
121           psh.proto = clib_host_to_net_u32 ((u32) ip6->protocol);
122           psh.l4len = is_tso ? 0 : ip6->payload_length;
123           sum = ~clib_ip_csum ((u8 *) &psh, sizeof (psh));
124         }
125
126   /* ip_csum does a byte swap for some reason... */
127   sum = clib_net_to_host_u16 (sum);
128   if (is_tcp)
129     tcp->checksum = sum;
130   else
131     udp->checksum = sum;
132   return flags;
133 }
134
135 static_always_inline u32
136 avf_tx_fill_ctx_desc (vlib_main_t *vm, avf_txq_t *txq, avf_tx_desc_t *d,
137                       vlib_buffer_t *b)
138 {
139   vlib_buffer_t *ctx_ph;
140   u32 *bi = txq->ph_bufs;
141
142 next:
143   ctx_ph = vlib_get_buffer (vm, bi[0]);
144   if (PREDICT_FALSE (ctx_ph->ref_count == 255))
145     {
146       bi++;
147       goto next;
148     }
149
150   /* Acquire a reference on the placeholder buffer */
151   ctx_ph->ref_count++;
152
153   u16 l234hdr_sz = vnet_buffer (b)->l4_hdr_offset - b->current_data +
154                    vnet_buffer2 (b)->gso_l4_hdr_sz;
155   u16 tlen = vlib_buffer_length_in_chain (vm, b) - l234hdr_sz;
156   d[0].qword[0] = 0;
157   d[0].qword[1] = AVF_TXD_DTYP_CTX | AVF_TXD_CTX_CMD_TSO
158     | AVF_TXD_CTX_SEG_MSS (vnet_buffer2 (b)->gso_size) |
159     AVF_TXD_CTX_SEG_TLEN (tlen);
160   return bi[0];
161 }
162
163 static_always_inline void
164 avf_tx_copy_desc (avf_tx_desc_t *d, avf_tx_desc_t *s, u32 n_descs)
165 {
166 #if defined CLIB_HAVE_VEC512
167   while (n_descs >= 8)
168     {
169       u64x8u *dv = (u64x8u *) d;
170       u64x8u *sv = (u64x8u *) s;
171
172       dv[0] = sv[0];
173       dv[1] = sv[1];
174
175       /* next */
176       d += 8;
177       s += 8;
178       n_descs -= 8;
179     }
180 #elif defined CLIB_HAVE_VEC256
181   while (n_descs >= 4)
182     {
183       u64x4u *dv = (u64x4u *) d;
184       u64x4u *sv = (u64x4u *) s;
185
186       dv[0] = sv[0];
187       dv[1] = sv[1];
188
189       /* next */
190       d += 4;
191       s += 4;
192       n_descs -= 4;
193     }
194 #elif defined CLIB_HAVE_VEC128
195   while (n_descs >= 2)
196     {
197       u64x2u *dv = (u64x2u *) d;
198       u64x2u *sv = (u64x2u *) s;
199
200       dv[0] = sv[0];
201       dv[1] = sv[1];
202
203       /* next */
204       d += 2;
205       s += 2;
206       n_descs -= 2;
207     }
208 #endif
209   while (n_descs)
210     {
211       d[0].qword[0] = s[0].qword[0];
212       d[0].qword[1] = s[0].qword[1];
213       d++;
214       s++;
215       n_descs--;
216     }
217 }
218
219 static_always_inline void
220 avf_tx_fill_data_desc (vlib_main_t *vm, avf_tx_desc_t *d, vlib_buffer_t *b,
221                        u64 cmd, int use_va_dma)
222 {
223   if (use_va_dma)
224     d->qword[0] = vlib_buffer_get_current_va (b);
225   else
226     d->qword[0] = vlib_buffer_get_current_pa (vm, b);
227   d->qword[1] = (((u64) b->current_length) << 34 | cmd | AVF_TXD_CMD_RSV);
228 }
229 static_always_inline u16
230 avf_tx_prepare (vlib_main_t *vm, vlib_node_runtime_t *node, avf_txq_t *txq,
231                 u32 *buffers, u32 n_packets, u16 *n_enq_descs, int use_va_dma)
232 {
233   const u64 cmd_eop = AVF_TXD_CMD_EOP;
234   u16 n_free_desc, n_desc_left, n_packets_left = n_packets;
235   vlib_buffer_t *b[4];
236   avf_tx_desc_t *d = txq->tmp_descs;
237   u32 *tb = txq->tmp_bufs;
238
239   n_free_desc = n_desc_left = txq->size - txq->n_enqueued - 8;
240
241   if (n_desc_left == 0)
242     return 0;
243
244   while (n_packets_left && n_desc_left)
245     {
246       u32 flags, or_flags;
247
248       if (n_packets_left < 8 || n_desc_left < 4)
249         goto one_by_one;
250
251       vlib_prefetch_buffer_with_index (vm, buffers[4], LOAD);
252       vlib_prefetch_buffer_with_index (vm, buffers[5], LOAD);
253       vlib_prefetch_buffer_with_index (vm, buffers[6], LOAD);
254       vlib_prefetch_buffer_with_index (vm, buffers[7], LOAD);
255
256       b[0] = vlib_get_buffer (vm, buffers[0]);
257       b[1] = vlib_get_buffer (vm, buffers[1]);
258       b[2] = vlib_get_buffer (vm, buffers[2]);
259       b[3] = vlib_get_buffer (vm, buffers[3]);
260
261       or_flags = b[0]->flags | b[1]->flags | b[2]->flags | b[3]->flags;
262
263       if (PREDICT_FALSE (or_flags &
264                          (VLIB_BUFFER_NEXT_PRESENT | VNET_BUFFER_F_OFFLOAD |
265                           VNET_BUFFER_F_GSO)))
266         goto one_by_one;
267
268       vlib_buffer_copy_indices (tb, buffers, 4);
269
270       avf_tx_fill_data_desc (vm, d + 0, b[0], cmd_eop, use_va_dma);
271       avf_tx_fill_data_desc (vm, d + 1, b[1], cmd_eop, use_va_dma);
272       avf_tx_fill_data_desc (vm, d + 2, b[2], cmd_eop, use_va_dma);
273       avf_tx_fill_data_desc (vm, d + 3, b[3], cmd_eop, use_va_dma);
274
275       buffers += 4;
276       n_packets_left -= 4;
277       n_desc_left -= 4;
278       d += 4;
279       tb += 4;
280       continue;
281
282     one_by_one:
283       tb[0] = buffers[0];
284       b[0] = vlib_get_buffer (vm, buffers[0]);
285       flags = b[0]->flags;
286
287       /* No chained buffers or TSO case */
288       if (PREDICT_TRUE (
289             (flags & (VLIB_BUFFER_NEXT_PRESENT | VNET_BUFFER_F_GSO)) == 0))
290         {
291           u64 cmd = cmd_eop;
292
293           if (PREDICT_FALSE (flags & VNET_BUFFER_F_OFFLOAD))
294             cmd |= avf_tx_prepare_cksum (b[0], 0 /* is_tso */);
295
296           avf_tx_fill_data_desc (vm, d, b[0], cmd, use_va_dma);
297         }
298       else
299         {
300           u16 n_desc_needed = 1;
301           u64 cmd = 0;
302
303           if (flags & VLIB_BUFFER_NEXT_PRESENT)
304             {
305               vlib_buffer_t *next = vlib_get_buffer (vm, b[0]->next_buffer);
306               n_desc_needed = 2;
307               while (next->flags & VLIB_BUFFER_NEXT_PRESENT)
308                 {
309                   next = vlib_get_buffer (vm, next->next_buffer);
310                   n_desc_needed++;
311                 }
312             }
313
314           if (flags & VNET_BUFFER_F_GSO)
315             {
316               n_desc_needed++;
317             }
318           else if (PREDICT_FALSE (n_desc_needed > 8))
319             {
320               vlib_buffer_free_one (vm, buffers[0]);
321               vlib_error_count (vm, node->node_index,
322                                 AVF_TX_ERROR_SEGMENT_SIZE_EXCEEDED, 1);
323               n_packets_left -= 1;
324               buffers += 1;
325               continue;
326             }
327
328           if (PREDICT_FALSE (n_desc_left < n_desc_needed))
329             break;
330
331           if (flags & VNET_BUFFER_F_GSO)
332             {
333               /* Enqueue a context descriptor */
334               tb[1] = tb[0];
335               tb[0] = avf_tx_fill_ctx_desc (vm, txq, d, b[0]);
336               n_desc_left -= 1;
337               d += 1;
338               tb += 1;
339               cmd = avf_tx_prepare_cksum (b[0], 1 /* is_tso */);
340             }
341           else if (flags & VNET_BUFFER_F_OFFLOAD)
342             {
343               cmd = avf_tx_prepare_cksum (b[0], 0 /* is_tso */);
344             }
345
346           /* Deal with chain buffer if present */
347           while (b[0]->flags & VLIB_BUFFER_NEXT_PRESENT)
348             {
349               avf_tx_fill_data_desc (vm, d, b[0], cmd, use_va_dma);
350
351               n_desc_left -= 1;
352               d += 1;
353               tb += 1;
354
355               tb[0] = b[0]->next_buffer;
356               b[0] = vlib_get_buffer (vm, b[0]->next_buffer);
357             }
358
359           avf_tx_fill_data_desc (vm, d, b[0], cmd_eop | cmd, use_va_dma);
360         }
361
362       buffers += 1;
363       n_packets_left -= 1;
364       n_desc_left -= 1;
365       d += 1;
366       tb += 1;
367     }
368
369   *n_enq_descs = n_free_desc - n_desc_left;
370   return n_packets - n_packets_left;
371 }
372
373 VNET_DEVICE_CLASS_TX_FN (avf_device_class) (vlib_main_t * vm,
374                                             vlib_node_runtime_t * node,
375                                             vlib_frame_t * frame)
376 {
377   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
378   avf_device_t *ad = avf_get_device (rd->dev_instance);
379   vnet_hw_if_tx_frame_t *tf = vlib_frame_scalar_args (frame);
380   u8 qid = tf->queue_id;
381   avf_txq_t *txq = vec_elt_at_index (ad->txqs, qid);
382   u16 next;
383   u16 mask = txq->size - 1;
384   u32 *buffers = vlib_frame_vector_args (frame);
385   u16 n_enq, n_left, n_desc, *slot;
386   u16 n_retry = 2;
387
388   if (tf->shared_queue)
389     clib_spinlock_lock (&txq->lock);
390
391   n_left = frame->n_vectors;
392
393 retry:
394   next = txq->next;
395   /* release consumed bufs */
396   if (txq->n_enqueued)
397     {
398       i32 complete_slot = -1;
399       while (1)
400         {
401           u16 *slot = clib_ring_get_first (txq->rs_slots);
402
403           if (slot == 0)
404             break;
405
406           if (avf_tx_desc_get_dtyp (txq->descs + slot[0]) != 0x0F)
407             break;
408
409           complete_slot = slot[0];
410
411           clib_ring_deq (txq->rs_slots);
412         }
413
414       if (complete_slot >= 0)
415         {
416           u16 first, mask, n_free;
417           mask = txq->size - 1;
418           first = (txq->next - txq->n_enqueued) & mask;
419           n_free = (complete_slot + 1 - first) & mask;
420
421           txq->n_enqueued -= n_free;
422           vlib_buffer_free_from_ring_no_next (vm, txq->bufs, first, txq->size,
423                                               n_free);
424         }
425     }
426
427   n_desc = 0;
428   if (ad->flags & AVF_DEVICE_F_VA_DMA)
429     n_enq = avf_tx_prepare (vm, node, txq, buffers, n_left, &n_desc, 1);
430   else
431     n_enq = avf_tx_prepare (vm, node, txq, buffers, n_left, &n_desc, 0);
432
433   if (n_desc)
434     {
435       if (PREDICT_TRUE (next + n_desc <= txq->size))
436         {
437           /* no wrap */
438           avf_tx_copy_desc (txq->descs + next, txq->tmp_descs, n_desc);
439           vlib_buffer_copy_indices (txq->bufs + next, txq->tmp_bufs, n_desc);
440         }
441       else
442         {
443           /* wrap */
444           u32 n_not_wrap = txq->size - next;
445           avf_tx_copy_desc (txq->descs + next, txq->tmp_descs, n_not_wrap);
446           avf_tx_copy_desc (txq->descs, txq->tmp_descs + n_not_wrap,
447                             n_desc - n_not_wrap);
448           vlib_buffer_copy_indices (txq->bufs + next, txq->tmp_bufs,
449                                     n_not_wrap);
450           vlib_buffer_copy_indices (txq->bufs, txq->tmp_bufs + n_not_wrap,
451                                     n_desc - n_not_wrap);
452         }
453
454       next += n_desc;
455       if ((slot = clib_ring_enq (txq->rs_slots)))
456         {
457           u16 rs_slot = slot[0] = (next - 1) & mask;
458           txq->descs[rs_slot].qword[1] |= AVF_TXD_CMD_RS;
459         }
460
461       txq->next = next & mask;
462       avf_tail_write (txq->qtx_tail, txq->next);
463       txq->n_enqueued += n_desc;
464       n_left -= n_enq;
465     }
466
467   if (n_left)
468     {
469       buffers += n_enq;
470
471       if (n_retry--)
472         goto retry;
473
474       vlib_buffer_free (vm, buffers, n_left);
475       vlib_error_count (vm, node->node_index,
476                         AVF_TX_ERROR_NO_FREE_SLOTS, n_left);
477     }
478
479   if (tf->shared_queue)
480     clib_spinlock_unlock (&txq->lock);
481
482   return frame->n_vectors - n_left;
483 }
484
485 /*
486  * fd.io coding-style-patch-verification: ON
487  *
488  * Local Variables:
489  * eval: (c-set-style "gnu")
490  * End:
491  */