Punt: specify packets by IP protocol Type
[vpp.git] / src / vnet / ip / punt.c
1 /*
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>
26 #include <vlib/vlib.h>
27 #include <vnet/pg/pg.h>
28 #include <vnet/udp/udp.h>
29 #include <vnet/tcp/tcp.h>
30 #include <vnet/sctp/sctp.h>
31 #include <vnet/ip/punt.h>
32 #include <vlib/unix/unix.h>
33
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <sys/socket.h>
37 #include <sys/uio.h>
38 #include <stdlib.h>
39
40 punt_main_t punt_main;
41
42 char *
43 vnet_punt_get_server_pathname (void)
44 {
45   punt_main_t *pm = &punt_main;
46   return pm->sun_path;
47 }
48
49 static void
50 punt_client_l4_db_add (ip_address_family_t af, u16 port, u32 index)
51 {
52   punt_main_t *pm = &punt_main;
53
54   pm->db.clients_by_l4_port = hash_set (pm->db.clients_by_l4_port,
55                                         punt_client_l4_mk_key (af, port),
56                                         index);
57 }
58
59 static u32
60 punt_client_l4_db_remove (ip_address_family_t af, u16 port)
61 {
62   punt_main_t *pm = &punt_main;
63   u32 key, index = ~0;
64   uword *p;
65
66   key = punt_client_l4_mk_key (af, port);
67   p = hash_get (pm->db.clients_by_l4_port, key);
68
69   if (p)
70     index = p[0];
71
72   hash_unset (pm->db.clients_by_l4_port, key);
73
74   return (index);
75 }
76
77 static void
78 punt_client_ip_proto_db_add (ip_address_family_t af,
79                              ip_protocol_t proto, u32 index)
80 {
81   punt_main_t *pm = &punt_main;
82
83   pm->db.clients_by_ip_proto = hash_set (pm->db.clients_by_ip_proto,
84                                          punt_client_ip_proto_mk_key (af,
85                                                                       proto),
86                                          index);
87 }
88
89 static u32
90 punt_client_ip_proto_db_remove (ip_address_family_t af, ip_protocol_t proto)
91 {
92   punt_main_t *pm = &punt_main;
93   u32 key, index = ~0;
94   uword *p;
95
96   key = punt_client_ip_proto_mk_key (af, proto);
97   p = hash_get (pm->db.clients_by_ip_proto, key);
98
99   if (p)
100     index = p[0];
101
102   hash_unset (pm->db.clients_by_ip_proto, key);
103
104   return (index);
105 }
106
107 static void
108 punt_client_exception_db_add (vlib_punt_reason_t reason, u32 pci)
109 {
110   punt_main_t *pm = &punt_main;
111
112   vec_validate_init_empty (pm->db.clients_by_exception, reason, ~0);
113
114   pm->db.clients_by_exception[reason] = pci;
115 }
116
117 static u32
118 punt_client_exception_db_remove (vlib_punt_reason_t reason)
119 {
120   punt_main_t *pm = &punt_main;
121   u32 pci = ~0;
122
123   if (punt_client_exception_get (reason))
124     {
125       pci = pm->db.clients_by_exception[reason];
126       pm->db.clients_by_exception[reason] = ~0;
127     }
128
129   return pci;
130 }
131
132 static clib_error_t *
133 punt_socket_read_ready (clib_file_t * uf)
134 {
135   vlib_main_t *vm = vlib_get_main ();
136   punt_main_t *pm = &punt_main;
137
138   /** Schedule the rx node */
139   vlib_node_set_interrupt_pending (vm, punt_socket_rx_node.index);
140   vec_add1 (pm->ready_fds, uf->file_descriptor);
141
142   return 0;
143 }
144
145 static clib_error_t *
146 punt_socket_register_l4 (vlib_main_t * vm,
147                          ip_address_family_t af,
148                          u8 protocol, u16 port, char *client_pathname)
149 {
150   punt_main_t *pm = &punt_main;
151   punt_client_t *c;
152
153   /* For now we only support UDP punt */
154   if (protocol != IP_PROTOCOL_UDP)
155     return clib_error_return (0,
156                               "only UDP protocol (%d) is supported, got %d",
157                               IP_PROTOCOL_UDP, protocol);
158
159   if (port == (u16) ~ 0)
160     return clib_error_return (0, "UDP port number required");
161
162   c = punt_client_l4_get (af, port);
163
164   if (NULL == c)
165     {
166       pool_get_zero (pm->punt_client_pool, c);
167       punt_client_l4_db_add (af, port, c - pm->punt_client_pool);
168     }
169
170   memcpy (c->caddr.sun_path, client_pathname, sizeof (c->caddr.sun_path));
171   c->caddr.sun_family = AF_UNIX;
172   c->reg.type = PUNT_TYPE_L4;
173   c->reg.punt.l4.port = port;
174   c->reg.punt.l4.protocol = protocol;
175   c->reg.punt.l4.af = af;
176
177   u32 node_index = (af == AF_IP4 ?
178                     udp4_punt_socket_node.index :
179                     udp6_punt_socket_node.index);
180
181   udp_register_dst_port (vm, port, node_index, af == AF_IP4);
182
183   return (NULL);
184 }
185
186 static clib_error_t *
187 punt_socket_register_ip_proto (vlib_main_t * vm,
188                                ip_address_family_t af,
189                                ip_protocol_t proto, char *client_pathname)
190 {
191   punt_main_t *pm = &punt_main;
192   punt_client_t *c;
193
194   c = punt_client_ip_proto_get (af, proto);
195
196   if (NULL == c)
197     {
198       pool_get_zero (pm->punt_client_pool, c);
199       punt_client_ip_proto_db_add (af, proto, c - pm->punt_client_pool);
200     }
201
202   memcpy (c->caddr.sun_path, client_pathname, sizeof (c->caddr.sun_path));
203   c->caddr.sun_family = AF_UNIX;
204   c->reg.type = PUNT_TYPE_IP_PROTO;
205   c->reg.punt.ip_proto.protocol = proto;
206   c->reg.punt.ip_proto.af = af;
207
208   if (af == AF_IP4)
209     ip4_register_protocol (proto, ip4_proto_punt_socket_node.index);
210   else
211     ip6_register_protocol (proto, ip6_proto_punt_socket_node.index);
212
213   return (NULL);
214 }
215
216 static clib_error_t *
217 punt_socket_register_exception (vlib_main_t * vm,
218                                 vlib_punt_reason_t reason,
219                                 char *client_pathname)
220 {
221   punt_main_t *pm = &punt_main;
222   punt_client_t *pc;
223
224   pc = punt_client_exception_get (reason);
225
226   if (NULL == pc)
227     {
228       pool_get_zero (pm->punt_client_pool, pc);
229       punt_client_exception_db_add (reason, pc - pm->punt_client_pool);
230     }
231
232   memcpy (pc->caddr.sun_path, client_pathname, sizeof (pc->caddr.sun_path));
233   pc->caddr.sun_family = AF_UNIX;
234   pc->reg.type = PUNT_TYPE_EXCEPTION;
235   pc->reg.punt.exception.reason = reason;
236
237   vlib_punt_register (pm->hdl,
238                       pc->reg.punt.exception.reason, "exception-punt-socket");
239
240   return (NULL);
241 }
242
243 static clib_error_t *
244 punt_socket_unregister_l4 (ip_address_family_t af,
245                            ip_protocol_t protocol, u16 port)
246 {
247   u32 pci;
248
249   udp_unregister_dst_port (vlib_get_main (), port, af == AF_IP4);
250
251   pci = punt_client_l4_db_remove (af, port);
252
253   if (~0 != pci)
254     pool_put_index (punt_main.punt_client_pool, pci);
255
256   return (NULL);
257 }
258
259 static clib_error_t *
260 punt_socket_unregister_ip_proto (ip_address_family_t af, ip_protocol_t proto)
261 {
262   u32 pci;
263
264   if (af == AF_IP4)
265     ip4_unregister_protocol (proto);
266   else
267     ip6_unregister_protocol (proto);
268
269   pci = punt_client_ip_proto_db_remove (af, proto);
270
271   if (~0 != pci)
272     pool_put_index (punt_main.punt_client_pool, pci);
273
274   return (NULL);
275 }
276
277 static clib_error_t *
278 punt_socket_unregister_exception (vlib_punt_reason_t reason)
279 {
280   u32 pci;
281
282   pci = punt_client_exception_db_remove (reason);
283
284   if (~0 != pci)
285     pool_put_index (punt_main.punt_client_pool, pci);
286
287   return (NULL);
288 }
289
290 clib_error_t *
291 vnet_punt_socket_add (vlib_main_t * vm, u32 header_version,
292                       const punt_reg_t * pr, char *client_pathname)
293 {
294   punt_main_t *pm = &punt_main;
295
296   if (!pm->is_configured)
297     return clib_error_return (0, "socket is not configured");
298
299   if (header_version != PUNT_PACKETDESC_VERSION)
300     return clib_error_return (0, "Invalid packet descriptor version");
301
302   if (strncmp (client_pathname, vnet_punt_get_server_pathname (),
303                UNIX_PATH_MAX) == 0)
304     return clib_error_return (0,
305                               "Punt socket: Invalid client path: %s",
306                               client_pathname);
307
308   /* Register client */
309   switch (pr->type)
310     {
311     case PUNT_TYPE_L4:
312       return (punt_socket_register_l4 (vm,
313                                        pr->punt.l4.af,
314                                        pr->punt.l4.protocol,
315                                        pr->punt.l4.port, client_pathname));
316     case PUNT_TYPE_IP_PROTO:
317       return (punt_socket_register_ip_proto (vm,
318                                              pr->punt.ip_proto.af,
319                                              pr->punt.ip_proto.protocol,
320                                              client_pathname));
321     case PUNT_TYPE_EXCEPTION:
322       return (punt_socket_register_exception (vm,
323                                               pr->punt.exception.reason,
324                                               client_pathname));
325     }
326
327   return 0;
328 }
329
330 clib_error_t *
331 vnet_punt_socket_del (vlib_main_t * vm, const punt_reg_t * pr)
332 {
333   punt_main_t *pm = &punt_main;
334
335   if (!pm->is_configured)
336     return clib_error_return (0, "socket is not configured");
337
338   switch (pr->type)
339     {
340     case PUNT_TYPE_L4:
341       return (punt_socket_unregister_l4 (pr->punt.l4.af,
342                                          pr->punt.l4.protocol,
343                                          pr->punt.l4.port));
344     case PUNT_TYPE_IP_PROTO:
345       return (punt_socket_unregister_ip_proto (pr->punt.ip_proto.af,
346                                                pr->punt.ip_proto.protocol));
347     case PUNT_TYPE_EXCEPTION:
348       return (punt_socket_unregister_exception (pr->punt.exception.reason));
349     }
350
351   return 0;
352 }
353
354 /**
355  * @brief Request IP traffic punt to the local TCP/IP stack.
356  *
357  * @em Note
358  * - UDP, TCP and SCTP are the only protocols supported in the current implementation
359  *
360  * @param vm       vlib_main_t corresponding to the current thread
361  * @param af       IP address family.
362  * @param protocol 8-bits L4 protocol value
363  *                 UDP is 17
364  *                 TCP is 1
365  * @param port     16-bits L4 (TCP/IP) port number when applicable (UDP only)
366  *
367  * @returns 0 on success, non-zero value otherwise
368  */
369 static clib_error_t *
370 punt_l4_add_del (vlib_main_t * vm,
371                  ip_address_family_t af,
372                  ip_protocol_t protocol, u16 port, bool is_add)
373 {
374   /* For now we only support TCP, UDP and SCTP punt */
375   if (protocol != IP_PROTOCOL_UDP &&
376       protocol != IP_PROTOCOL_TCP && protocol != IP_PROTOCOL_SCTP)
377     return clib_error_return (0,
378                               "only UDP (%d), TCP (%d) and SCTP (%d) protocols are supported, got %d",
379                               IP_PROTOCOL_UDP, IP_PROTOCOL_TCP,
380                               IP_PROTOCOL_SCTP, protocol);
381
382   if (port == (u16) ~ 0)
383     {
384       if (protocol == IP_PROTOCOL_UDP)
385         udp_punt_unknown (vm, af == AF_IP4, is_add);
386       else if (protocol == IP_PROTOCOL_TCP)
387         tcp_punt_unknown (vm, af == AF_IP4, is_add);
388       else if (protocol == IP_PROTOCOL_SCTP)
389         sctp_punt_unknown (vm, af == AF_IP4, is_add);
390
391       return 0;
392     }
393
394   else if (is_add)
395     {
396       if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_SCTP)
397         return clib_error_return (0,
398                                   "punt TCP/SCTP ports is not supported yet");
399
400       if (!udp_is_valid_dst_port (port, af == AF_IP4))
401         return clib_error_return (0, "invalid port: %d", port);
402
403       udp_register_dst_port (vm, port, udp4_punt_node.index, af == AF_IP4);
404
405       return 0;
406     }
407   else
408     {
409       if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_SCTP)
410         return clib_error_return (0,
411                                   "punt TCP/SCTP ports is not supported yet");
412
413       udp_unregister_dst_port (vm, port, af == AF_IP4);
414
415       return 0;
416     }
417 }
418
419 clib_error_t *
420 vnet_punt_add_del (vlib_main_t * vm, const punt_reg_t * pr, bool is_add)
421 {
422   switch (pr->type)
423     {
424     case PUNT_TYPE_L4:
425       return (punt_l4_add_del (vm, pr->punt.l4.af, pr->punt.l4.protocol,
426                                pr->punt.l4.port, is_add));
427     case PUNT_TYPE_EXCEPTION:
428     case PUNT_TYPE_IP_PROTO:
429       break;
430     }
431
432   return (clib_error_return (0, "Unsupported punt type: %d", pr->type));
433 }
434
435 static clib_error_t *
436 punt_cli (vlib_main_t * vm,
437           unformat_input_t * input, vlib_cli_command_t * cmd)
438 {
439   clib_error_t *error = NULL;
440   bool is_add = true;
441   punt_reg_t pr = {
442     .punt = {
443              .l4 = {
444                     .af = AF_IP4,
445                     .port = ~0,
446                     .protocol = ~0,
447                     },
448              },
449     .type = PUNT_TYPE_L4,
450   };
451
452   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
453     {
454       if (unformat (input, "del"))
455         is_add = false;
456       else if (unformat (input, "ipv6"))
457         pr.punt.l4.af = AF_IP6;
458       else if (unformat (input, "ip6"))
459         pr.punt.l4.af = AF_IP6;
460       else if (unformat (input, "%d", &pr.punt.l4.port))
461         ;
462       else if (unformat (input, "udp"))
463         pr.punt.l4.protocol = IP_PROTOCOL_UDP;
464       else if (unformat (input, "tcp"))
465         pr.punt.l4.protocol = IP_PROTOCOL_TCP;
466       else
467         {
468           error = clib_error_return (0, "parse error: '%U'",
469                                      format_unformat_error, input);
470           goto done;
471         }
472     }
473
474   /* punt both IPv6 and IPv4 when used in CLI */
475   error = vnet_punt_add_del (vm, &pr, is_add);
476   if (error)
477     {
478       clib_error_report (error);
479     }
480
481 done:
482   return error;
483 }
484
485 /*?
486  * The set of '<em>set punt</em>' commands allows specific IP traffic to
487  * be punted to the host TCP/IP stack
488  *
489  * @em Note
490  * - UDP is the only protocol supported in the current implementation
491  * - All TCP traffic is currently punted to the host by default
492  *
493  * @cliexpar
494  * @parblock
495  * Example of how to request NTP traffic to be punted
496  * @cliexcmd{set punt udp 125}
497  *
498  * Example of how to request all 'unknown' UDP traffic to be punted
499  * @cliexcmd{set punt udp all}
500  *
501  * Example of how to stop all 'unknown' UDP traffic to be punted
502  * @cliexcmd{set punt udp del all}
503  * @endparblock
504 ?*/
505 /* *INDENT-OFF* */
506 VLIB_CLI_COMMAND (punt_command, static) = {
507   .path = "set punt",
508   .short_help = "set punt [udp|tcp] [del] <all | port-num1 [port-num2 ...]>",
509   .function = punt_cli,
510 };
511 /* *INDENT-ON* */
512
513 static clib_error_t *
514 punt_socket_register_cmd (vlib_main_t * vm,
515                           unformat_input_t * input, vlib_cli_command_t * cmd)
516 {
517   u8 *socket_name = 0;
518   clib_error_t *error = NULL;
519   /* *INDENT-OFF* */
520   punt_reg_t pr = {
521     .punt = {
522       .l4 = {
523         .af = AF_IP4,
524         .port = ~0,
525         .protocol = ~0,
526       },
527     },
528     .type = PUNT_TYPE_L4,
529   };
530   /* *INDENT-ON* */
531
532   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
533     {
534       if (unformat (input, "ipv4"))
535         ;
536       else if (unformat (input, "ipv6"))
537         pr.punt.l4.af = AF_IP6;
538       else if (unformat (input, "udp"))
539         pr.punt.l4.protocol = IP_PROTOCOL_UDP;
540       else if (unformat (input, "tcp"))
541         pr.punt.l4.protocol = IP_PROTOCOL_TCP;
542       else if (unformat (input, "%d", &pr.punt.l4.port))
543         ;
544       else if (unformat (input, "socket %s", &socket_name))
545         ;
546       else
547         {
548           error = clib_error_return (0, "parse error: '%U'",
549                                      format_unformat_error, input);
550           goto done;
551         }
552     }
553
554   error = vnet_punt_socket_add (vm, 1, &pr, (char *) socket_name);
555
556 done:
557   return error;
558 }
559
560 /*?
561  *
562  * @cliexpar
563  * @cliexcmd{punt socket register}
564  ?*/
565 /* *INDENT-OFF* */
566 VLIB_CLI_COMMAND (punt_socket_register_command, static) =
567 {
568   .path = "punt socket register",
569   .function = punt_socket_register_cmd,
570   .short_help = "punt socket register [ipv4|ipv6] [udp|tcp]> <all | port-num1 [port-num2 ...]> <socket>",
571   .is_mp_safe = 1,
572 };
573 /* *INDENT-ON* */
574
575 static clib_error_t *
576 punt_socket_deregister_cmd (vlib_main_t * vm,
577                             unformat_input_t * input,
578                             vlib_cli_command_t * cmd)
579 {
580   clib_error_t *error = NULL;
581   /* *INDENT-OFF* */
582   punt_reg_t pr = {
583     .punt = {
584       .l4 = {
585         .af = AF_IP4,
586         .port = ~0,
587         .protocol = ~0,
588       },
589     },
590     .type = PUNT_TYPE_L4,
591   };
592   /* *INDENT-ON* */
593
594   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
595     {
596       if (unformat (input, "ipv4"))
597         ;
598       else if (unformat (input, "ipv6"))
599         pr.punt.l4.af = AF_IP6;
600       else if (unformat (input, "udp"))
601         pr.punt.l4.protocol = IP_PROTOCOL_UDP;
602       else if (unformat (input, "tcp"))
603         pr.punt.l4.protocol = IP_PROTOCOL_TCP;
604       else if (unformat (input, "%d", &pr.punt.l4.port))
605         ;
606       else
607         {
608           error = clib_error_return (0, "parse error: '%U'",
609                                      format_unformat_error, input);
610           goto done;
611         }
612     }
613
614   error = vnet_punt_socket_del (vm, &pr);
615 done:
616   return error;
617 }
618
619 /*?
620  *
621  * @cliexpar
622  * @cliexcmd{punt socket register}
623  ?*/
624 /* *INDENT-OFF* */
625 VLIB_CLI_COMMAND (punt_socket_deregister_command, static) =
626 {
627   .path = "punt socket deregister",
628   .function = punt_socket_deregister_cmd,
629   .short_help = "punt socket deregister [ipv4|ipv6] [udp|tcp]> <all | port-num1 [port-num2 ...]>",
630   .is_mp_safe = 1,
631 };
632 /* *INDENT-ON* */
633
634 void
635 punt_client_walk (punt_type_t pt, punt_client_walk_cb_t cb, void *ctx)
636 {
637   punt_main_t *pm = &punt_main;
638
639   switch (pt)
640     {
641     case PUNT_TYPE_L4:
642       {
643         u32 pci, key;
644
645         /* *INDENT-OFF* */
646         hash_foreach(key, pci, pm->db.clients_by_l4_port,
647         ({
648           cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
649         }));
650         /* *INDENT-ON* */
651         break;
652       }
653     case PUNT_TYPE_IP_PROTO:
654       {
655         u32 pci, key;
656
657         /* *INDENT-OFF* */
658         hash_foreach(key, pci, pm->db.clients_by_ip_proto,
659         ({
660           cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
661         }));
662         /* *INDENT-ON* */
663         break;
664       }
665     case PUNT_TYPE_EXCEPTION:
666       {
667         u32 *pci;
668
669         vec_foreach (pci, pm->db.clients_by_exception)
670         {
671           if (~0 != *pci)
672             cb (pool_elt_at_index (pm->punt_client_pool, *pci), ctx);
673         }
674
675         break;
676       }
677     }
678 }
679
680 static u8 *
681 format_punt_client (u8 * s, va_list * args)
682 {
683   punt_client_t *pc = va_arg (*args, punt_client_t *);
684
685   s = format (s, " punt ");
686
687   switch (pc->reg.type)
688     {
689     case PUNT_TYPE_L4:
690       s = format (s, "%U %U port %d",
691                   format_ip_address_family, pc->reg.punt.l4.af,
692                   format_ip_protocol, pc->reg.punt.l4.protocol,
693                   pc->reg.punt.l4.port);
694       break;
695     case PUNT_TYPE_IP_PROTO:
696       s = format (s, "%U %U",
697                   format_ip_address_family, pc->reg.punt.ip_proto.af,
698                   format_ip_protocol, pc->reg.punt.ip_proto.protocol);
699       break;
700     case PUNT_TYPE_EXCEPTION:
701       s = format (s, " %U", format_vlib_punt_reason,
702                   pc->reg.punt.exception.reason);
703       break;
704     }
705
706   s = format (s, " to socket %s \n", pc->caddr.sun_path);
707
708   return (s);
709 }
710
711 static walk_rc_t
712 punt_client_show_one (const punt_client_t * pc, void *ctx)
713 {
714   vlib_cli_output (ctx, "%U", format_punt_client, pc);
715
716   return (WALK_CONTINUE);
717 }
718
719 static clib_error_t *
720 punt_socket_show_cmd (vlib_main_t * vm,
721                       unformat_input_t * input, vlib_cli_command_t * cmd)
722 {
723   clib_error_t *error = NULL;
724   punt_type_t pt;
725
726   pt = PUNT_TYPE_L4;
727
728   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
729     {
730       if (unformat (input, "exception"))
731         pt = PUNT_TYPE_EXCEPTION;
732       else if (unformat (input, "l4"))
733         pt = PUNT_TYPE_L4;
734       else if (unformat (input, "ip"))
735         pt = PUNT_TYPE_IP_PROTO;
736       else
737         {
738           error = clib_error_return (0, "parse error: '%U'",
739                                      format_unformat_error, input);
740           goto done;
741         }
742     }
743
744   punt_client_walk (pt, punt_client_show_one, vm);
745
746 done:
747   return (error);
748 }
749
750 /*?
751  *
752  * @cliexpar
753  * @cliexcmd{show punt socket ipv4}
754  ?*/
755 /* *INDENT-OFF* */
756 VLIB_CLI_COMMAND (show_punt_socket_registration_command, static) =
757 {
758   .path = "show punt socket registrations",
759   .function = punt_socket_show_cmd,
760   .short_help = "show punt socket registrations [l4|exception]",
761   .is_mp_safe = 1,
762 };
763 /* *INDENT-ON* */
764
765 clib_error_t *
766 ip_punt_init (vlib_main_t * vm)
767 {
768   clib_error_t *error = NULL;
769   punt_main_t *pm = &punt_main;
770
771   pm->is_configured = false;
772   pm->interface_output_node =
773     vlib_get_node_by_name (vm, (u8 *) "interface-output");
774
775   if ((error = vlib_call_init_function (vm, punt_init)))
776     return error;
777
778   pm->hdl = vlib_punt_client_register ("ip-punt");
779
780   return (error);
781 }
782
783 VLIB_INIT_FUNCTION (ip_punt_init);
784
785 static clib_error_t *
786 punt_config (vlib_main_t * vm, unformat_input_t * input)
787 {
788   punt_main_t *pm = &punt_main;
789   char *socket_path = 0;
790
791   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
792     {
793       if (unformat (input, "socket %s", &socket_path))
794         strncpy (pm->sun_path, socket_path, UNIX_PATH_MAX - 1);
795       else
796         return clib_error_return (0, "unknown input `%U'",
797                                   format_unformat_error, input);
798     }
799
800   if (socket_path == 0)
801     return 0;
802
803   /* UNIX domain socket */
804   struct sockaddr_un addr;
805   if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1)
806     {
807       return clib_error_return (0, "socket error");
808     }
809
810   clib_memset (&addr, 0, sizeof (addr));
811   addr.sun_family = AF_UNIX;
812   if (*socket_path == '\0')
813     {
814       *addr.sun_path = '\0';
815       strncpy (addr.sun_path + 1, socket_path + 1,
816                sizeof (addr.sun_path) - 2);
817     }
818   else
819     {
820       strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1);
821       unlink (socket_path);
822     }
823
824   if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
825     {
826       return clib_error_return (0, "bind error");
827     }
828
829   int n_bytes = 0x10000;
830
831   if (setsockopt
832       (pm->socket_fd, SOL_SOCKET, SO_SNDBUF, &n_bytes,
833        sizeof (n_bytes)) == -1)
834     {
835       return clib_error_return (0, "setsockopt error");
836     }
837
838   /* Register socket */
839   clib_file_main_t *fm = &file_main;
840   clib_file_t template = { 0 };
841   template.read_function = punt_socket_read_ready;
842   template.file_descriptor = pm->socket_fd;
843   template.description = format (0, "%s", socket_path);
844   pm->clib_file_index = clib_file_add (fm, &template);
845
846   pm->is_configured = true;
847
848   return 0;
849 }
850
851 VLIB_CONFIG_FUNCTION (punt_config, "punt");
852
853 /*
854  * fd.io coding-style-patch-verification: ON
855  *
856  * Local Variables:
857  * eval: (c-set-style "gnu")
858  * End:
859  */