Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 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 | |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 23 | #include <linux/un.h> |
Neale Ranns | b8d4481 | 2017-11-10 06:53:54 -0800 | [diff] [blame] | 24 | #include <stdbool.h> |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 25 | #include <vnet/ip/ip.h> |
Neale Ranns | b8d4481 | 2017-11-10 06:53:54 -0800 | [diff] [blame] | 26 | |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 27 | /* Punting reason flags bitfield |
| 28 | * (position, length, value, name, string) |
| 29 | */ |
| 30 | #define foreach_vnet_punt_reason_flag \ |
| 31 | _ (0, 1, 0, IP4_PACKET, "ip4-packet") \ |
| 32 | _ (0, 1, 1, IP6_PACKET, "ip6-packet") |
| 33 | |
| 34 | typedef enum vnet_punt_reason_flag_t_ |
| 35 | { |
| 36 | #define _(pos, len, value, name, str) \ |
| 37 | VNET_PUNT_REASON_F_##name = ((value) << (pos)), |
| 38 | foreach_vnet_punt_reason_flag |
| 39 | #undef _ |
| 40 | } __clib_packed vnet_punt_reason_flag_t; |
| 41 | |
| 42 | enum vnet_punt_reason_flag_mask_t_ |
| 43 | { |
| 44 | #define _(pos, len, value, name, str) \ |
| 45 | VNET_PUNT_REASON_F_MASK_##name = (((1 << (len)) - 1) << (pos)), |
| 46 | foreach_vnet_punt_reason_flag |
| 47 | #undef _ |
| 48 | }; |
| 49 | |
| 50 | /* predicates associated with vlib_punt_reason_flag_t*/ |
| 51 | #define _(pos, len, value, name, str) \ |
| 52 | static_always_inline int vnet_punt_reason_flag_is_##name ( \ |
| 53 | vnet_punt_reason_flag_t f) \ |
| 54 | { \ |
| 55 | return (f & VNET_PUNT_REASON_F_MASK_##name) == VNET_PUNT_REASON_F_##name; \ |
| 56 | } |
| 57 | foreach_vnet_punt_reason_flag |
| 58 | #undef _ |
| 59 | |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 60 | #define foreach_punt_type \ |
| 61 | _(L4, "l4") \ |
| 62 | _(EXCEPTION, "exception") \ |
| 63 | _(IP_PROTO, "ip-proto") |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 64 | |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 65 | typedef enum punt_type_t_ { |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 66 | #define _(v, s) PUNT_TYPE_##v, |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 67 | foreach_punt_type |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 68 | #undef _ |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 69 | } punt_type_t; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 70 | |
| 71 | typedef struct punt_l4_t_ |
| 72 | { |
| 73 | ip_address_family_t af; |
| 74 | ip_protocol_t protocol; |
| 75 | u16 port; |
| 76 | } punt_l4_t; |
| 77 | |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 78 | typedef struct punt_ip_proto_t_ |
| 79 | { |
| 80 | ip_address_family_t af; |
| 81 | ip_protocol_t protocol; |
| 82 | } punt_ip_proto_t; |
| 83 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 84 | typedef struct punt_exception_t_ |
| 85 | { |
| 86 | vlib_punt_reason_t reason; |
| 87 | } punt_exception_t; |
| 88 | |
| 89 | typedef struct punt_union_t_ |
| 90 | { |
| 91 | punt_exception_t exception; |
| 92 | punt_l4_t l4; |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 93 | punt_ip_proto_t ip_proto; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 94 | } punt_union_t; |
| 95 | |
| 96 | typedef struct punt_reg_t_ |
| 97 | { |
| 98 | punt_type_t type; |
| 99 | punt_union_t punt; |
| 100 | } punt_reg_t; |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 101 | |
| 102 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 103 | clib_error_t *vnet_punt_add_del (vlib_main_t * vm, |
| 104 | const punt_reg_t * pr, bool is_add); |
| 105 | clib_error_t *vnet_punt_socket_add (vlib_main_t * vm, |
| 106 | u32 header_version, |
| 107 | const punt_reg_t * pr, |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 108 | char *client_pathname); |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 109 | clib_error_t *vnet_punt_socket_del (vlib_main_t * vm, const punt_reg_t * pr); |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 110 | char *vnet_punt_get_server_pathname (void); |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 111 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 112 | enum punt_action_e |
| 113 | { |
| 114 | PUNT_L2 = 0, |
| 115 | PUNT_IP4_ROUTED, |
| 116 | PUNT_IP6_ROUTED, |
| 117 | }; |
| 118 | |
| 119 | /* |
| 120 | * Packet descriptor header. Version 1 |
| 121 | * If this header changes, the version must also change to notify clients. |
| 122 | */ |
| 123 | #define PUNT_PACKETDESC_VERSION 1 |
| 124 | typedef struct __attribute__ ((packed)) |
| 125 | { |
| 126 | u32 sw_if_index; /* RX or TX interface */ |
| 127 | enum punt_action_e action; |
| 128 | } punt_packetdesc_t; |
| 129 | |
| 130 | /* |
| 131 | * Client registration |
| 132 | */ |
| 133 | typedef struct |
| 134 | { |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 135 | punt_reg_t reg; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 136 | struct sockaddr_un caddr; |
| 137 | } punt_client_t; |
| 138 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 139 | typedef struct punt_client_db_t_ |
| 140 | { |
| 141 | void *clients_by_l4_port; |
| 142 | u32 *clients_by_exception; |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 143 | void *clients_by_ip_proto; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 144 | } punt_client_db_t; |
| 145 | |
Neale Ranns | 39040a6 | 2019-07-10 01:47:15 -0700 | [diff] [blame] | 146 | typedef struct punt_thread_data_t_ |
| 147 | { |
| 148 | struct iovec *iovecs; |
| 149 | } punt_thread_data_t; |
| 150 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 151 | typedef struct |
| 152 | { |
| 153 | int socket_fd; |
| 154 | char sun_path[sizeof (struct sockaddr_un)]; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 155 | punt_client_db_t db; |
| 156 | punt_client_t *punt_client_pool; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 157 | u32 clib_file_index; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 158 | bool is_configured; |
| 159 | vlib_node_t *interface_output_node; |
| 160 | u32 *ready_fds; |
| 161 | u32 *rx_buffers; |
Neale Ranns | 39040a6 | 2019-07-10 01:47:15 -0700 | [diff] [blame] | 162 | punt_thread_data_t *thread_data; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 163 | vlib_punt_hdl_t hdl; |
Filip Tehlar | ce74c6f | 2021-06-22 14:10:41 +0000 | [diff] [blame] | 164 | u16 msg_id_base; |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 165 | } punt_main_t; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 166 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 167 | extern punt_main_t punt_main; |
| 168 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 169 | typedef walk_rc_t (*punt_client_walk_cb_t) (const punt_client_t * pc, |
| 170 | void *ctx); |
| 171 | extern void punt_client_walk (punt_type_t pt, |
| 172 | punt_client_walk_cb_t cb, void *ctx); |
Pavel Kotucek | e88865d | 2018-11-28 07:42:11 +0100 | [diff] [blame] | 173 | |
Mohammed Hawari | 45723b8 | 2021-02-05 15:40:00 +0100 | [diff] [blame] | 174 | extern u8 *format_vnet_punt_reason_flags (u8 *s, va_list *args); |
| 175 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 176 | /* |
| 177 | * inlines for the data-plane |
| 178 | */ |
| 179 | static_always_inline u32 |
| 180 | punt_client_l4_mk_key (ip_address_family_t af, u16 port) |
| 181 | { |
| 182 | return (af << BITS (port) | port); |
| 183 | } |
| 184 | |
| 185 | static_always_inline punt_client_t * |
| 186 | punt_client_l4_get (ip_address_family_t af, u16 port) |
| 187 | { |
| 188 | punt_main_t *pm = &punt_main; |
| 189 | uword *p; |
| 190 | |
| 191 | p = hash_get (pm->db.clients_by_l4_port, punt_client_l4_mk_key (af, port)); |
| 192 | |
| 193 | if (p) |
| 194 | return (pool_elt_at_index (pm->punt_client_pool, p[0])); |
| 195 | |
| 196 | return (NULL); |
| 197 | } |
| 198 | |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 199 | static_always_inline u32 |
| 200 | punt_client_ip_proto_mk_key (ip_address_family_t af, ip_protocol_t proto) |
| 201 | { |
| 202 | return (af << 16 | proto); |
| 203 | } |
| 204 | |
| 205 | static_always_inline punt_client_t * |
| 206 | punt_client_ip_proto_get (ip_address_family_t af, ip_protocol_t proto) |
| 207 | { |
| 208 | punt_main_t *pm = &punt_main; |
| 209 | uword *p; |
| 210 | |
| 211 | p = |
| 212 | hash_get (pm->db.clients_by_ip_proto, |
| 213 | punt_client_ip_proto_mk_key (af, proto)); |
| 214 | |
| 215 | if (p) |
| 216 | return (pool_elt_at_index (pm->punt_client_pool, p[0])); |
| 217 | |
| 218 | return (NULL); |
| 219 | } |
| 220 | |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 221 | static_always_inline punt_client_t * |
| 222 | punt_client_exception_get (vlib_punt_reason_t reason) |
| 223 | { |
| 224 | punt_main_t *pm = &punt_main; |
| 225 | u32 pci; |
| 226 | |
| 227 | if (reason >= vec_len (pm->db.clients_by_exception)) |
| 228 | return (NULL); |
| 229 | |
| 230 | pci = pm->db.clients_by_exception[reason]; |
| 231 | |
| 232 | if (~0 != pci) |
| 233 | return (pool_elt_at_index (pm->punt_client_pool, pci)); |
| 234 | |
| 235 | return (NULL); |
| 236 | } |
| 237 | |
| 238 | extern vlib_node_registration_t udp4_punt_node; |
| 239 | extern vlib_node_registration_t udp6_punt_node; |
| 240 | extern vlib_node_registration_t udp4_punt_socket_node; |
| 241 | extern vlib_node_registration_t udp6_punt_socket_node; |
Neale Ranns | b538dd8 | 2019-05-21 06:54:54 -0700 | [diff] [blame] | 242 | extern vlib_node_registration_t ip4_proto_punt_socket_node; |
| 243 | extern vlib_node_registration_t ip6_proto_punt_socket_node; |
Neale Ranns | 50f0ac0 | 2019-05-15 02:13:37 -0700 | [diff] [blame] | 244 | extern vlib_node_registration_t punt_socket_rx_node; |
| 245 | |
Ole Troan | f7a55ad | 2017-05-16 14:59:29 +0200 | [diff] [blame] | 246 | #endif |
Alexander Popovsky (apopovsk) | 4a7e58b | 2016-10-05 22:31:23 -0700 | [diff] [blame] | 247 | |
| 248 | /* |
| 249 | * fd.io coding-style-patch-verification: ON |
| 250 | * |
| 251 | * Local Variables: |
| 252 | * eval: (c-set-style "gnu") |
| 253 | * End: |
| 254 | */ |