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