Punt: socket register for exception dispatched/punted packets based on reason
[vpp.git] / src / vnet / ip / punt.h
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 Definitions for punt infrastructure.
19  */
20 #ifndef included_punt_h
21 #define included_punt_h
22
23 #include <linux/un.h>
24 #include <stdbool.h>
25 #include <vnet/ip/ip.h>
26
27 #define foreach_punt_type \
28   _(L4, "l4")             \
29   _(EXCEPTION, "exception")
30
31 typedef enum punt_type_t_
32 {
33 #define _(v, s) PUNT_TYPE_##v,
34   foreach_punt_type
35 #undef _
36 } punt_type_t;
37
38 typedef struct punt_l4_t_
39 {
40   ip_address_family_t af;
41   ip_protocol_t protocol;
42   u16 port;
43 } punt_l4_t;
44
45 typedef struct punt_exception_t_
46 {
47   vlib_punt_reason_t reason;
48 } punt_exception_t;
49
50 typedef struct punt_union_t_
51 {
52   punt_exception_t exception;
53   punt_l4_t l4;
54 } punt_union_t;
55
56 typedef struct punt_reg_t_
57 {
58   punt_type_t type;
59   punt_union_t punt;
60 } punt_reg_t;
61
62
63 clib_error_t *vnet_punt_add_del (vlib_main_t * vm,
64                                  const punt_reg_t * pr, bool is_add);
65 clib_error_t *vnet_punt_socket_add (vlib_main_t * vm,
66                                     u32 header_version,
67                                     const punt_reg_t * pr,
68                                     char *client_pathname);
69 clib_error_t *vnet_punt_socket_del (vlib_main_t * vm, const punt_reg_t * pr);
70 char *vnet_punt_get_server_pathname (void);
71
72 enum punt_action_e
73 {
74   PUNT_L2 = 0,
75   PUNT_IP4_ROUTED,
76   PUNT_IP6_ROUTED,
77 };
78
79 /*
80  * Packet descriptor header. Version 1
81  * If this header changes, the version must also change to notify clients.
82  */
83 #define PUNT_PACKETDESC_VERSION 1
84 typedef struct __attribute__ ((packed))
85 {
86   u32 sw_if_index;              /* RX or TX interface */
87   enum punt_action_e action;
88 } punt_packetdesc_t;
89
90 /*
91  * Client registration
92  */
93 typedef struct
94 {
95   punt_reg_t reg;
96   struct sockaddr_un caddr;
97 } punt_client_t;
98
99 typedef struct punt_client_db_t_
100 {
101   void *clients_by_l4_port;
102   u32 *clients_by_exception;
103 } punt_client_db_t;
104
105 typedef struct
106 {
107   int socket_fd;
108   char sun_path[sizeof (struct sockaddr_un)];
109   punt_client_db_t db;
110   punt_client_t *punt_client_pool;
111   u32 clib_file_index;
112   bool is_configured;
113   vlib_node_t *interface_output_node;
114   u32 *ready_fds;
115   u32 *rx_buffers;
116   vlib_punt_hdl_t hdl;
117 } punt_main_t;
118
119 extern punt_main_t punt_main;
120
121 typedef walk_rc_t (*punt_client_walk_cb_t) (const punt_client_t * pc,
122                                             void *ctx);
123 extern void punt_client_walk (punt_type_t pt,
124                               punt_client_walk_cb_t cb, void *ctx);
125
126 /*
127  * inlines for the data-plane
128  */
129 static_always_inline u32
130 punt_client_l4_mk_key (ip_address_family_t af, u16 port)
131 {
132   return (af << BITS (port) | port);
133 }
134
135 static_always_inline punt_client_t *
136 punt_client_l4_get (ip_address_family_t af, u16 port)
137 {
138   punt_main_t *pm = &punt_main;
139   uword *p;
140
141   p = hash_get (pm->db.clients_by_l4_port, punt_client_l4_mk_key (af, port));
142
143   if (p)
144     return (pool_elt_at_index (pm->punt_client_pool, p[0]));
145
146   return (NULL);
147 }
148
149 static_always_inline punt_client_t *
150 punt_client_exception_get (vlib_punt_reason_t reason)
151 {
152   punt_main_t *pm = &punt_main;
153   u32 pci;
154
155   if (reason >= vec_len (pm->db.clients_by_exception))
156     return (NULL);
157
158   pci = pm->db.clients_by_exception[reason];
159
160   if (~0 != pci)
161     return (pool_elt_at_index (pm->punt_client_pool, pci));
162
163   return (NULL);
164 }
165
166 extern vlib_node_registration_t udp4_punt_node;
167 extern vlib_node_registration_t udp6_punt_node;
168 extern vlib_node_registration_t udp4_punt_socket_node;
169 extern vlib_node_registration_t udp6_punt_socket_node;
170 extern vlib_node_registration_t punt_socket_rx_node;
171
172 #endif
173
174 /*
175  * fd.io coding-style-patch-verification: ON
176  *
177  * Local Variables:
178  * eval: (c-set-style "gnu")
179  * End:
180  */