blob: 6400e49c626262d2d90941d8e0703946b5474fa7 [file] [log] [blame]
Neale Ranns50f0ac02019-05-15 02:13:37 -07001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/**
17 * @file
18 * @brief Local TCP/IP stack punt infrastructure.
19 *
20 * Provides a set of VPP nodes together with the relevant APIs and CLI
21 * commands in order to adjust and dispatch packets from the VPP data plane
22 * to the local TCP/IP stack
23 */
24
25#include <vnet/ip/ip.h>
Andrew Yourtchenkofeb77422023-03-17 01:47:58 +000026#include <vnet/ethernet/ethernet.h>
Neale Ranns50f0ac02019-05-15 02:13:37 -070027#include <vlib/vlib.h>
Neale Ranns50f0ac02019-05-15 02:13:37 -070028#include <vnet/ip/punt.h>
29#include <vlib/unix/unix.h>
30
31#include <stdio.h>
32#include <unistd.h>
33#include <sys/socket.h>
34#include <sys/uio.h>
35#include <stdlib.h>
36
37typedef enum
38{
39#define punt_error(n,s) PUNT_ERROR_##n,
40#include <vnet/ip/punt_error.def>
41#undef punt_error
42 PUNT_N_ERROR,
43} punt_error_t;
44
45#define foreach_punt_next \
46 _ (PUNT4, "ip4-punt") \
47 _ (PUNT6, "ip6-punt")
48
49typedef enum
50{
51#define _(s,n) PUNT_NEXT_##s,
52 foreach_punt_next
53#undef _
54 PUNT_N_NEXT,
55} punt_next_t;
56
57enum punt_socket_rx_next_e
58{
59 PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT,
60 PUNT_SOCKET_RX_NEXT_IP4_LOOKUP,
61 PUNT_SOCKET_RX_NEXT_IP6_LOOKUP,
62 PUNT_SOCKET_RX_N_NEXT
63};
64
65#define punt_next_punt(is_ip4) (is_ip4 ? PUNT_NEXT_PUNT4 : PUNT_NEXT_PUNT6)
66
67/** @brief IPv4/IPv6 UDP punt node main loop.
68
69 This is the main loop inline function for IPv4/IPv6 UDP punt
70 transition node.
71
72 @param vm vlib_main_t corresponding to the current thread
73 @param node vlib_node_runtime_t
74 @param frame vlib_frame_t whose contents should be dispatched
75 @param is_ipv4 indicates if called for IPv4 or IPv6 node
76*/
77always_inline uword
78udp46_punt_inline (vlib_main_t * vm,
79 vlib_node_runtime_t * node,
80 vlib_frame_t * from_frame, int is_ip4)
81{
82 u32 n_left_from, *from, *to_next;
83 word advance;
84
85 from = vlib_frame_vector_args (from_frame);
86 n_left_from = from_frame->n_vectors;
87
88 /* udp[46]_lookup hands us the data payload, not the IP header */
89 if (is_ip4)
90 advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t));
91 else
92 advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t));
93
94 while (n_left_from > 0)
95 {
96 u32 n_left_to_next;
97
98 vlib_get_next_frame (vm, node, punt_next_punt (is_ip4), to_next,
99 n_left_to_next);
100
101 while (n_left_from > 0 && n_left_to_next > 0)
102 {
103 u32 bi0;
104 vlib_buffer_t *b0;
105
106 bi0 = from[0];
107 to_next[0] = bi0;
108 from += 1;
109 to_next += 1;
110 n_left_from -= 1;
111 n_left_to_next -= 1;
112
113 b0 = vlib_get_buffer (vm, bi0);
114 vlib_buffer_advance (b0, advance);
115 b0->error = node->errors[PUNT_ERROR_UDP_PORT];
116 }
117
118 vlib_put_next_frame (vm, node, punt_next_punt (is_ip4), n_left_to_next);
119 }
120
121 return from_frame->n_vectors;
122}
123
124static char *punt_error_strings[] = {
125#define punt_error(n,s) s,
126#include "punt_error.def"
127#undef punt_error
128};
129
130/** @brief IPv4 UDP punt node.
131 @node ip4-udp-punt
132
133 This is the IPv4 UDP punt transition node. It is registered as a next
134 node for the "ip4-udp-lookup" handling UDP port(s) requested for punt.
135 The buffer's current data pointer is adjusted to the original packet
136 IPv4 header. All buffers are dispatched to "error-punt".
137
138 @param vm vlib_main_t corresponding to the current thread
139 @param node vlib_node_runtime_t
140 @param frame vlib_frame_t whose contents should be dispatched
141
142 @par Graph mechanics: next index usage
143
144 @em Sets:
145 - <code>vnet_buffer(b)->current_data</code>
146 - <code>vnet_buffer(b)->current_len</code>
147
148 <em>Next Index:</em>
149 - Dispatches the packet to the "error-punt" node
150*/
151VLIB_NODE_FN (udp4_punt_node) (vlib_main_t * vm,
152 vlib_node_runtime_t * node,
153 vlib_frame_t * from_frame)
154{
155 return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ );
156}
157
158/** @brief IPv6 UDP punt node.
159 @node ip6-udp-punt
160
161 This is the IPv6 UDP punt transition node. It is registered as a next
162 node for the "ip6-udp-lookup" handling UDP port(s) requested for punt.
163 The buffer's current data pointer is adjusted to the original packet
164 IPv6 header. All buffers are dispatched to "error-punt".
165
166 @param vm vlib_main_t corresponding to the current thread
167 @param node vlib_node_runtime_t
168 @param frame vlib_frame_t whose contents should be dispatched
169
170 @par Graph mechanics: next index usage
171
172 @em Sets:
173 - <code>vnet_buffer(b)->current_data</code>
174 - <code>vnet_buffer(b)->current_len</code>
175
176 <em>Next Index:</em>
177 - Dispatches the packet to the "error-punt" node
178*/
179VLIB_NODE_FN (udp6_punt_node) (vlib_main_t * vm,
180 vlib_node_runtime_t * node,
181 vlib_frame_t * from_frame)
182{
183 return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ );
184}
185
Neale Ranns50f0ac02019-05-15 02:13:37 -0700186VLIB_REGISTER_NODE (udp4_punt_node) = {
187 .name = "ip4-udp-punt",
188 /* Takes a vector of packets. */
189 .vector_size = sizeof (u32),
190
191 .n_errors = PUNT_N_ERROR,
192 .error_strings = punt_error_strings,
193
194 .n_next_nodes = PUNT_N_NEXT,
195 .next_nodes = {
196#define _(s,n) [PUNT_NEXT_##s] = n,
197 foreach_punt_next
198#undef _
199 },
200};
201
202VLIB_REGISTER_NODE (udp6_punt_node) = {
203 .name = "ip6-udp-punt",
204 /* Takes a vector of packets. */
205 .vector_size = sizeof (u32),
206
207 .n_errors = PUNT_N_ERROR,
208 .error_strings = punt_error_strings,
209
210 .n_next_nodes = PUNT_N_NEXT,
211 .next_nodes = {
212#define _(s,n) [PUNT_NEXT_##s] = n,
213 foreach_punt_next
214#undef _
215 },
216};
Neale Ranns50f0ac02019-05-15 02:13:37 -0700217
218typedef struct
219{
220 punt_client_t client;
221 u8 is_midchain;
Neale Ranns43ba2922019-07-10 08:48:55 +0000222 u8 packet_data[64];
Neale Ranns50f0ac02019-05-15 02:13:37 -0700223} udp_punt_trace_t;
224
225static u8 *
226format_udp_punt_trace (u8 * s, va_list * args)
227{
228 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
229 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
230 udp_punt_trace_t *t = va_arg (*args, udp_punt_trace_t *);
231 u32 indent = format_get_indent (s);
232 s = format (s, "to: %s", t->client.caddr.sun_path);
233 if (t->is_midchain)
234 {
235 s = format (s, "\n%U(buffer is part of chain)", format_white_space,
236 indent);
237 }
Neale Ranns43ba2922019-07-10 08:48:55 +0000238 s = format (s, "\n%U%U", format_white_space, indent,
239 format_hex_bytes, t->packet_data, sizeof (t->packet_data));
240
Neale Ranns50f0ac02019-05-15 02:13:37 -0700241 return s;
242}
243
244always_inline uword
Ole Troan56b8abc2023-09-05 08:27:53 +0200245punt_socket_inline2 (vlib_main_t *vm, vlib_node_runtime_t *node,
246 vlib_frame_t *frame, punt_type_t pt,
247 ip_address_family_t af, ip_protocol_t protocol)
Neale Ranns50f0ac02019-05-15 02:13:37 -0700248{
249 u32 *buffers = vlib_frame_vector_args (frame);
Neale Ranns39040a62019-07-10 01:47:15 -0700250 u32 thread_index = vm->thread_index;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700251 uword n_packets = frame->n_vectors;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700252 punt_main_t *pm = &punt_main;
253 int i;
254
Neale Ranns39040a62019-07-10 01:47:15 -0700255 punt_thread_data_t *ptd = &pm->thread_data[thread_index];
256 u32 node_index = (AF_IP4 == af ?
257 udp4_punt_socket_node.index :
258 udp6_punt_socket_node.index);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700259
260 for (i = 0; i < n_packets; i++)
261 {
262 struct iovec *iov;
263 vlib_buffer_t *b;
264 uword l;
265 punt_packetdesc_t packetdesc;
266 punt_client_t *c;
Ole Troan56b8abc2023-09-05 08:27:53 +0200267 u16 port = 0;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700268 b = vlib_get_buffer (vm, buffers[i]);
269
270 if (PUNT_TYPE_L4 == pt)
271 {
Ole Troan56b8abc2023-09-05 08:27:53 +0200272 if (protocol == IP_PROTOCOL_UDP)
Neale Ranns50f0ac02019-05-15 02:13:37 -0700273 {
Ole Troan56b8abc2023-09-05 08:27:53 +0200274 /* Reverse UDP Punt advance */
275 udp_header_t *udp;
276 if (AF_IP4 == af)
277 {
278 vlib_buffer_advance (
279 b, -(sizeof (ip4_header_t) + sizeof (udp_header_t)));
280 ip4_header_t *ip = vlib_buffer_get_current (b);
281 udp = (udp_header_t *) (ip + 1);
282 }
283 else
284 {
285 vlib_buffer_advance (
286 b, -(sizeof (ip6_header_t) + sizeof (udp_header_t)));
287 ip6_header_t *ip = vlib_buffer_get_current (b);
288 udp = (udp_header_t *) (ip + 1);
289 }
290 port = clib_net_to_host_u16 (udp->dst_port);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700291 }
Ole Troan56b8abc2023-09-05 08:27:53 +0200292 else if (protocol == IP_PROTOCOL_ICMP6)
Neale Ranns50f0ac02019-05-15 02:13:37 -0700293 {
Neale Ranns50f0ac02019-05-15 02:13:37 -0700294 ip6_header_t *ip = vlib_buffer_get_current (b);
Ole Troan56b8abc2023-09-05 08:27:53 +0200295 icmp46_header_t *icmp = ip6_next_header (ip);
296 port = icmp->type;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700297 }
Neale Ranns50f0ac02019-05-15 02:13:37 -0700298 /*
299 * Find registerered client
300 * If no registered client, drop packet and count
301 */
Ole Troan56b8abc2023-09-05 08:27:53 +0200302 c = punt_client_l4_get (af, port);
Neale Rannsb538dd82019-05-21 06:54:54 -0700303 }
304 else if (PUNT_TYPE_IP_PROTO == pt)
305 {
306 /* Reverse UDP Punt advance */
307 ip_protocol_t proto;
308
309 if (AF_IP4 == af)
310 {
311 ip4_header_t *ip = vlib_buffer_get_current (b);
312 proto = ip->protocol;
313 }
314 else
315 {
316 ip6_header_t *ip = vlib_buffer_get_current (b);
317 proto = ip->protocol;
318 }
319
320 c = punt_client_ip_proto_get (af, proto);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700321 }
322 else if (PUNT_TYPE_EXCEPTION == pt)
323 {
324 c = punt_client_exception_get (b->punt_reason);
325 }
326 else
327 c = NULL;
328
329 if (PREDICT_FALSE (NULL == c))
330 {
331 vlib_node_increment_counter (vm, node_index,
332 PUNT_ERROR_SOCKET_TX_ERROR, 1);
333 goto error;
334 }
335
336 struct sockaddr_un *caddr = &c->caddr;
337
Neale Ranns39040a62019-07-10 01:47:15 -0700338 /* Re-set iovecs */
339 vec_reset_length (ptd->iovecs);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700340
341 /* Add packet descriptor */
342 packetdesc.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
343 packetdesc.action = 0;
Neale Ranns39040a62019-07-10 01:47:15 -0700344 vec_add2 (ptd->iovecs, iov, 1);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700345 iov->iov_base = &packetdesc;
346 iov->iov_len = sizeof (packetdesc);
347
348 /** VLIB buffer chain -> Unix iovec(s). */
Andrew Yourtchenkofeb77422023-03-17 01:47:58 +0000349 vlib_buffer_advance (b, -ethernet_buffer_header_size (b));
Neale Ranns39040a62019-07-10 01:47:15 -0700350 vec_add2 (ptd->iovecs, iov, 1);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700351 iov->iov_base = b->data + b->current_data;
352 iov->iov_len = l = b->current_length;
353
Neale Ranns43ba2922019-07-10 08:48:55 +0000354 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
355 {
356 udp_punt_trace_t *t;
357 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
358 clib_memcpy_fast (&t->client, c, sizeof (t->client));
359 clib_memcpy_fast (t->packet_data,
360 vlib_buffer_get_current (b),
361 sizeof (t->packet_data));
362 }
363
Neale Ranns50f0ac02019-05-15 02:13:37 -0700364 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
365 {
366 do
367 {
368 b = vlib_get_buffer (vm, b->next_buffer);
369 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
370 {
371 udp_punt_trace_t *t;
372 t = vlib_add_trace (vm, node, b, sizeof (t[0]));
373 clib_memcpy_fast (&t->client, c, sizeof (t->client));
374 t->is_midchain = 1;
375 }
376
Neale Ranns39040a62019-07-10 01:47:15 -0700377 vec_add2 (ptd->iovecs, iov, 1);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700378
379 iov->iov_base = b->data + b->current_data;
380 iov->iov_len = b->current_length;
381 l += b->current_length;
382 }
383 while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
384 }
385
386 struct msghdr msg = {
387 .msg_name = caddr,
388 .msg_namelen = sizeof (*caddr),
Neale Ranns39040a62019-07-10 01:47:15 -0700389 .msg_iov = ptd->iovecs,
390 .msg_iovlen = vec_len (ptd->iovecs),
Neale Ranns50f0ac02019-05-15 02:13:37 -0700391 };
392
393 if (sendmsg (pm->socket_fd, &msg, 0) < (ssize_t) l)
394 vlib_node_increment_counter (vm, node_index,
395 PUNT_ERROR_SOCKET_TX_ERROR, 1);
396 else
397 vlib_node_increment_counter (vm, node_index, PUNT_ERROR_SOCKET_TX, 1);
398 }
399
400error:
401 vlib_buffer_free (vm, buffers, n_packets);
402
403 return n_packets;
404}
405
Ole Troan56b8abc2023-09-05 08:27:53 +0200406always_inline uword
407punt_socket_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
408 vlib_frame_t *frame, punt_type_t pt,
409 ip_address_family_t af)
410{
411 return punt_socket_inline2 (vm, node, frame, pt, af, IP_PROTOCOL_UDP);
412}
413
Neale Ranns50f0ac02019-05-15 02:13:37 -0700414static uword
415udp4_punt_socket (vlib_main_t * vm,
416 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
417{
418 return punt_socket_inline (vm, node, from_frame, PUNT_TYPE_L4, AF_IP4);
419}
420
421static uword
422udp6_punt_socket (vlib_main_t * vm,
423 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
424{
425 return punt_socket_inline (vm, node, from_frame, PUNT_TYPE_L4, AF_IP6);
426}
427
428static uword
Neale Rannsb538dd82019-05-21 06:54:54 -0700429ip4_proto_punt_socket (vlib_main_t * vm,
430 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
431{
432 return punt_socket_inline (vm, node, from_frame,
433 PUNT_TYPE_IP_PROTO, AF_IP4);
434}
435
436static uword
437ip6_proto_punt_socket (vlib_main_t * vm,
438 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
439{
440 return punt_socket_inline (vm, node, from_frame,
441 PUNT_TYPE_IP_PROTO, AF_IP6);
442}
443
444static uword
Ole Troan56b8abc2023-09-05 08:27:53 +0200445icmp6_punt_socket (vlib_main_t *vm, vlib_node_runtime_t *node,
446 vlib_frame_t *from_frame)
447{
448 return punt_socket_inline2 (vm, node, from_frame, PUNT_TYPE_L4, AF_IP6,
449 IP_PROTOCOL_ICMP6);
450}
451
452static uword
Neale Ranns50f0ac02019-05-15 02:13:37 -0700453exception_punt_socket (vlib_main_t * vm,
454 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
455{
456 return punt_socket_inline (vm, node, from_frame,
457 PUNT_TYPE_EXCEPTION, AF_IP4);
458}
459
460
Neale Ranns50f0ac02019-05-15 02:13:37 -0700461VLIB_REGISTER_NODE (udp4_punt_socket_node) = {
462 .function = udp4_punt_socket,
463 .name = "ip4-udp-punt-socket",
464 .format_trace = format_udp_punt_trace,
465 .flags = VLIB_NODE_FLAG_IS_DROP,
466 /* Takes a vector of packets. */
467 .vector_size = sizeof (u32),
468 .n_errors = PUNT_N_ERROR,
469 .error_strings = punt_error_strings,
470};
471VLIB_REGISTER_NODE (udp6_punt_socket_node) = {
472 .function = udp6_punt_socket,
473 .name = "ip6-udp-punt-socket",
474 .format_trace = format_udp_punt_trace,
475 .flags = VLIB_NODE_FLAG_IS_DROP,
476 .vector_size = sizeof (u32),
477 .n_errors = PUNT_N_ERROR,
478 .error_strings = punt_error_strings,
479};
Neale Rannsb538dd82019-05-21 06:54:54 -0700480VLIB_REGISTER_NODE (ip4_proto_punt_socket_node) = {
481 .function = ip4_proto_punt_socket,
482 .name = "ip4-proto-punt-socket",
483 .format_trace = format_udp_punt_trace,
484 .flags = VLIB_NODE_FLAG_IS_DROP,
485 /* Takes a vector of packets. */
486 .vector_size = sizeof (u32),
487 .n_errors = PUNT_N_ERROR,
488 .error_strings = punt_error_strings,
489};
490VLIB_REGISTER_NODE (ip6_proto_punt_socket_node) = {
491 .function = ip6_proto_punt_socket,
492 .name = "ip6-proto-punt-socket",
493 .format_trace = format_udp_punt_trace,
494 .flags = VLIB_NODE_FLAG_IS_DROP,
495 .vector_size = sizeof (u32),
496 .n_errors = PUNT_N_ERROR,
497 .error_strings = punt_error_strings,
498};
Neale Ranns50f0ac02019-05-15 02:13:37 -0700499VLIB_REGISTER_NODE (exception_punt_socket_node) = {
500 .function = exception_punt_socket,
501 .name = "exception-punt-socket",
502 .format_trace = format_udp_punt_trace,
503 .flags = VLIB_NODE_FLAG_IS_DROP,
504 .vector_size = sizeof (u32),
505 .n_errors = PUNT_N_ERROR,
506 .error_strings = punt_error_strings,
507};
Ole Troan56b8abc2023-09-05 08:27:53 +0200508VLIB_REGISTER_NODE (icmp6_punt_socket_node) = {
509 .function = icmp6_punt_socket,
510 .name = "ip6-icmp-punt-socket",
511 .format_trace = format_udp_punt_trace,
512 .flags = VLIB_NODE_FLAG_IS_DROP,
513 .vector_size = sizeof (u32),
514 .n_errors = PUNT_N_ERROR,
515 .error_strings = punt_error_strings,
516};
517
Neale Ranns50f0ac02019-05-15 02:13:37 -0700518
519typedef struct
520{
521 enum punt_action_e action;
522 u32 sw_if_index;
523} punt_trace_t;
524
525static u8 *
526format_punt_trace (u8 * s, va_list * va)
527{
528 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
529 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
530 vnet_main_t *vnm = vnet_get_main ();
531 punt_trace_t *t = va_arg (*va, punt_trace_t *);
532 s = format (s, "%U Action: %d", format_vnet_sw_if_index_name,
533 vnm, t->sw_if_index, t->action);
534 return s;
535}
536
537static uword
538punt_socket_rx_fd (vlib_main_t * vm, vlib_node_runtime_t * node, u32 fd)
539{
540 const uword buffer_size = vlib_buffer_get_default_data_size (vm);
541 u32 n_trace = vlib_get_trace_count (vm, node);
542 u32 next = node->cached_next_index;
543 u32 n_left_to_next, next_index;
544 u32 *to_next;
545 u32 error = PUNT_ERROR_NONE;
546 vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
547
548 /* $$$$ Only dealing with one buffer at the time for now */
549
550 u32 bi;
551 vlib_buffer_t *b;
552 punt_packetdesc_t packetdesc;
553 ssize_t size;
554 struct iovec io[2];
555
556 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
557 {
558 error = PUNT_ERROR_NOBUFFER;
559 goto error;
560 }
561
562 b = vlib_get_buffer (vm, bi);
563 io[0].iov_base = &packetdesc;
564 io[0].iov_len = sizeof (packetdesc);
565 io[1].iov_base = b->data;
566 io[1].iov_len = buffer_size;
567
568 size = readv (fd, io, 2);
569 /* We need at least the packet descriptor plus a header */
570 if (size <= (int) (sizeof (packetdesc) + sizeof (ip4_header_t)))
571 {
572 vlib_buffer_free (vm, &bi, 1);
573 error = PUNT_ERROR_READV;
574 goto error;
575 }
576
577 b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
578 b->current_length = size - sizeof (packetdesc);
579
Neale Ranns50f0ac02019-05-15 02:13:37 -0700580 switch (packetdesc.action)
581 {
582 case PUNT_L2:
583 vnet_buffer (b)->sw_if_index[VLIB_TX] = packetdesc.sw_if_index;
584 next_index = PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT;
585 break;
586
587 case PUNT_IP4_ROUTED:
588 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
589 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
590 next_index = PUNT_SOCKET_RX_NEXT_IP4_LOOKUP;
591 break;
592
593 case PUNT_IP6_ROUTED:
594 vnet_buffer (b)->sw_if_index[VLIB_RX] = packetdesc.sw_if_index;
595 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
596 next_index = PUNT_SOCKET_RX_NEXT_IP6_LOOKUP;
597 break;
598
599 default:
600 error = PUNT_ERROR_ACTION;
601 vlib_buffer_free (vm, &bi, 1);
602 goto error;
603 }
604
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200605 if (PREDICT_FALSE
606 (n_trace > 0
607 && vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ )))
Neale Ranns50f0ac02019-05-15 02:13:37 -0700608 {
609 punt_trace_t *t;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700610 vlib_set_trace_count (vm, node, --n_trace);
611 t = vlib_add_trace (vm, node, b, sizeof (*t));
612 t->sw_if_index = packetdesc.sw_if_index;
613 t->action = packetdesc.action;
614 }
615
616 to_next[0] = bi;
617 to_next++;
618 n_left_to_next--;
619
620 vlib_validate_buffer_enqueue_x1 (vm, node, next, to_next, n_left_to_next,
621 bi, next_index);
622 vlib_put_next_frame (vm, node, next, n_left_to_next);
Benoît Ganne9a3973e2020-10-02 19:36:57 +0200623
Neale Ranns50f0ac02019-05-15 02:13:37 -0700624 return 1;
625
626error:
Dave Barach1adc7e72020-04-08 14:31:45 -0400627 vlib_put_next_frame (vm, node, next, n_left_to_next);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700628 vlib_node_increment_counter (vm, punt_socket_rx_node.index, error, 1);
629 return 0;
630}
631
632static uword
633punt_socket_rx (vlib_main_t * vm,
634 vlib_node_runtime_t * node, vlib_frame_t * frame)
635{
636 punt_main_t *pm = &punt_main;
637 u32 total_count = 0;
638 int i;
639
640 for (i = 0; i < vec_len (pm->ready_fds); i++)
641 {
642 total_count += punt_socket_rx_fd (vm, node, pm->ready_fds[i]);
643 vec_del1 (pm->ready_fds, i);
644 }
645 return total_count;
646}
647
Neale Ranns50f0ac02019-05-15 02:13:37 -0700648VLIB_REGISTER_NODE (punt_socket_rx_node) =
649{
650 .function = punt_socket_rx,
651 .name = "punt-socket-rx",
Damjan Marion7ca5aaa2019-09-24 18:10:49 +0200652 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
Neale Ranns50f0ac02019-05-15 02:13:37 -0700653 .type = VLIB_NODE_TYPE_INPUT,
654 .state = VLIB_NODE_STATE_INTERRUPT,
655 .vector_size = 1,
656 .n_errors = PUNT_N_ERROR,
657 .error_strings = punt_error_strings,
658 .n_next_nodes = PUNT_SOCKET_RX_N_NEXT,
659 .next_nodes = {
660 [PUNT_SOCKET_RX_NEXT_INTERFACE_OUTPUT] = "interface-output",
661 [PUNT_SOCKET_RX_NEXT_IP4_LOOKUP] = "ip4-lookup",
662 [PUNT_SOCKET_RX_NEXT_IP6_LOOKUP] = "ip6-lookup",
663 },
664 .format_trace = format_punt_trace,
665};
Neale Ranns50f0ac02019-05-15 02:13:37 -0700666
667/*
668 * fd.io coding-style-patch-verification: ON
669 *
670 * Local Variables:
671 * eval: (c-set-style "gnu")
672 * End:
673 */