blob: e8495caad610576570445b12126580bc16919d49 [file] [log] [blame]
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -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 Definitions for punt infrastructure.
19 */
20#ifndef included_punt_h
21#define included_punt_h
22
Tom Jones078affa2024-01-29 15:23:23 +000023#ifdef __linux__
Pavel Kotuceke88865d2018-11-28 07:42:11 +010024#include <linux/un.h>
Tom Jones078affa2024-01-29 15:23:23 +000025#elif __FreeBSD__
26#include <sys/un.h>
27#define UNIX_PATH_MAX SUNPATHLEN
28#endif /* __linux__ */
Neale Rannsb8d44812017-11-10 06:53:54 -080029#include <stdbool.h>
Neale Ranns50f0ac02019-05-15 02:13:37 -070030#include <vnet/ip/ip.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080031
Mohammed Hawari45723b82021-02-05 15:40:00 +010032/* Punting reason flags bitfield
33 * (position, length, value, name, string)
34 */
35#define foreach_vnet_punt_reason_flag \
36 _ (0, 1, 0, IP4_PACKET, "ip4-packet") \
37 _ (0, 1, 1, IP6_PACKET, "ip6-packet")
38
39typedef enum vnet_punt_reason_flag_t_
40{
41#define _(pos, len, value, name, str) \
42 VNET_PUNT_REASON_F_##name = ((value) << (pos)),
43 foreach_vnet_punt_reason_flag
44#undef _
45} __clib_packed vnet_punt_reason_flag_t;
46
47enum vnet_punt_reason_flag_mask_t_
48{
49#define _(pos, len, value, name, str) \
50 VNET_PUNT_REASON_F_MASK_##name = (((1 << (len)) - 1) << (pos)),
51 foreach_vnet_punt_reason_flag
52#undef _
53};
54
55/* predicates associated with vlib_punt_reason_flag_t*/
56#define _(pos, len, value, name, str) \
57 static_always_inline int vnet_punt_reason_flag_is_##name ( \
58 vnet_punt_reason_flag_t f) \
59 { \
60 return (f & VNET_PUNT_REASON_F_MASK_##name) == VNET_PUNT_REASON_F_##name; \
61 }
62foreach_vnet_punt_reason_flag
63#undef _
64
Neale Rannsb538dd82019-05-21 06:54:54 -070065#define foreach_punt_type \
66 _(L4, "l4") \
67 _(EXCEPTION, "exception") \
68 _(IP_PROTO, "ip-proto")
Neale Ranns50f0ac02019-05-15 02:13:37 -070069
Mohammed Hawari45723b82021-02-05 15:40:00 +010070 typedef enum punt_type_t_ {
Neale Ranns50f0ac02019-05-15 02:13:37 -070071#define _(v, s) PUNT_TYPE_##v,
Mohammed Hawari45723b82021-02-05 15:40:00 +010072 foreach_punt_type
Neale Ranns50f0ac02019-05-15 02:13:37 -070073#undef _
Mohammed Hawari45723b82021-02-05 15:40:00 +010074 } punt_type_t;
Neale Ranns50f0ac02019-05-15 02:13:37 -070075
76typedef struct punt_l4_t_
77{
78 ip_address_family_t af;
79 ip_protocol_t protocol;
80 u16 port;
81} punt_l4_t;
82
Neale Rannsb538dd82019-05-21 06:54:54 -070083typedef struct punt_ip_proto_t_
84{
85 ip_address_family_t af;
86 ip_protocol_t protocol;
87} punt_ip_proto_t;
88
Neale Ranns50f0ac02019-05-15 02:13:37 -070089typedef struct punt_exception_t_
90{
91 vlib_punt_reason_t reason;
92} punt_exception_t;
93
94typedef struct punt_union_t_
95{
96 punt_exception_t exception;
97 punt_l4_t l4;
Neale Rannsb538dd82019-05-21 06:54:54 -070098 punt_ip_proto_t ip_proto;
Neale Ranns50f0ac02019-05-15 02:13:37 -070099} punt_union_t;
100
101typedef struct punt_reg_t_
102{
103 punt_type_t type;
104 punt_union_t punt;
105} punt_reg_t;
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700106
107
Neale Ranns50f0ac02019-05-15 02:13:37 -0700108clib_error_t *vnet_punt_add_del (vlib_main_t * vm,
109 const punt_reg_t * pr, bool is_add);
110clib_error_t *vnet_punt_socket_add (vlib_main_t * vm,
111 u32 header_version,
112 const punt_reg_t * pr,
Ole Troanf7a55ad2017-05-16 14:59:29 +0200113 char *client_pathname);
Neale Ranns50f0ac02019-05-15 02:13:37 -0700114clib_error_t *vnet_punt_socket_del (vlib_main_t * vm, const punt_reg_t * pr);
Ole Troanf7a55ad2017-05-16 14:59:29 +0200115char *vnet_punt_get_server_pathname (void);
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700116
Ole Troanf7a55ad2017-05-16 14:59:29 +0200117enum punt_action_e
118{
119 PUNT_L2 = 0,
120 PUNT_IP4_ROUTED,
121 PUNT_IP6_ROUTED,
122};
123
124/*
125 * Packet descriptor header. Version 1
126 * If this header changes, the version must also change to notify clients.
127 */
128#define PUNT_PACKETDESC_VERSION 1
129typedef struct __attribute__ ((packed))
130{
131 u32 sw_if_index; /* RX or TX interface */
132 enum punt_action_e action;
133} punt_packetdesc_t;
134
135/*
136 * Client registration
137 */
138typedef struct
139{
Neale Ranns50f0ac02019-05-15 02:13:37 -0700140 punt_reg_t reg;
Ole Troanf7a55ad2017-05-16 14:59:29 +0200141 struct sockaddr_un caddr;
142} punt_client_t;
143
Neale Ranns50f0ac02019-05-15 02:13:37 -0700144typedef struct punt_client_db_t_
145{
146 void *clients_by_l4_port;
147 u32 *clients_by_exception;
Neale Rannsb538dd82019-05-21 06:54:54 -0700148 void *clients_by_ip_proto;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700149} punt_client_db_t;
150
Neale Ranns39040a62019-07-10 01:47:15 -0700151typedef struct punt_thread_data_t_
152{
153 struct iovec *iovecs;
154} punt_thread_data_t;
155
Ole Troanf7a55ad2017-05-16 14:59:29 +0200156typedef struct
157{
158 int socket_fd;
159 char sun_path[sizeof (struct sockaddr_un)];
Neale Ranns50f0ac02019-05-15 02:13:37 -0700160 punt_client_db_t db;
161 punt_client_t *punt_client_pool;
Damjan Marion56dd5432017-09-08 19:52:02 +0200162 u32 clib_file_index;
Ole Troanf7a55ad2017-05-16 14:59:29 +0200163 bool is_configured;
164 vlib_node_t *interface_output_node;
165 u32 *ready_fds;
166 u32 *rx_buffers;
Neale Ranns39040a62019-07-10 01:47:15 -0700167 punt_thread_data_t *thread_data;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700168 vlib_punt_hdl_t hdl;
Filip Tehlarce74c6f2021-06-22 14:10:41 +0000169 u16 msg_id_base;
Ole Troanf7a55ad2017-05-16 14:59:29 +0200170} punt_main_t;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700171
Ole Troanf7a55ad2017-05-16 14:59:29 +0200172extern punt_main_t punt_main;
173
Neale Ranns50f0ac02019-05-15 02:13:37 -0700174typedef walk_rc_t (*punt_client_walk_cb_t) (const punt_client_t * pc,
175 void *ctx);
176extern void punt_client_walk (punt_type_t pt,
177 punt_client_walk_cb_t cb, void *ctx);
Pavel Kotuceke88865d2018-11-28 07:42:11 +0100178
Mohammed Hawari45723b82021-02-05 15:40:00 +0100179extern u8 *format_vnet_punt_reason_flags (u8 *s, va_list *args);
180
Neale Ranns50f0ac02019-05-15 02:13:37 -0700181/*
182 * inlines for the data-plane
183 */
184static_always_inline u32
185punt_client_l4_mk_key (ip_address_family_t af, u16 port)
186{
187 return (af << BITS (port) | port);
188}
189
190static_always_inline punt_client_t *
191punt_client_l4_get (ip_address_family_t af, u16 port)
192{
193 punt_main_t *pm = &punt_main;
194 uword *p;
195
196 p = hash_get (pm->db.clients_by_l4_port, punt_client_l4_mk_key (af, port));
197
198 if (p)
199 return (pool_elt_at_index (pm->punt_client_pool, p[0]));
200
201 return (NULL);
202}
203
Neale Rannsb538dd82019-05-21 06:54:54 -0700204static_always_inline u32
205punt_client_ip_proto_mk_key (ip_address_family_t af, ip_protocol_t proto)
206{
207 return (af << 16 | proto);
208}
209
210static_always_inline punt_client_t *
211punt_client_ip_proto_get (ip_address_family_t af, ip_protocol_t proto)
212{
213 punt_main_t *pm = &punt_main;
214 uword *p;
215
216 p =
217 hash_get (pm->db.clients_by_ip_proto,
218 punt_client_ip_proto_mk_key (af, proto));
219
220 if (p)
221 return (pool_elt_at_index (pm->punt_client_pool, p[0]));
222
223 return (NULL);
224}
225
Neale Ranns50f0ac02019-05-15 02:13:37 -0700226static_always_inline punt_client_t *
227punt_client_exception_get (vlib_punt_reason_t reason)
228{
229 punt_main_t *pm = &punt_main;
230 u32 pci;
231
232 if (reason >= vec_len (pm->db.clients_by_exception))
233 return (NULL);
234
235 pci = pm->db.clients_by_exception[reason];
236
237 if (~0 != pci)
238 return (pool_elt_at_index (pm->punt_client_pool, pci));
239
240 return (NULL);
241}
242
243extern vlib_node_registration_t udp4_punt_node;
244extern vlib_node_registration_t udp6_punt_node;
245extern vlib_node_registration_t udp4_punt_socket_node;
246extern vlib_node_registration_t udp6_punt_socket_node;
Ole Troan56b8abc2023-09-05 08:27:53 +0200247extern vlib_node_registration_t icmp6_punt_socket_node;
Neale Rannsb538dd82019-05-21 06:54:54 -0700248extern vlib_node_registration_t ip4_proto_punt_socket_node;
249extern vlib_node_registration_t ip6_proto_punt_socket_node;
Neale Ranns50f0ac02019-05-15 02:13:37 -0700250extern vlib_node_registration_t punt_socket_rx_node;
251
Ole Troanf7a55ad2017-05-16 14:59:29 +0200252#endif
Alexander Popovsky (apopovsk)4a7e58b2016-10-05 22:31:23 -0700253
254/*
255 * fd.io coding-style-patch-verification: ON
256 *
257 * Local Variables:
258 * eval: (c-set-style "gnu")
259 * End:
260 */