Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * flow_api.c - flow api |
| 4 | * |
| 5 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <vnet/vnet.h> |
| 21 | #include <vlibmemory/api.h> |
Jakub Grajciar | 2f71a88 | 2019-10-10 14:21:22 +0200 | [diff] [blame] | 22 | #include <vnet/ip/ip_types_api.h> |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 23 | #include <vnet/udp/udp_local.h> |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 24 | |
| 25 | #include <vnet/interface.h> |
| 26 | #include <vnet/api_errno.h> |
| 27 | |
| 28 | #include <vnet/fib/fib_table.h> |
Ole Troan | a9855ef | 2018-05-02 12:45:10 +0200 | [diff] [blame] | 29 | #include <vnet/ipfix-export/flow_report.h> |
| 30 | #include <vnet/ipfix-export/flow_report_classify.h> |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 31 | |
Filip Tehlar | 53dea27 | 2021-06-21 10:57:49 +0000 | [diff] [blame] | 32 | #include <vnet/format_fns.h> |
| 33 | #include <vnet/ipfix-export/ipfix_export.api_enum.h> |
| 34 | #include <vnet/ipfix-export/ipfix_export.api_types.h> |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 35 | |
Filip Tehlar | 53dea27 | 2021-06-21 10:57:49 +0000 | [diff] [blame] | 36 | #define REPLY_MSG_ID_BASE frm->msg_id_base |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 37 | #include <vlibapi/api_helper_macros.h> |
| 38 | |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 39 | ipfix_exporter_t * |
| 40 | vnet_ipfix_exporter_lookup (ip4_address_t *ipfix_collector) |
| 41 | { |
| 42 | flow_report_main_t *frm = &flow_report_main; |
| 43 | ipfix_exporter_t *exp; |
| 44 | |
| 45 | pool_foreach (exp, frm->exporters) |
| 46 | { |
| 47 | if (exp->ipfix_collector.as_u32 == ipfix_collector->as_u32) |
| 48 | return exp; |
| 49 | } |
| 50 | |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * For backwards compatibility reasons index 0 in the set of exporters |
| 56 | * is alwyas used for the exporter created via the set_ipfix_exporter |
| 57 | * API. |
| 58 | */ |
| 59 | #define USE_INDEX_0 true |
| 60 | #define USE_ANY_INDEX false |
| 61 | |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 62 | static int |
| 63 | vl_api_set_ipfix_exporter_t_internal ( |
| 64 | u32 client_index, vl_api_address_t *mp_collector_address, |
| 65 | u16 mp_collector_port, vl_api_address_t *mp_src_address, u32 mp_vrf_id, |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 66 | u32 mp_path_mtu, u32 mp_template_interval, bool mp_udp_checksum, |
| 67 | bool use_index_0, bool is_create) |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 68 | { |
| 69 | vlib_main_t *vm = vlib_get_main (); |
| 70 | flow_report_main_t *frm = &flow_report_main; |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 71 | ipfix_exporter_t *exp; |
Paul Vinciguerra | 21b83e9 | 2019-06-24 09:55:46 -0400 | [diff] [blame] | 72 | vl_api_registration_t *reg; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 73 | ip4_address_t collector, src; |
| 74 | u16 collector_port = UDP_DST_PORT_ipfix; |
| 75 | u32 path_mtu; |
| 76 | u32 template_interval; |
| 77 | u8 udp_checksum; |
| 78 | u32 fib_id; |
| 79 | u32 fib_index = ~0; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 80 | |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 81 | reg = vl_api_client_index_to_registration (client_index); |
Paul Vinciguerra | 21b83e9 | 2019-06-24 09:55:46 -0400 | [diff] [blame] | 82 | if (!reg) |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 83 | return VNET_API_ERROR_UNIMPLEMENTED; |
Paul Vinciguerra | 21b83e9 | 2019-06-24 09:55:46 -0400 | [diff] [blame] | 84 | |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 85 | /* Collector address is the key for the exporter lookup */ |
| 86 | ip4_address_decode (mp_collector_address->un.ip4, &collector); |
| 87 | |
| 88 | if (use_index_0) |
| 89 | /* |
| 90 | * In this case we update the existing exporter. There is no delete |
| 91 | * for exp[0] |
| 92 | */ |
| 93 | exp = &frm->exporters[0]; |
| 94 | else |
| 95 | { |
| 96 | if (is_create) |
| 97 | { |
| 98 | exp = vnet_ipfix_exporter_lookup (&collector); |
| 99 | if (!exp) |
| 100 | { |
| 101 | /* Create a new exporter instead of updating an existing one */ |
| 102 | if (pool_elts (frm->exporters) >= IPFIX_EXPORTERS_MAX) |
| 103 | return VNET_API_ERROR_INVALID_VALUE; |
| 104 | pool_get (frm->exporters, exp); |
| 105 | if (!exp) |
| 106 | return VNET_API_ERROR_INVALID_VALUE; |
| 107 | } |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | /* Delete the exporter */ |
| 112 | exp = vnet_ipfix_exporter_lookup (&collector); |
| 113 | if (!exp) |
| 114 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 115 | |
| 116 | pool_put (frm->exporters, exp); |
| 117 | return 0; |
| 118 | } |
| 119 | } |
| 120 | |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 121 | if (mp_src_address->af == ADDRESS_IP6 || |
| 122 | mp_collector_address->af == ADDRESS_IP6) |
Jakub Grajciar | 2f71a88 | 2019-10-10 14:21:22 +0200 | [diff] [blame] | 123 | { |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 124 | return VNET_API_ERROR_UNIMPLEMENTED; |
Jakub Grajciar | 2f71a88 | 2019-10-10 14:21:22 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 127 | collector_port = ntohs (mp_collector_port); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 128 | if (collector_port == (u16) ~ 0) |
| 129 | collector_port = UDP_DST_PORT_ipfix; |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 130 | ip4_address_decode (mp_src_address->un.ip4, &src); |
| 131 | fib_id = ntohl (mp_vrf_id); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 132 | |
| 133 | ip4_main_t *im = &ip4_main; |
| 134 | if (fib_id == ~0) |
| 135 | { |
| 136 | fib_index = ~0; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | uword *p = hash_get (im->fib_index_by_table_id, fib_id); |
| 141 | if (!p) |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 142 | return VNET_API_ERROR_NO_SUCH_FIB; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 143 | fib_index = p[0]; |
| 144 | } |
| 145 | |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 146 | path_mtu = ntohl (mp_path_mtu); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 147 | if (path_mtu == ~0) |
| 148 | path_mtu = 512; // RFC 7011 section 10.3.3. |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 149 | template_interval = ntohl (mp_template_interval); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 150 | if (template_interval == ~0) |
| 151 | template_interval = 20; |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 152 | udp_checksum = mp_udp_checksum; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 153 | |
Alexander Chernavin | 67ec752 | 2020-10-01 08:57:59 -0400 | [diff] [blame] | 154 | if (collector.as_u32 != 0 && src.as_u32 == 0) |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 155 | return VNET_API_ERROR_INVALID_VALUE; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 156 | |
| 157 | if (path_mtu > 1450 /* vpp does not support fragmentation */ ) |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 158 | return VNET_API_ERROR_INVALID_VALUE; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 159 | |
| 160 | if (path_mtu < 68) |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 161 | return VNET_API_ERROR_INVALID_VALUE; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 162 | |
Paul Atkins | 19a5f23 | 2021-09-27 21:30:13 +0100 | [diff] [blame] | 163 | /* Calculate how much header data we need. */ |
| 164 | exp->all_headers_size = sizeof (ip4_header_t) + sizeof (udp_header_t) + |
| 165 | sizeof (ipfix_message_header_t) + |
| 166 | sizeof (ipfix_set_header_t); |
| 167 | |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 168 | /* Reset report streams if we are reconfiguring IP addresses */ |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 169 | if (exp->ipfix_collector.as_u32 != collector.as_u32 || |
| 170 | exp->src_address.as_u32 != src.as_u32 || |
| 171 | exp->collector_port != collector_port) |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 172 | vnet_flow_reports_reset (exp); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 173 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 174 | exp->ipfix_collector.as_u32 = collector.as_u32; |
| 175 | exp->collector_port = collector_port; |
| 176 | exp->src_address.as_u32 = src.as_u32; |
| 177 | exp->fib_index = fib_index; |
| 178 | exp->path_mtu = path_mtu; |
| 179 | exp->template_interval = template_interval; |
| 180 | exp->udp_checksum = udp_checksum; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 181 | |
| 182 | /* Turn on the flow reporting process */ |
| 183 | vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0); |
| 184 | |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static void |
| 189 | vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t *mp) |
| 190 | { |
| 191 | vl_api_set_ipfix_exporter_reply_t *rmp; |
| 192 | flow_report_main_t *frm = &flow_report_main; |
| 193 | int rv = vl_api_set_ipfix_exporter_t_internal ( |
| 194 | mp->client_index, &mp->collector_address, mp->collector_port, |
| 195 | &mp->src_address, mp->vrf_id, mp->path_mtu, mp->template_interval, |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 196 | mp->udp_checksum, USE_INDEX_0, 0); |
Paul Atkins | a6e131e | 2021-09-22 14:18:45 +0100 | [diff] [blame] | 197 | |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 198 | REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY); |
| 199 | } |
| 200 | |
| 201 | static void |
Paul Atkins | d747dd9 | 2021-09-22 14:56:17 +0100 | [diff] [blame] | 202 | vl_api_ipfix_exporter_create_delete_t_handler ( |
| 203 | vl_api_ipfix_exporter_create_delete_t *mp) |
| 204 | { |
| 205 | vl_api_ipfix_exporter_create_delete_reply_t *rmp; |
| 206 | flow_report_main_t *frm = &flow_report_main; |
| 207 | int rv = vl_api_set_ipfix_exporter_t_internal ( |
| 208 | mp->client_index, &mp->collector_address, mp->collector_port, |
| 209 | &mp->src_address, mp->vrf_id, mp->path_mtu, mp->template_interval, |
| 210 | mp->udp_checksum, USE_ANY_INDEX, mp->is_create); |
| 211 | |
| 212 | REPLY_MACRO (VL_API_IPFIX_EXPORTER_CREATE_DELETE_REPLY); |
| 213 | } |
| 214 | |
| 215 | static void |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 216 | vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp) |
| 217 | { |
| 218 | flow_report_main_t *frm = &flow_report_main; |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 219 | ipfix_exporter_t *exp = pool_elt_at_index (flow_report_main.exporters, 0); |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 220 | vl_api_registration_t *reg; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 221 | vl_api_ipfix_exporter_details_t *rmp; |
| 222 | ip4_main_t *im = &ip4_main; |
Jakub Grajciar | 2f71a88 | 2019-10-10 14:21:22 +0200 | [diff] [blame] | 223 | ip46_address_t collector = {.as_u64[0] = 0,.as_u64[1] = 0 }; |
| 224 | ip46_address_t src = {.as_u64[0] = 0,.as_u64[1] = 0 }; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 225 | u32 vrf_id; |
| 226 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 227 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 228 | if (!reg) |
Paul Vinciguerra | 21b83e9 | 2019-06-24 09:55:46 -0400 | [diff] [blame] | 229 | return; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 230 | |
| 231 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 232 | clib_memset (rmp, 0, sizeof (*rmp)); |
Paul Atkins | 5df0b34 | 2021-09-23 10:55:25 +0100 | [diff] [blame] | 233 | rmp->_vl_msg_id = |
| 234 | ntohs ((REPLY_MSG_ID_BASE) + VL_API_IPFIX_EXPORTER_DETAILS); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 235 | rmp->context = mp->context; |
Jakub Grajciar | 2f71a88 | 2019-10-10 14:21:22 +0200 | [diff] [blame] | 236 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 237 | memcpy (&collector.ip4, &exp->ipfix_collector, sizeof (ip4_address_t)); |
Jakub Grajciar | 2f71a88 | 2019-10-10 14:21:22 +0200 | [diff] [blame] | 238 | ip_address_encode (&collector, IP46_TYPE_IP4, &rmp->collector_address); |
| 239 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 240 | rmp->collector_port = htons (exp->collector_port); |
Jakub Grajciar | 2f71a88 | 2019-10-10 14:21:22 +0200 | [diff] [blame] | 241 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 242 | memcpy (&src.ip4, &exp->src_address, sizeof (ip4_address_t)); |
Jakub Grajciar | 2f71a88 | 2019-10-10 14:21:22 +0200 | [diff] [blame] | 243 | ip_address_encode (&src, IP46_TYPE_IP4, &rmp->src_address); |
| 244 | |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 245 | if (exp->fib_index == ~0) |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 246 | vrf_id = ~0; |
| 247 | else |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 248 | vrf_id = im->fibs[exp->fib_index].ft_table_id; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 249 | rmp->vrf_id = htonl (vrf_id); |
Paul Atkins | 9ec6449 | 2021-09-21 20:49:12 +0100 | [diff] [blame] | 250 | rmp->path_mtu = htonl (exp->path_mtu); |
| 251 | rmp->template_interval = htonl (exp->template_interval); |
| 252 | rmp->udp_checksum = (exp->udp_checksum != 0); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 253 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 254 | vl_api_send_msg (reg, (u8 *) rmp); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void |
Paul Atkins | acb0d2d | 2021-09-23 09:28:50 +0100 | [diff] [blame] | 258 | ipfix_all_fill_details (vl_api_ipfix_all_exporter_details_t *rmp, |
| 259 | ipfix_exporter_t *exp) |
| 260 | { |
| 261 | ip4_main_t *im = &ip4_main; |
| 262 | ip46_address_t collector = { .as_u64[0] = 0, .as_u64[1] = 0 }; |
| 263 | ip46_address_t src = { .as_u64[0] = 0, .as_u64[1] = 0 }; |
| 264 | u32 vrf_id; |
| 265 | |
| 266 | memcpy (&collector.ip4, &exp->ipfix_collector, sizeof (ip4_address_t)); |
| 267 | ip_address_encode (&collector, IP46_TYPE_IP4, &rmp->collector_address); |
| 268 | |
| 269 | rmp->collector_port = htons (exp->collector_port); |
| 270 | |
| 271 | memcpy (&src.ip4, &exp->src_address, sizeof (ip4_address_t)); |
| 272 | ip_address_encode (&src, IP46_TYPE_IP4, &rmp->src_address); |
| 273 | |
| 274 | if (exp->fib_index == ~0) |
| 275 | vrf_id = ~0; |
| 276 | else |
| 277 | vrf_id = im->fibs[exp->fib_index].ft_table_id; |
| 278 | rmp->vrf_id = htonl (vrf_id); |
| 279 | rmp->path_mtu = htonl (exp->path_mtu); |
| 280 | rmp->template_interval = htonl (exp->template_interval); |
| 281 | rmp->udp_checksum = (exp->udp_checksum != 0); |
| 282 | } |
| 283 | |
| 284 | static void |
| 285 | ipfix_all_exporter_details (flow_report_main_t *frm, u32 index, |
| 286 | vl_api_registration_t *rp, u32 context) |
| 287 | { |
| 288 | ipfix_exporter_t *exp = pool_elt_at_index (frm->exporters, index); |
| 289 | |
| 290 | vl_api_ipfix_all_exporter_details_t *rmp; |
| 291 | |
| 292 | REPLY_MACRO_DETAILS4 (VL_API_IPFIX_ALL_EXPORTER_DETAILS, rp, context, |
| 293 | ({ ipfix_all_fill_details (rmp, exp); })); |
| 294 | } |
| 295 | |
| 296 | static void |
| 297 | vl_api_ipfix_all_exporter_get_t_handler (vl_api_ipfix_all_exporter_get_t *mp) |
| 298 | { |
| 299 | flow_report_main_t *frm = &flow_report_main; |
| 300 | vl_api_ipfix_all_exporter_get_reply_t *rmp; |
| 301 | int rv = 0; |
| 302 | |
| 303 | REPLY_AND_DETAILS_MACRO ( |
| 304 | VL_API_IPFIX_ALL_EXPORTER_GET_REPLY, frm->exporters, |
| 305 | ({ ipfix_all_exporter_details (frm, cursor, rp, mp->context); })); |
| 306 | } |
| 307 | |
| 308 | static void |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 309 | vl_api_set_ipfix_classify_stream_t_handler |
| 310 | (vl_api_set_ipfix_classify_stream_t * mp) |
| 311 | { |
| 312 | vl_api_set_ipfix_classify_stream_reply_t *rmp; |
| 313 | flow_report_classify_main_t *fcm = &flow_report_classify_main; |
| 314 | flow_report_main_t *frm = &flow_report_main; |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 315 | ipfix_exporter_t *exp = &frm->exporters[0]; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 316 | u32 domain_id = 0; |
| 317 | u32 src_port = UDP_DST_PORT_ipfix; |
| 318 | int rv = 0; |
| 319 | |
| 320 | domain_id = ntohl (mp->domain_id); |
| 321 | src_port = ntohs (mp->src_port); |
| 322 | |
| 323 | if (fcm->src_port != 0 && |
| 324 | (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port)) |
| 325 | { |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 326 | int rv = vnet_stream_change (exp, fcm->domain_id, fcm->src_port, |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 327 | domain_id, (u16) src_port); |
| 328 | ASSERT (rv == 0); |
| 329 | } |
| 330 | |
| 331 | fcm->domain_id = domain_id; |
| 332 | fcm->src_port = (u16) src_port; |
| 333 | |
| 334 | REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY); |
| 335 | } |
| 336 | |
| 337 | static void |
| 338 | vl_api_ipfix_classify_stream_dump_t_handler |
| 339 | (vl_api_ipfix_classify_stream_dump_t * mp) |
| 340 | { |
| 341 | flow_report_classify_main_t *fcm = &flow_report_classify_main; |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 342 | vl_api_registration_t *reg; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 343 | vl_api_ipfix_classify_stream_details_t *rmp; |
| 344 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 345 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 346 | if (!reg) |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 347 | return; |
| 348 | |
| 349 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 350 | clib_memset (rmp, 0, sizeof (*rmp)); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 351 | rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS); |
| 352 | rmp->context = mp->context; |
| 353 | rmp->domain_id = htonl (fcm->domain_id); |
| 354 | rmp->src_port = htons (fcm->src_port); |
| 355 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 356 | vl_api_send_msg (reg, (u8 *) rmp); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static void |
| 360 | vl_api_ipfix_classify_table_add_del_t_handler |
| 361 | (vl_api_ipfix_classify_table_add_del_t * mp) |
| 362 | { |
| 363 | vl_api_ipfix_classify_table_add_del_reply_t *rmp; |
Paul Vinciguerra | 21b83e9 | 2019-06-24 09:55:46 -0400 | [diff] [blame] | 364 | vl_api_registration_t *reg; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 365 | flow_report_classify_main_t *fcm = &flow_report_classify_main; |
| 366 | flow_report_main_t *frm = &flow_report_main; |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 367 | ipfix_exporter_t *exp = &frm->exporters[0]; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 368 | vnet_flow_report_add_del_args_t args; |
| 369 | ipfix_classify_table_t *table; |
| 370 | int is_add; |
| 371 | u32 classify_table_index; |
| 372 | u8 ip_version; |
| 373 | u8 transport_protocol; |
| 374 | int rv = 0; |
| 375 | |
Paul Vinciguerra | 21b83e9 | 2019-06-24 09:55:46 -0400 | [diff] [blame] | 376 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 377 | if (!reg) |
| 378 | return; |
| 379 | |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 380 | classify_table_index = ntohl (mp->table_id); |
Alexander Chernavin | f6cf57c | 2020-09-30 10:36:10 -0400 | [diff] [blame] | 381 | ip_version = (mp->ip_version == ADDRESS_IP4) ? 4 : 6; |
| 382 | transport_protocol = mp->transport_protocol; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 383 | is_add = mp->is_add; |
| 384 | |
| 385 | if (fcm->src_port == 0) |
| 386 | { |
| 387 | /* call set_ipfix_classify_stream first */ |
| 388 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 389 | goto out; |
| 390 | } |
| 391 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 392 | clib_memset (&args, 0, sizeof (args)); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 393 | |
| 394 | table = 0; |
| 395 | int i; |
| 396 | for (i = 0; i < vec_len (fcm->tables); i++) |
| 397 | if (ipfix_classify_table_index_valid (i)) |
| 398 | if (fcm->tables[i].classify_table_index == classify_table_index) |
| 399 | { |
| 400 | table = &fcm->tables[i]; |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | if (is_add) |
| 405 | { |
| 406 | if (table) |
| 407 | { |
| 408 | rv = VNET_API_ERROR_VALUE_EXIST; |
| 409 | goto out; |
| 410 | } |
| 411 | table = ipfix_classify_add_table (); |
| 412 | table->classify_table_index = classify_table_index; |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | if (!table) |
| 417 | { |
| 418 | rv = VNET_API_ERROR_NO_SUCH_ENTRY; |
| 419 | goto out; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | table->ip_version = ip_version; |
| 424 | table->transport_protocol = transport_protocol; |
| 425 | |
| 426 | args.opaque.as_uword = table - fcm->tables; |
| 427 | args.rewrite_callback = ipfix_classify_template_rewrite; |
| 428 | args.flow_data_callback = ipfix_classify_send_flows; |
| 429 | args.is_add = is_add; |
| 430 | args.domain_id = fcm->domain_id; |
| 431 | args.src_port = fcm->src_port; |
| 432 | |
Paul Atkins | 40f9a7a | 2021-09-22 10:06:23 +0100 | [diff] [blame] | 433 | rv = vnet_flow_report_add_del (exp, &args, NULL); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 434 | |
| 435 | /* If deleting, or add failed */ |
| 436 | if (is_add == 0 || (rv && is_add)) |
| 437 | ipfix_classify_delete_table (table - fcm->tables); |
| 438 | |
| 439 | out: |
| 440 | REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY); |
| 441 | } |
| 442 | |
| 443 | static void |
| 444 | send_ipfix_classify_table_details (u32 table_index, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 445 | vl_api_registration_t * reg, u32 context) |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 446 | { |
| 447 | flow_report_classify_main_t *fcm = &flow_report_classify_main; |
| 448 | vl_api_ipfix_classify_table_details_t *mp; |
| 449 | |
| 450 | ipfix_classify_table_t *table = &fcm->tables[table_index]; |
| 451 | |
| 452 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 453 | clib_memset (mp, 0, sizeof (*mp)); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 454 | mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS); |
| 455 | mp->context = context; |
| 456 | mp->table_id = htonl (table->classify_table_index); |
Alexander Chernavin | f6cf57c | 2020-09-30 10:36:10 -0400 | [diff] [blame] | 457 | mp->ip_version = (table->ip_version == 4) ? ADDRESS_IP4 : ADDRESS_IP6; |
| 458 | mp->transport_protocol = table->transport_protocol; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 459 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 460 | vl_api_send_msg (reg, (u8 *) mp); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static void |
| 464 | vl_api_ipfix_classify_table_dump_t_handler |
| 465 | (vl_api_ipfix_classify_table_dump_t * mp) |
| 466 | { |
| 467 | flow_report_classify_main_t *fcm = &flow_report_classify_main; |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 468 | vl_api_registration_t *reg; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 469 | u32 i; |
| 470 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 471 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 472 | if (!reg) |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 473 | return; |
| 474 | |
| 475 | for (i = 0; i < vec_len (fcm->tables); i++) |
| 476 | if (ipfix_classify_table_index_valid (i)) |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 477 | send_ipfix_classify_table_details (i, reg, mp->context); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 478 | } |
| 479 | |
Paul Vinciguerra | 21b83e9 | 2019-06-24 09:55:46 -0400 | [diff] [blame] | 480 | static void |
| 481 | vl_api_ipfix_flush_t_handler (vl_api_ipfix_flush_t * mp) |
| 482 | { |
Filip Tehlar | 53dea27 | 2021-06-21 10:57:49 +0000 | [diff] [blame] | 483 | flow_report_main_t *frm = &flow_report_main; |
Paul Vinciguerra | 21b83e9 | 2019-06-24 09:55:46 -0400 | [diff] [blame] | 484 | vl_api_ipfix_flush_reply_t *rmp; |
| 485 | vl_api_registration_t *reg; |
| 486 | vlib_main_t *vm = vlib_get_main (); |
| 487 | int rv = 0; |
| 488 | |
| 489 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 490 | if (!reg) |
| 491 | return; |
| 492 | |
| 493 | /* poke the flow reporting process */ |
| 494 | vlib_process_signal_event (vm, flow_report_process_node.index, |
| 495 | 1 /* type_opaque */ , 0 /* data */ ); |
| 496 | |
| 497 | REPLY_MACRO (VL_API_IPFIX_FLUSH_REPLY); |
| 498 | } |
| 499 | |
Filip Tehlar | 53dea27 | 2021-06-21 10:57:49 +0000 | [diff] [blame] | 500 | #include <vnet/ipfix-export/ipfix_export.api.c> |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 501 | static clib_error_t * |
| 502 | flow_api_hookup (vlib_main_t * vm) |
| 503 | { |
Filip Tehlar | 53dea27 | 2021-06-21 10:57:49 +0000 | [diff] [blame] | 504 | flow_report_main_t *frm = &flow_report_main; |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 505 | /* |
| 506 | * Set up the (msg_name, crc, message-id) table |
| 507 | */ |
Filip Tehlar | 53dea27 | 2021-06-21 10:57:49 +0000 | [diff] [blame] | 508 | REPLY_MSG_ID_BASE = setup_message_id_table (); |
Pavel Kotucek | eb9e666 | 2017-01-24 13:40:26 +0100 | [diff] [blame] | 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | VLIB_API_INIT_FUNCTION (flow_api_hookup); |
| 514 | |
| 515 | /* |
| 516 | * fd.io coding-style-patch-verification: ON |
| 517 | * |
| 518 | * Local Variables: |
| 519 | * eval: (c-set-style "gnu") |
| 520 | * End: |
| 521 | */ |