tcp: timestamp adjustment
[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       udp_register_dst_port (vm, port, udp4_punt_node.index, af == AF_IP4);
401
402       return 0;
403     }
404   else
405     {
406       if (protocol == IP_PROTOCOL_TCP || protocol == IP_PROTOCOL_SCTP)
407         return clib_error_return (0,
408                                   "punt TCP/SCTP ports is not supported yet");
409
410       udp_unregister_dst_port (vm, port, af == AF_IP4);
411
412       return 0;
413     }
414 }
415
416 clib_error_t *
417 vnet_punt_add_del (vlib_main_t * vm, const punt_reg_t * pr, bool is_add)
418 {
419   switch (pr->type)
420     {
421     case PUNT_TYPE_L4:
422       return (punt_l4_add_del (vm, pr->punt.l4.af, pr->punt.l4.protocol,
423                                pr->punt.l4.port, is_add));
424     case PUNT_TYPE_EXCEPTION:
425     case PUNT_TYPE_IP_PROTO:
426       break;
427     }
428
429   return (clib_error_return (0, "Unsupported punt type: %d", pr->type));
430 }
431
432 static clib_error_t *
433 punt_cli (vlib_main_t * vm,
434           unformat_input_t * input, vlib_cli_command_t * cmd)
435 {
436   clib_error_t *error = NULL;
437   bool is_add = true;
438   /* *INDENT-OFF* */
439   punt_reg_t pr = {
440     .punt = {
441       .l4 = {
442         .af = AF_IP4,
443         .port = ~0,
444         .protocol = ~0,
445       },
446     },
447     .type = PUNT_TYPE_L4,
448   };
449   u32 port;
450   /* *INDENT-ON* */
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", &port))
461         pr.punt.l4.port = port;
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   if (!socket_name)
555     error = clib_error_return (0, "socket name not specified");
556   else
557     error = vnet_punt_socket_add (vm, 1, &pr, (char *) socket_name);
558
559 done:
560   return error;
561 }
562
563 /*?
564  *
565  * @cliexpar
566  * @cliexcmd{punt socket register}
567  ?*/
568 /* *INDENT-OFF* */
569 VLIB_CLI_COMMAND (punt_socket_register_command, static) =
570 {
571   .path = "punt socket register",
572   .function = punt_socket_register_cmd,
573   .short_help = "punt socket register [ipv4|ipv6] [udp|tcp]> <all | port-num1 [port-num2 ...]> <socket>",
574   .is_mp_safe = 1,
575 };
576 /* *INDENT-ON* */
577
578 static clib_error_t *
579 punt_socket_deregister_cmd (vlib_main_t * vm,
580                             unformat_input_t * input,
581                             vlib_cli_command_t * cmd)
582 {
583   clib_error_t *error = NULL;
584   /* *INDENT-OFF* */
585   punt_reg_t pr = {
586     .punt = {
587       .l4 = {
588         .af = AF_IP4,
589         .port = ~0,
590         .protocol = ~0,
591       },
592     },
593     .type = PUNT_TYPE_L4,
594   };
595   /* *INDENT-ON* */
596
597   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
598     {
599       if (unformat (input, "ipv4"))
600         ;
601       else if (unformat (input, "ipv6"))
602         pr.punt.l4.af = AF_IP6;
603       else if (unformat (input, "udp"))
604         pr.punt.l4.protocol = IP_PROTOCOL_UDP;
605       else if (unformat (input, "tcp"))
606         pr.punt.l4.protocol = IP_PROTOCOL_TCP;
607       else if (unformat (input, "%d", &pr.punt.l4.port))
608         ;
609       else
610         {
611           error = clib_error_return (0, "parse error: '%U'",
612                                      format_unformat_error, input);
613           goto done;
614         }
615     }
616
617   error = vnet_punt_socket_del (vm, &pr);
618 done:
619   return error;
620 }
621
622 /*?
623  *
624  * @cliexpar
625  * @cliexcmd{punt socket register}
626  ?*/
627 /* *INDENT-OFF* */
628 VLIB_CLI_COMMAND (punt_socket_deregister_command, static) =
629 {
630   .path = "punt socket deregister",
631   .function = punt_socket_deregister_cmd,
632   .short_help = "punt socket deregister [ipv4|ipv6] [udp|tcp]> <all | port-num1 [port-num2 ...]>",
633   .is_mp_safe = 1,
634 };
635 /* *INDENT-ON* */
636
637 void
638 punt_client_walk (punt_type_t pt, punt_client_walk_cb_t cb, void *ctx)
639 {
640   punt_main_t *pm = &punt_main;
641
642   switch (pt)
643     {
644     case PUNT_TYPE_L4:
645       {
646         u32 pci, key;
647
648         /* *INDENT-OFF* */
649         hash_foreach(key, pci, pm->db.clients_by_l4_port,
650         ({
651           cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
652         }));
653         /* *INDENT-ON* */
654         break;
655       }
656     case PUNT_TYPE_IP_PROTO:
657       {
658         u32 pci, key;
659
660         /* *INDENT-OFF* */
661         hash_foreach(key, pci, pm->db.clients_by_ip_proto,
662         ({
663           cb (pool_elt_at_index(pm->punt_client_pool, pci), ctx);
664         }));
665         /* *INDENT-ON* */
666         break;
667       }
668     case PUNT_TYPE_EXCEPTION:
669       {
670         u32 *pci;
671
672         vec_foreach (pci, pm->db.clients_by_exception)
673         {
674           if (~0 != *pci)
675             cb (pool_elt_at_index (pm->punt_client_pool, *pci), ctx);
676         }
677
678         break;
679       }
680     }
681 }
682
683 static u8 *
684 format_punt_client (u8 * s, va_list * args)
685 {
686   punt_client_t *pc = va_arg (*args, punt_client_t *);
687
688   s = format (s, " punt ");
689
690   switch (pc->reg.type)
691     {
692     case PUNT_TYPE_L4:
693       s = format (s, "%U %U port %d",
694                   format_ip_address_family, pc->reg.punt.l4.af,
695                   format_ip_protocol, pc->reg.punt.l4.protocol,
696                   pc->reg.punt.l4.port);
697       break;
698     case PUNT_TYPE_IP_PROTO:
699       s = format (s, "%U %U",
700                   format_ip_address_family, pc->reg.punt.ip_proto.af,
701                   format_ip_protocol, pc->reg.punt.ip_proto.protocol);
702       break;
703     case PUNT_TYPE_EXCEPTION:
704       s = format (s, " %U", format_vlib_punt_reason,
705                   pc->reg.punt.exception.reason);
706       break;
707     }
708
709   s = format (s, " to socket %s \n", pc->caddr.sun_path);
710
711   return (s);
712 }
713
714 static walk_rc_t
715 punt_client_show_one (const punt_client_t * pc, void *ctx)
716 {
717   vlib_cli_output (ctx, "%U", format_punt_client, pc);
718
719   return (WALK_CONTINUE);
720 }
721
722 static clib_error_t *
723 punt_socket_show_cmd (vlib_main_t * vm,
724                       unformat_input_t * input, vlib_cli_command_t * cmd)
725 {
726   clib_error_t *error = NULL;
727   punt_type_t pt;
728
729   pt = PUNT_TYPE_L4;
730
731   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
732     {
733       if (unformat (input, "exception"))
734         pt = PUNT_TYPE_EXCEPTION;
735       else if (unformat (input, "l4"))
736         pt = PUNT_TYPE_L4;
737       else if (unformat (input, "ip"))
738         pt = PUNT_TYPE_IP_PROTO;
739       else
740         {
741           error = clib_error_return (0, "parse error: '%U'",
742                                      format_unformat_error, input);
743           goto done;
744         }
745     }
746
747   punt_client_walk (pt, punt_client_show_one, vm);
748
749 done:
750   return (error);
751 }
752
753 /*?
754  *
755  * @cliexpar
756  * @cliexcmd{show punt socket ipv4}
757  ?*/
758 /* *INDENT-OFF* */
759 VLIB_CLI_COMMAND (show_punt_socket_registration_command, static) =
760 {
761   .path = "show punt socket registrations",
762   .function = punt_socket_show_cmd,
763   .short_help = "show punt socket registrations [l4|exception]",
764   .is_mp_safe = 1,
765 };
766 /* *INDENT-ON* */
767
768 clib_error_t *
769 ip_punt_init (vlib_main_t * vm)
770 {
771   clib_error_t *error = NULL;
772   punt_main_t *pm = &punt_main;
773
774   pm->is_configured = false;
775   pm->interface_output_node =
776     vlib_get_node_by_name (vm, (u8 *) "interface-output");
777
778   if ((error = vlib_call_init_function (vm, punt_init)))
779     return error;
780
781   pm->hdl = vlib_punt_client_register ("ip-punt");
782
783   return (error);
784 }
785
786 VLIB_INIT_FUNCTION (ip_punt_init);
787
788 static clib_error_t *
789 punt_config (vlib_main_t * vm, unformat_input_t * input)
790 {
791   punt_main_t *pm = &punt_main;
792   char *socket_path = 0;
793
794   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
795     {
796       if (unformat (input, "socket %s", &socket_path))
797         strncpy (pm->sun_path, socket_path, UNIX_PATH_MAX - 1);
798       else
799         return clib_error_return (0, "unknown input `%U'",
800                                   format_unformat_error, input);
801     }
802
803   if (socket_path == 0)
804     return 0;
805
806   /* UNIX domain socket */
807   struct sockaddr_un addr;
808   if ((pm->socket_fd = socket (AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)) == -1)
809     {
810       return clib_error_return (0, "socket error");
811     }
812
813   clib_memset (&addr, 0, sizeof (addr));
814   addr.sun_family = AF_UNIX;
815   if (*socket_path == '\0')
816     {
817       *addr.sun_path = '\0';
818       strncpy (addr.sun_path + 1, socket_path + 1,
819                sizeof (addr.sun_path) - 2);
820     }
821   else
822     {
823       strncpy (addr.sun_path, socket_path, sizeof (addr.sun_path) - 1);
824       unlink (socket_path);
825     }
826
827   if (bind (pm->socket_fd, (struct sockaddr *) &addr, sizeof (addr)) == -1)
828     {
829       return clib_error_return (0, "bind error");
830     }
831
832   int n_bytes = 0x10000;
833
834   if (setsockopt
835       (pm->socket_fd, SOL_SOCKET, SO_SNDBUF, &n_bytes,
836        sizeof (n_bytes)) == -1)
837     {
838       return clib_error_return (0, "setsockopt error");
839     }
840
841   /* Register socket */
842   clib_file_main_t *fm = &file_main;
843   clib_file_t template = { 0 };
844   template.read_function = punt_socket_read_ready;
845   template.file_descriptor = pm->socket_fd;
846   template.description = format (0, "%s", socket_path);
847   pm->clib_file_index = clib_file_add (fm, &template);
848
849   pm->is_configured = true;
850
851   return 0;
852 }
853
854 VLIB_CONFIG_FUNCTION (punt_config, "punt");
855
856 /*
857  * fd.io coding-style-patch-verification: ON
858  *
859  * Local Variables:
860  * eval: (c-set-style "gnu")
861  * End:
862  */