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