devices: af-packet gso mtu
[vpp.git] / src / vnet / devices / af_packet / node.c
1 /*
2  *------------------------------------------------------------------
3  * af_packet.c - linux kernel packet interface
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <linux/if_packet.h>
21
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/interface/rx_queue_funcs.h>
27 #include <vnet/feature/feature.h>
28 #include <vnet/ethernet/packet.h>
29
30 #include <vnet/devices/af_packet/af_packet.h>
31
32 #define foreach_af_packet_input_error \
33   _(PARTIAL_PKT, "partial packet")
34
35 typedef enum
36 {
37 #define _(f,s) AF_PACKET_INPUT_ERROR_##f,
38   foreach_af_packet_input_error
39 #undef _
40     AF_PACKET_INPUT_N_ERROR,
41 } af_packet_input_error_t;
42
43 static char *af_packet_input_error_strings[] = {
44 #define _(n,s) s,
45   foreach_af_packet_input_error
46 #undef _
47 };
48
49 typedef struct
50 {
51   u32 next_index;
52   u32 hw_if_index;
53   int block;
54   struct tpacket2_hdr tph;
55 } af_packet_input_trace_t;
56
57 static u8 *
58 format_af_packet_input_trace (u8 * s, va_list * args)
59 {
60   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
61   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
62   af_packet_input_trace_t *t = va_arg (*args, af_packet_input_trace_t *);
63   u32 indent = format_get_indent (s);
64
65   s = format (s, "af_packet: hw_if_index %d next-index %d",
66               t->hw_if_index, t->next_index);
67
68   s =
69     format (s,
70             "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
71             "\n%Usec 0x%x nsec 0x%x vlan %U"
72 #ifdef TP_STATUS_VLAN_TPID_VALID
73             " vlan_tpid %u"
74 #endif
75             ,
76             format_white_space, indent + 2,
77             format_white_space, indent + 4,
78             t->tph.tp_status,
79             t->tph.tp_len,
80             t->tph.tp_snaplen,
81             t->tph.tp_mac,
82             t->tph.tp_net,
83             format_white_space, indent + 4,
84             t->tph.tp_sec,
85             t->tph.tp_nsec, format_ethernet_vlan_tci, t->tph.tp_vlan_tci
86 #ifdef TP_STATUS_VLAN_TPID_VALID
87             , t->tph.tp_vlan_tpid
88 #endif
89     );
90   return s;
91 }
92
93 always_inline void
94 buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi, u32 prev_bi)
95 {
96   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
97   vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
98   vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
99
100   /* update first buffer */
101   first_b->total_length_not_including_first_buffer += b->current_length;
102
103   /* update previous buffer */
104   prev_b->next_buffer = bi;
105   prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
106
107   /* update current buffer */
108   b->next_buffer = 0;
109 }
110
111 static_always_inline void
112 fill_gso_buffer_flags (vlib_buffer_t *b, u32 gso_size, u8 l4_hdr_sz)
113 {
114   b->flags |= VNET_BUFFER_F_GSO;
115   vnet_buffer2 (b)->gso_size = gso_size;
116   vnet_buffer2 (b)->gso_l4_hdr_sz = l4_hdr_sz;
117 }
118
119 static_always_inline void
120 mark_tcp_udp_cksum_calc (vlib_buffer_t *b, u8 *l4_hdr_sz)
121 {
122   ethernet_header_t *eth = vlib_buffer_get_current (b);
123   if (clib_net_to_host_u16 (eth->type) == ETHERNET_TYPE_IP4)
124     {
125       ip4_header_t *ip4 =
126         (vlib_buffer_get_current (b) + sizeof (ethernet_header_t));
127       b->flags |= VNET_BUFFER_F_IS_IP4;
128       if (ip4->protocol == IP_PROTOCOL_TCP)
129         {
130           b->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
131           tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b) +
132                                                 sizeof (ethernet_header_t) +
133                                                 ip4_header_bytes (ip4));
134           tcp->checksum = 0;
135           *l4_hdr_sz = tcp_header_bytes (tcp);
136         }
137       else if (ip4->protocol == IP_PROTOCOL_UDP)
138         {
139           b->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
140           udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b) +
141                                                 sizeof (ethernet_header_t) +
142                                                 ip4_header_bytes (ip4));
143           udp->checksum = 0;
144           *l4_hdr_sz = sizeof (*udp);
145         }
146       vnet_buffer (b)->l3_hdr_offset = sizeof (ethernet_header_t);
147       vnet_buffer (b)->l4_hdr_offset =
148         sizeof (ethernet_header_t) + ip4_header_bytes (ip4);
149     }
150   else if (clib_net_to_host_u16 (eth->type) == ETHERNET_TYPE_IP6)
151     {
152       ip6_header_t *ip6 =
153         (vlib_buffer_get_current (b) + sizeof (ethernet_header_t));
154       b->flags |= VNET_BUFFER_F_IS_IP6;
155       u16 ip6_hdr_len = sizeof (ip6_header_t);
156       if (ip6_ext_hdr (ip6->protocol))
157         {
158           ip6_ext_header_t *p = (void *) (ip6 + 1);
159           ip6_hdr_len += ip6_ext_header_len (p);
160           while (ip6_ext_hdr (p->next_hdr))
161             {
162               ip6_hdr_len += ip6_ext_header_len (p);
163               p = ip6_ext_next_header (p);
164             }
165         }
166       if (ip6->protocol == IP_PROTOCOL_TCP)
167         {
168           b->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
169           tcp_header_t *tcp =
170             (tcp_header_t *) (vlib_buffer_get_current (b) +
171                               sizeof (ethernet_header_t) + ip6_hdr_len);
172           tcp->checksum = 0;
173           *l4_hdr_sz = tcp_header_bytes (tcp);
174         }
175       else if (ip6->protocol == IP_PROTOCOL_UDP)
176         {
177           b->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
178           udp_header_t *udp =
179             (udp_header_t *) (vlib_buffer_get_current (b) +
180                               sizeof (ethernet_header_t) + ip6_hdr_len);
181           udp->checksum = 0;
182           *l4_hdr_sz = sizeof (*udp);
183         }
184       vnet_buffer (b)->l3_hdr_offset = sizeof (ethernet_header_t);
185       vnet_buffer (b)->l4_hdr_offset =
186         sizeof (ethernet_header_t) + ip6_hdr_len;
187     }
188 }
189
190 always_inline uword
191 af_packet_device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
192                            vlib_frame_t * frame, af_packet_if_t * apif)
193 {
194   af_packet_main_t *apm = &af_packet_main;
195   struct tpacket2_hdr *tph;
196   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
197   u32 block = 0;
198   u32 rx_frame;
199   u32 n_free_bufs;
200   u32 n_rx_packets = 0;
201   u32 n_rx_bytes = 0;
202   u32 *to_next = 0;
203   u32 block_size = apif->rx_req->tp_block_size;
204   u32 frame_size = apif->rx_req->tp_frame_size;
205   u32 frame_num = apif->rx_req->tp_frame_nr;
206   u8 *block_start = apif->rx_ring + block * block_size;
207   uword n_trace = vlib_get_trace_count (vm, node);
208   u32 thread_index = vm->thread_index;
209   u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
210   u32 min_bufs = apif->rx_req->tp_frame_size / n_buffer_bytes;
211
212   n_free_bufs = vec_len (apm->rx_buffers[thread_index]);
213   if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
214     {
215       vec_validate (apm->rx_buffers[thread_index],
216                     VLIB_FRAME_SIZE + n_free_bufs - 1);
217       n_free_bufs +=
218         vlib_buffer_alloc (vm, &apm->rx_buffers[thread_index][n_free_bufs],
219                            VLIB_FRAME_SIZE);
220       _vec_len (apm->rx_buffers[thread_index]) = n_free_bufs;
221     }
222
223   rx_frame = apif->next_rx_frame;
224   tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
225   while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs))
226     {
227       vlib_buffer_t *b0 = 0, *first_b0 = 0;
228       u32 next0 = next_index;
229
230       u32 n_left_to_next;
231       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
232       while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) &&
233              n_left_to_next)
234         {
235           u32 data_len = tph->tp_snaplen;
236           u32 offset = 0;
237           u32 bi0 = 0, first_bi0 = 0, prev_bi0;
238           u8 l4_hdr_sz = 0;
239
240           while (data_len)
241             {
242               /* grab free buffer */
243               u32 last_empty_buffer =
244                 vec_len (apm->rx_buffers[thread_index]) - 1;
245               prev_bi0 = bi0;
246               bi0 = apm->rx_buffers[thread_index][last_empty_buffer];
247               b0 = vlib_get_buffer (vm, bi0);
248               _vec_len (apm->rx_buffers[thread_index]) = last_empty_buffer;
249               n_free_bufs--;
250
251               /* copy data */
252               u32 bytes_to_copy =
253                 data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
254               u32 vlan_len = 0;
255               u32 bytes_copied = 0;
256               b0->current_data = 0;
257               /* Kernel removes VLAN headers, so reconstruct VLAN */
258               if (PREDICT_FALSE (tph->tp_status & TP_STATUS_VLAN_VALID))
259                 {
260                   if (PREDICT_TRUE (offset == 0))
261                     {
262                       clib_memcpy_fast (vlib_buffer_get_current (b0),
263                                         (u8 *) tph + tph->tp_mac,
264                                         sizeof (ethernet_header_t));
265                       ethernet_header_t *eth = vlib_buffer_get_current (b0);
266                       ethernet_vlan_header_t *vlan =
267                         (ethernet_vlan_header_t *) (eth + 1);
268                       vlan->priority_cfi_and_id =
269                         clib_host_to_net_u16 (tph->tp_vlan_tci);
270                       vlan->type = eth->type;
271                       eth->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
272                       vlan_len = sizeof (ethernet_vlan_header_t);
273                       bytes_copied = sizeof (ethernet_header_t);
274                     }
275                 }
276               clib_memcpy_fast (((u8 *) vlib_buffer_get_current (b0)) +
277                                 bytes_copied + vlan_len,
278                                 (u8 *) tph + tph->tp_mac + offset +
279                                 bytes_copied, (bytes_to_copy - bytes_copied));
280
281               /* fill buffer header */
282               b0->current_length = bytes_to_copy + vlan_len;
283
284               if (offset == 0)
285                 {
286                   b0->total_length_not_including_first_buffer = 0;
287                   b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
288                   vnet_buffer (b0)->sw_if_index[VLIB_RX] = apif->sw_if_index;
289                   vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
290                   first_bi0 = bi0;
291                   first_b0 = vlib_get_buffer (vm, first_bi0);
292                   if (tph->tp_status & TP_STATUS_CSUMNOTREADY)
293                     mark_tcp_udp_cksum_calc (first_b0, &l4_hdr_sz);
294                   if (tph->tp_snaplen > apif->host_mtu)
295                     fill_gso_buffer_flags (first_b0, apif->host_mtu,
296                                            l4_hdr_sz);
297                 }
298               else
299                 buffer_add_to_chain (vm, bi0, first_bi0, prev_bi0);
300
301               offset += bytes_to_copy;
302               data_len -= bytes_to_copy;
303             }
304           n_rx_packets++;
305           n_rx_bytes += tph->tp_snaplen;
306           to_next[0] = first_bi0;
307           to_next += 1;
308           n_left_to_next--;
309
310           /* drop partial packets */
311           if (PREDICT_FALSE (tph->tp_len != tph->tp_snaplen))
312             {
313               next0 = VNET_DEVICE_INPUT_NEXT_DROP;
314               first_b0->error =
315                 node->errors[AF_PACKET_INPUT_ERROR_PARTIAL_PKT];
316             }
317           else
318             {
319               next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
320
321               if (PREDICT_FALSE (apif->per_interface_next_index != ~0))
322                 next0 = apif->per_interface_next_index;
323
324               /* redirect if feature path enabled */
325               vnet_feature_start_device_input_x1 (apif->sw_if_index, &next0,
326                                                   first_b0);
327             }
328
329           /* trace */
330           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b0);
331           if (PREDICT_FALSE
332               (n_trace > 0 && vlib_trace_buffer (vm, node, next0, first_b0,
333                                                  /* follow_chain */ 0)))
334             {
335               af_packet_input_trace_t *tr;
336               vlib_set_trace_count (vm, node, --n_trace);
337               tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
338               tr->next_index = next0;
339               tr->hw_if_index = apif->hw_if_index;
340               clib_memcpy_fast (&tr->tph, tph, sizeof (struct tpacket2_hdr));
341             }
342
343           /* enque and take next packet */
344           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
345                                            n_left_to_next, first_bi0, next0);
346
347           /* next packet */
348           tph->tp_status = TP_STATUS_KERNEL;
349           rx_frame = (rx_frame + 1) % frame_num;
350           tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
351         }
352
353       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
354     }
355
356   apif->next_rx_frame = rx_frame;
357
358   vlib_increment_combined_counter
359     (vnet_get_main ()->interface_main.combined_sw_if_counters
360      + VNET_INTERFACE_COUNTER_RX,
361      vlib_get_thread_index (), apif->hw_if_index, n_rx_packets, n_rx_bytes);
362
363   vnet_device_increment_rx_packets (thread_index, n_rx_packets);
364   return n_rx_packets;
365 }
366
367 VLIB_NODE_FN (af_packet_input_node) (vlib_main_t * vm,
368                                      vlib_node_runtime_t * node,
369                                      vlib_frame_t * frame)
370 {
371   u32 n_rx_packets = 0;
372   af_packet_main_t *apm = &af_packet_main;
373   vnet_hw_if_rxq_poll_vector_t *pv;
374   pv = vnet_hw_if_get_rxq_poll_vector (vm, node);
375   for (int i = 0; i < vec_len (pv); i++)
376     {
377       af_packet_if_t *apif;
378       apif = vec_elt_at_index (apm->interfaces, pv[i].dev_instance);
379       if (apif->is_admin_up)
380         n_rx_packets += af_packet_device_input_fn (vm, node, frame, apif);
381     }
382
383   return n_rx_packets;
384 }
385
386 /* *INDENT-OFF* */
387 VLIB_REGISTER_NODE (af_packet_input_node) = {
388   .name = "af-packet-input",
389   .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
390   .sibling_of = "device-input",
391   .format_trace = format_af_packet_input_trace,
392   .type = VLIB_NODE_TYPE_INPUT,
393   .state = VLIB_NODE_STATE_INTERRUPT,
394   .n_errors = AF_PACKET_INPUT_N_ERROR,
395   .error_strings = af_packet_input_error_strings,
396 };
397 /* *INDENT-ON* */
398
399
400 /*
401  * fd.io coding-style-patch-verification: ON
402  *
403  * Local Variables:
404  * eval: (c-set-style "gnu")
405  * End:
406  */