feature: add new feature handling code and device-input features
[vpp.git] / vnet / 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/feature/feature.h>
27
28 #include <vnet/devices/af_packet/af_packet.h>
29
30 #define foreach_af_packet_input_error
31
32 typedef enum
33 {
34 #define _(f,s) AF_PACKET_INPUT_ERROR_##f,
35   foreach_af_packet_input_error
36 #undef _
37     AF_PACKET_INPUT_N_ERROR,
38 } af_packet_input_error_t;
39
40 static char *af_packet_input_error_strings[] = {
41 #define _(n,s) s,
42   foreach_af_packet_input_error
43 #undef _
44 };
45
46 enum
47 {
48   AF_PACKET_INPUT_NEXT_DROP,
49   AF_PACKET_INPUT_NEXT_ETHERNET_INPUT,
50   AF_PACKET_INPUT_N_NEXT,
51 };
52
53 typedef struct
54 {
55   u32 next_index;
56   u32 hw_if_index;
57   int block;
58   struct tpacket2_hdr tph;
59 } af_packet_input_trace_t;
60
61 static u8 *
62 format_af_packet_input_trace (u8 * s, va_list * args)
63 {
64   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
65   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
66   af_packet_input_trace_t *t = va_arg (*args, af_packet_input_trace_t *);
67   uword indent = format_get_indent (s);
68
69   s = format (s, "af_packet: hw_if_index %d next-index %d",
70               t->hw_if_index, t->next_index);
71
72   s =
73     format (s,
74             "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
75             "\n%Usec 0x%x nsec 0x%x vlan %U"
76 #ifdef TP_STATUS_VLAN_TPID_VALID
77             " vlan_tpid %u"
78 #endif
79             ,
80             format_white_space, indent + 2,
81             format_white_space, indent + 4,
82             t->tph.tp_status,
83             t->tph.tp_len,
84             t->tph.tp_snaplen,
85             t->tph.tp_mac,
86             t->tph.tp_net,
87             format_white_space, indent + 4,
88             t->tph.tp_sec,
89             t->tph.tp_nsec, format_ethernet_vlan_tci, t->tph.tp_vlan_tci
90 #ifdef TP_STATUS_VLAN_TPID_VALID
91             , t->tph.tp_vlan_tpid
92 #endif
93     );
94   return s;
95 }
96
97 always_inline void
98 buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi, u32 prev_bi)
99 {
100   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
101   vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
102   vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
103
104   /* update first buffer */
105   first_b->total_length_not_including_first_buffer += b->current_length;
106
107   /* update previous buffer */
108   prev_b->next_buffer = bi;
109   prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
110
111   /* update current buffer */
112   b->next_buffer = 0;
113
114 #if DPDK > 0
115   struct rte_mbuf *mbuf = rte_mbuf_from_vlib_buffer (b);
116   struct rte_mbuf *first_mbuf = rte_mbuf_from_vlib_buffer (first_b);
117   struct rte_mbuf *prev_mbuf = rte_mbuf_from_vlib_buffer (prev_b);
118   first_mbuf->nb_segs++;
119   prev_mbuf->next = mbuf;
120   mbuf->data_len = b->current_length;
121   mbuf->data_off = RTE_PKTMBUF_HEADROOM + b->current_data;
122   mbuf->next = 0;
123 #endif
124 }
125
126 always_inline uword
127 af_packet_device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
128                            vlib_frame_t * frame, u32 device_idx)
129 {
130   af_packet_main_t *apm = &af_packet_main;
131   af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, device_idx);
132   struct tpacket2_hdr *tph;
133   u32 next_index = AF_PACKET_INPUT_NEXT_ETHERNET_INPUT;
134   u32 block = 0;
135   u32 rx_frame;
136   u32 n_free_bufs;
137   u32 n_rx_packets = 0;
138   u32 n_rx_bytes = 0;
139   u32 *to_next = 0;
140   u32 block_size = apif->rx_req->tp_block_size;
141   u32 frame_size = apif->rx_req->tp_frame_size;
142   u32 frame_num = apif->rx_req->tp_frame_nr;
143   u8 *block_start = apif->rx_ring + block * block_size;
144   uword n_trace = vlib_get_trace_count (vm, node);
145   u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm,
146                                                           VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
147   u32 min_bufs = apif->rx_req->tp_frame_size / n_buffer_bytes;
148   int cpu_index = node->cpu_index;
149
150   if (apif->per_interface_next_index != ~0)
151     next_index = apif->per_interface_next_index;
152
153   n_free_bufs = vec_len (apm->rx_buffers[cpu_index]);
154   if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
155     {
156       vec_validate (apm->rx_buffers[cpu_index],
157                     VLIB_FRAME_SIZE + n_free_bufs - 1);
158       n_free_bufs +=
159         vlib_buffer_alloc (vm, &apm->rx_buffers[cpu_index][n_free_bufs],
160                            VLIB_FRAME_SIZE);
161       _vec_len (apm->rx_buffers[cpu_index]) = n_free_bufs;
162     }
163
164   rx_frame = apif->next_rx_frame;
165   tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
166   while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs))
167     {
168       vlib_buffer_t *b0 = 0, *first_b0 = 0;
169       u32 next0 = next_index;
170
171       u32 n_left_to_next;
172       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
173       while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) &&
174              n_left_to_next)
175         {
176           u32 data_len = tph->tp_snaplen;
177           u32 offset = 0;
178           u32 bi0 = 0, first_bi0 = 0, prev_bi0;
179
180           while (data_len)
181             {
182               /* grab free buffer */
183               u32 last_empty_buffer =
184                 vec_len (apm->rx_buffers[cpu_index]) - 1;
185               prev_bi0 = bi0;
186               bi0 = apm->rx_buffers[cpu_index][last_empty_buffer];
187               b0 = vlib_get_buffer (vm, bi0);
188               _vec_len (apm->rx_buffers[cpu_index]) = last_empty_buffer;
189               n_free_bufs--;
190
191               /* copy data */
192               u32 bytes_to_copy =
193                 data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
194               b0->current_data = 0;
195               clib_memcpy (vlib_buffer_get_current (b0),
196                            (u8 *) tph + tph->tp_mac + offset, bytes_to_copy);
197
198               /* fill buffer header */
199               b0->current_length = bytes_to_copy;
200
201               if (offset == 0)
202                 {
203 #if DPDK > 0
204                   struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer (b0);
205                   rte_pktmbuf_data_len (mb) = b0->current_length;
206                   rte_pktmbuf_pkt_len (mb) = b0->current_length;
207 #endif
208                   b0->total_length_not_including_first_buffer = 0;
209                   b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
210                   vnet_buffer (b0)->sw_if_index[VLIB_RX] = apif->sw_if_index;
211                   vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
212                   first_bi0 = bi0;
213                   first_b0 = vlib_get_buffer (vm, first_bi0);
214                 }
215               else
216                 buffer_add_to_chain (vm, bi0, first_bi0, prev_bi0);
217
218               offset += bytes_to_copy;
219               data_len -= bytes_to_copy;
220             }
221           n_rx_packets++;
222           n_rx_bytes += tph->tp_snaplen;
223           to_next[0] = first_bi0;
224           to_next += 1;
225           n_left_to_next--;
226
227           /* trace */
228           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b0);
229           if (PREDICT_FALSE (n_trace > 0))
230             {
231               af_packet_input_trace_t *tr;
232               vlib_trace_buffer (vm, node, next0, first_b0,     /* follow_chain */
233                                  0);
234               vlib_set_trace_count (vm, node, --n_trace);
235               tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
236               tr->next_index = next0;
237               tr->hw_if_index = apif->hw_if_index;
238               clib_memcpy (&tr->tph, tph, sizeof (struct tpacket2_hdr));
239             }
240
241           /* redirect if feature path enabled */
242           vnet_feature_device_input_redirect_x1 (node, apif->sw_if_index,
243                                                  &next0, b0, 0);
244
245           /* enque and take next packet */
246           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
247                                            n_left_to_next, first_bi0, next0);
248
249           /* next packet */
250           tph->tp_status = TP_STATUS_KERNEL;
251           rx_frame = (rx_frame + 1) % frame_num;
252           tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
253         }
254
255       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
256     }
257
258   apif->next_rx_frame = rx_frame;
259
260   vlib_increment_combined_counter
261     (vnet_get_main ()->interface_main.combined_sw_if_counters
262      + VNET_INTERFACE_COUNTER_RX,
263      os_get_cpu_number (), apif->hw_if_index, n_rx_packets, n_rx_bytes);
264
265   return n_rx_packets;
266 }
267
268 static uword
269 af_packet_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
270                     vlib_frame_t * frame)
271 {
272   int i;
273   u32 n_rx_packets = 0;
274
275   af_packet_main_t *apm = &af_packet_main;
276
277   /* *INDENT-OFF* */
278   clib_bitmap_foreach (i, apm->pending_input_bitmap,
279     ({
280       clib_bitmap_set (apm->pending_input_bitmap, i, 0);
281       n_rx_packets += af_packet_device_input_fn(vm, node, frame, i);
282     }));
283   /* *INDENT-ON* */
284
285   return n_rx_packets;
286 }
287
288 /* *INDENT-OFF* */
289 VLIB_REGISTER_NODE (af_packet_input_node) = {
290   .function = af_packet_input_fn,
291   .name = "af-packet-input",
292   .format_trace = format_af_packet_input_trace,
293   .type = VLIB_NODE_TYPE_INPUT,
294   .state = VLIB_NODE_STATE_INTERRUPT,
295   .n_errors = AF_PACKET_INPUT_N_ERROR,
296   .error_strings = af_packet_input_error_strings,
297
298   .n_next_nodes = AF_PACKET_INPUT_N_NEXT,
299   .next_nodes = {
300     [AF_PACKET_INPUT_NEXT_DROP] = "error-drop",
301     [AF_PACKET_INPUT_NEXT_ETHERNET_INPUT] = "ethernet-input",
302   },
303 };
304
305 VLIB_NODE_FUNCTION_MULTIARCH (af_packet_input_node, af_packet_input_fn)
306 /* *INDENT-ON* */
307
308
309 /*
310  * fd.io coding-style-patch-verification: ON
311  *
312  * Local Variables:
313  * eval: (c-set-style "gnu")
314  * End:
315  */