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