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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
18 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vlib/pci/pci.h>
21 #include <vppinfra/ring.h>
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>
29 #include <vnet/devices/devices.h>
33 static_always_inline u8
34 avf_tx_desc_get_dtyp (avf_tx_desc_t * d)
36 return d->qword[1] & 0x0f;
56 static_always_inline u64
57 avf_tx_prepare_cksum (vlib_buffer_t * b, u8 is_tso)
60 if (!is_tso && !(b->flags & VNET_BUFFER_F_OFFLOAD))
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);
80 is_tcp ? tcp_header_bytes (tcp) : is_udp ? sizeof (udp_header_t) : 0;
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;
96 ip6->payload_length = 0;
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;
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));
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));
123 /* ip_csum does a byte swap for some reason... */
124 sum = clib_net_to_host_u16 (sum);
132 static_always_inline u32
133 avf_tx_fill_ctx_desc (vlib_main_t *vm, avf_txq_t *txq, avf_tx_desc_t *d,
136 vlib_buffer_t *ctx_ph;
137 u32 *bi = txq->ph_bufs;
140 ctx_ph = vlib_get_buffer (vm, bi[0]);
141 if (PREDICT_FALSE (ctx_ph->ref_count == 255))
147 /* Acquire a reference on the placeholder buffer */
150 u16 l234hdr_sz = vnet_buffer (b)->l4_hdr_offset - b->current_data +
151 vnet_buffer2 (b)->gso_l4_hdr_sz;
152 u16 tlen = vlib_buffer_length_in_chain (vm, b) - l234hdr_sz;
154 d[0].qword[1] = AVF_TXD_DTYP_CTX | AVF_TXD_CTX_CMD_TSO
155 | AVF_TXD_CTX_SEG_MSS (vnet_buffer2 (b)->gso_size) |
156 AVF_TXD_CTX_SEG_TLEN (tlen);
160 static_always_inline void
161 avf_tx_copy_desc (avf_tx_desc_t *d, avf_tx_desc_t *s, u32 n_descs)
163 #if defined CLIB_HAVE_VEC512
166 u64x8u *dv = (u64x8u *) d;
167 u64x8u *sv = (u64x8u *) s;
177 #elif defined CLIB_HAVE_VEC256
180 u64x4u *dv = (u64x4u *) d;
181 u64x4u *sv = (u64x4u *) s;
191 #elif defined CLIB_HAVE_VEC128
194 u64x2u *dv = (u64x2u *) d;
195 u64x2u *sv = (u64x2u *) s;
208 d[0].qword[0] = s[0].qword[0];
209 d[0].qword[1] = s[0].qword[1];
216 static_always_inline void
217 avf_tx_fill_data_desc (vlib_main_t *vm, avf_tx_desc_t *d, vlib_buffer_t *b,
218 u64 cmd, int use_va_dma)
221 d->qword[0] = vlib_buffer_get_current_va (b);
223 d->qword[0] = vlib_buffer_get_current_pa (vm, b);
224 d->qword[1] = (((u64) b->current_length) << 34 | cmd | AVF_TXD_CMD_RSV);
226 static_always_inline u16
227 avf_tx_prepare (vlib_main_t *vm, vlib_node_runtime_t *node, avf_txq_t *txq,
228 u32 *buffers, u32 n_packets, u16 *n_enq_descs, int use_va_dma)
230 const u64 cmd_eop = AVF_TXD_CMD_EOP;
231 u16 n_free_desc, n_desc_left, n_packets_left = n_packets;
233 avf_tx_desc_t *d = txq->tmp_descs;
234 u32 *tb = txq->tmp_bufs;
236 n_free_desc = n_desc_left = txq->size - txq->n_enqueued - 8;
238 if (n_desc_left == 0)
241 while (n_packets_left && n_desc_left)
245 if (n_packets_left < 8 || n_desc_left < 4)
248 vlib_prefetch_buffer_with_index (vm, buffers[4], LOAD);
249 vlib_prefetch_buffer_with_index (vm, buffers[5], LOAD);
250 vlib_prefetch_buffer_with_index (vm, buffers[6], LOAD);
251 vlib_prefetch_buffer_with_index (vm, buffers[7], LOAD);
253 b[0] = vlib_get_buffer (vm, buffers[0]);
254 b[1] = vlib_get_buffer (vm, buffers[1]);
255 b[2] = vlib_get_buffer (vm, buffers[2]);
256 b[3] = vlib_get_buffer (vm, buffers[3]);
258 or_flags = b[0]->flags | b[1]->flags | b[2]->flags | b[3]->flags;
260 if (PREDICT_FALSE (or_flags &
261 (VLIB_BUFFER_NEXT_PRESENT | VNET_BUFFER_F_OFFLOAD |
265 vlib_buffer_copy_indices (tb, buffers, 4);
267 avf_tx_fill_data_desc (vm, d + 0, b[0], cmd_eop, use_va_dma);
268 avf_tx_fill_data_desc (vm, d + 1, b[1], cmd_eop, use_va_dma);
269 avf_tx_fill_data_desc (vm, d + 2, b[2], cmd_eop, use_va_dma);
270 avf_tx_fill_data_desc (vm, d + 3, b[3], cmd_eop, use_va_dma);
281 b[0] = vlib_get_buffer (vm, buffers[0]);
284 /* No chained buffers or TSO case */
286 (flags & (VLIB_BUFFER_NEXT_PRESENT | VNET_BUFFER_F_GSO)) == 0))
290 if (PREDICT_FALSE (flags & VNET_BUFFER_F_OFFLOAD))
291 cmd |= avf_tx_prepare_cksum (b[0], 0 /* is_tso */);
293 avf_tx_fill_data_desc (vm, d, b[0], cmd, use_va_dma);
297 u16 n_desc_needed = 1;
300 if (flags & VLIB_BUFFER_NEXT_PRESENT)
302 vlib_buffer_t *next = vlib_get_buffer (vm, b[0]->next_buffer);
304 while (next->flags & VLIB_BUFFER_NEXT_PRESENT)
306 next = vlib_get_buffer (vm, next->next_buffer);
311 if (flags & VNET_BUFFER_F_GSO)
315 else if (PREDICT_FALSE (n_desc_needed > 8))
317 vlib_buffer_free_one (vm, buffers[0]);
318 vlib_error_count (vm, node->node_index,
319 AVF_TX_ERROR_SEGMENT_SIZE_EXCEEDED, 1);
325 if (PREDICT_FALSE (n_desc_left < n_desc_needed))
328 if (flags & VNET_BUFFER_F_GSO)
330 /* Enqueue a context descriptor */
332 tb[0] = avf_tx_fill_ctx_desc (vm, txq, d, b[0]);
336 cmd = avf_tx_prepare_cksum (b[0], 1 /* is_tso */);
338 else if (flags & VNET_BUFFER_F_OFFLOAD)
340 cmd = avf_tx_prepare_cksum (b[0], 0 /* is_tso */);
343 /* Deal with chain buffer if present */
344 while (b[0]->flags & VLIB_BUFFER_NEXT_PRESENT)
346 avf_tx_fill_data_desc (vm, d, b[0], cmd, use_va_dma);
352 tb[0] = b[0]->next_buffer;
353 b[0] = vlib_get_buffer (vm, b[0]->next_buffer);
356 avf_tx_fill_data_desc (vm, d, b[0], cmd_eop | cmd, use_va_dma);
366 *n_enq_descs = n_free_desc - n_desc_left;
367 return n_packets - n_packets_left;
370 VNET_DEVICE_CLASS_TX_FN (avf_device_class) (vlib_main_t * vm,
371 vlib_node_runtime_t * node,
372 vlib_frame_t * frame)
374 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
375 avf_device_t *ad = avf_get_device (rd->dev_instance);
376 u32 thread_index = vm->thread_index;
377 u8 qid = thread_index;
378 avf_txq_t *txq = vec_elt_at_index (ad->txqs, qid % ad->num_queue_pairs);
380 u16 mask = txq->size - 1;
381 u32 *buffers = vlib_frame_vector_args (frame);
382 u16 n_enq, n_left, n_desc, *slot;
385 clib_spinlock_lock_if_init (&txq->lock);
387 n_left = frame->n_vectors;
391 /* release consumed bufs */
394 i32 complete_slot = -1;
397 u16 *slot = clib_ring_get_first (txq->rs_slots);
402 if (avf_tx_desc_get_dtyp (txq->descs + slot[0]) != 0x0F)
405 complete_slot = slot[0];
407 clib_ring_deq (txq->rs_slots);
410 if (complete_slot >= 0)
412 u16 first, mask, n_free;
413 mask = txq->size - 1;
414 first = (txq->next - txq->n_enqueued) & mask;
415 n_free = (complete_slot + 1 - first) & mask;
417 txq->n_enqueued -= n_free;
418 vlib_buffer_free_from_ring_no_next (vm, txq->bufs, first, txq->size,
424 if (ad->flags & AVF_DEVICE_F_VA_DMA)
425 n_enq = avf_tx_prepare (vm, node, txq, buffers, n_left, &n_desc, 1);
427 n_enq = avf_tx_prepare (vm, node, txq, buffers, n_left, &n_desc, 0);
431 if (PREDICT_TRUE (next + n_desc <= txq->size))
434 avf_tx_copy_desc (txq->descs + next, txq->tmp_descs, n_desc);
435 vlib_buffer_copy_indices (txq->bufs + next, txq->tmp_bufs, n_desc);
440 u32 n_not_wrap = txq->size - next;
441 avf_tx_copy_desc (txq->descs + next, txq->tmp_descs, n_not_wrap);
442 avf_tx_copy_desc (txq->descs, txq->tmp_descs + n_not_wrap,
443 n_desc - n_not_wrap);
444 vlib_buffer_copy_indices (txq->bufs + next, txq->tmp_bufs,
446 vlib_buffer_copy_indices (txq->bufs, txq->tmp_bufs + n_not_wrap,
447 n_desc - n_not_wrap);
451 if ((slot = clib_ring_enq (txq->rs_slots)))
453 u16 rs_slot = slot[0] = (next - 1) & mask;
454 txq->descs[rs_slot].qword[1] |= AVF_TXD_CMD_RS;
457 txq->next = next & mask;
459 avf_tail_write (txq->qtx_tail, txq->next);
460 txq->n_enqueued += n_desc;
471 vlib_buffer_free (vm, buffers, n_left);
472 vlib_error_count (vm, node->node_index,
473 AVF_TX_ERROR_NO_FREE_SLOTS, n_left);
476 clib_spinlock_unlock_if_init (&txq->lock);
478 return frame->n_vectors - n_left;
482 * fd.io coding-style-patch-verification: ON
485 * eval: (c-set-style "gnu")