Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * span_api.c - span mirroring 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> |
| 22 | |
| 23 | #include <vnet/interface.h> |
| 24 | #include <vnet/api_errno.h> |
| 25 | #include <vnet/span/span.h> |
| 26 | |
| 27 | #include <vnet/vnet_msg_enum.h> |
| 28 | |
| 29 | #define vl_typedefs /* define message structures */ |
| 30 | #include <vnet/vnet_all_api_h.h> |
| 31 | #undef vl_typedefs |
| 32 | |
| 33 | #define vl_endianfun /* define message structures */ |
| 34 | #include <vnet/vnet_all_api_h.h> |
| 35 | #undef vl_endianfun |
| 36 | |
| 37 | /* instantiate all the print functions we know about */ |
| 38 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 39 | #define vl_printfun |
| 40 | #include <vnet/vnet_all_api_h.h> |
| 41 | #undef vl_printfun |
| 42 | |
| 43 | #include <vlibapi/api_helper_macros.h> |
| 44 | |
| 45 | #define foreach_vpe_api_msg \ |
| 46 | _(SW_INTERFACE_SPAN_ENABLE_DISABLE, sw_interface_span_enable_disable) \ |
| 47 | _(SW_INTERFACE_SPAN_DUMP, sw_interface_span_dump) \ |
| 48 | |
| 49 | static void |
| 50 | vl_api_sw_interface_span_enable_disable_t_handler |
| 51 | (vl_api_sw_interface_span_enable_disable_t * mp) |
| 52 | { |
| 53 | vl_api_sw_interface_span_enable_disable_reply_t *rmp; |
| 54 | int rv; |
| 55 | |
| 56 | vlib_main_t *vm = vlib_get_main (); |
| 57 | |
| 58 | rv = span_add_delete_entry (vm, ntohl (mp->sw_if_index_from), |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame] | 59 | ntohl (mp->sw_if_index_to), mp->state, |
| 60 | mp->is_l2 ? SPAN_FEAT_L2 : SPAN_FEAT_DEVICE); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 61 | |
| 62 | REPLY_MACRO (VL_API_SW_INTERFACE_SPAN_ENABLE_DISABLE_REPLY); |
| 63 | } |
| 64 | |
| 65 | static void |
| 66 | vl_api_sw_interface_span_dump_t_handler (vl_api_sw_interface_span_dump_t * mp) |
| 67 | { |
| 68 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 69 | vl_api_registration_t *reg; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 70 | span_interface_t *si; |
| 71 | vl_api_sw_interface_span_details_t *rmp; |
| 72 | span_main_t *sm = &span_main; |
| 73 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 74 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 75 | if (!reg) |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 76 | return; |
| 77 | |
Eyal Bari | 5b31120 | 2017-07-31 13:12:30 +0300 | [diff] [blame] | 78 | span_feat_t sf = mp->is_l2 ? SPAN_FEAT_L2 : SPAN_FEAT_DEVICE; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 79 | /* *INDENT-OFF* */ |
| 80 | vec_foreach (si, sm->interfaces) |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame] | 81 | { |
Eyal Bari | 5b31120 | 2017-07-31 13:12:30 +0300 | [diff] [blame] | 82 | span_mirror_t * rxm = &si->mirror_rxtx[sf][VLIB_RX]; |
| 83 | span_mirror_t * txm = &si->mirror_rxtx[sf][VLIB_TX]; |
| 84 | if (rxm->num_mirror_ports || txm->num_mirror_ports) |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 85 | { |
| 86 | clib_bitmap_t *b; |
| 87 | u32 i; |
Eyal Bari | 5b31120 | 2017-07-31 13:12:30 +0300 | [diff] [blame] | 88 | b = clib_bitmap_dup_or (rxm->mirror_ports, txm->mirror_ports); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 89 | clib_bitmap_foreach (i, b, ( |
| 90 | { |
| 91 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 92 | clib_memset (rmp, 0, sizeof (*rmp)); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 93 | rmp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SPAN_DETAILS); |
| 94 | rmp->context = mp->context; |
| 95 | |
| 96 | rmp->sw_if_index_from = htonl (si - sm->interfaces); |
| 97 | rmp->sw_if_index_to = htonl (i); |
Eyal Bari | 5b31120 | 2017-07-31 13:12:30 +0300 | [diff] [blame] | 98 | rmp->state = (u8) (clib_bitmap_get (rxm->mirror_ports, i) + |
| 99 | clib_bitmap_get (txm->mirror_ports, i) * 2); |
Jon Loeliger | 179ab36 | 2018-03-12 14:50:08 -0500 | [diff] [blame] | 100 | rmp->is_l2 = mp->is_l2; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 101 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 102 | vl_api_send_msg (reg, (u8 *) rmp); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 103 | })); |
| 104 | clib_bitmap_free (b); |
| 105 | } |
Eyal Bari | 001fd40 | 2017-07-16 09:34:53 +0300 | [diff] [blame] | 106 | } |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 107 | /* *INDENT-ON* */ |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * vpe_api_hookup |
| 112 | * Add vpe's API message handlers to the table. |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 113 | * vlib has already mapped shared memory and |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 114 | * added the client registration handlers. |
| 115 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 116 | */ |
| 117 | #define vl_msg_name_crc_list |
| 118 | #include <vnet/vnet_all_api_h.h> |
| 119 | #undef vl_msg_name_crc_list |
| 120 | |
| 121 | static void |
| 122 | setup_message_id_table (api_main_t * am) |
| 123 | { |
| 124 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
Ole Troan | 399ca1c | 2016-12-06 23:00:38 +0100 | [diff] [blame] | 125 | foreach_vl_msg_name_crc_span; |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 126 | #undef _ |
| 127 | } |
| 128 | |
| 129 | static clib_error_t * |
| 130 | span_api_hookup (vlib_main_t * vm) |
| 131 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame^] | 132 | api_main_t *am = vlibapi_get_main (); |
Pavel Kotucek | 3a2a1c4 | 2016-12-06 10:10:10 +0100 | [diff] [blame] | 133 | |
| 134 | #define _(N,n) \ |
| 135 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 136 | vl_api_##n##_t_handler, \ |
| 137 | vl_noop_handler, \ |
| 138 | vl_api_##n##_t_endian, \ |
| 139 | vl_api_##n##_t_print, \ |
| 140 | sizeof(vl_api_##n##_t), 1); |
| 141 | foreach_vpe_api_msg; |
| 142 | #undef _ |
| 143 | |
| 144 | /* |
| 145 | * Set up the (msg_name, crc, message-id) table |
| 146 | */ |
| 147 | setup_message_id_table (am); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | VLIB_API_INIT_FUNCTION (span_api_hookup); |
| 153 | |
| 154 | /* |
| 155 | * fd.io coding-style-patch-verification: ON |
| 156 | * |
| 157 | * Local Variables: |
| 158 | * eval: (c-set-style "gnu") |
| 159 | * End: |
| 160 | */ |