Steve Shin | 99a0e60 | 2017-07-01 04:16:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * lldp_api.c - lldp api |
| 4 | * |
| 5 | * Copyright (c) 2017 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/lldp/lldp.h> |
| 26 | |
Jakub Grajciar | 1c684f9 | 2020-01-30 14:01:17 +0100 | [diff] [blame^] | 27 | #include <vnet/ip/ip4_packet.h> |
| 28 | #include <vnet/ip/ip6_packet.h> |
| 29 | #include <vnet/ip/ip_types_api.h> |
| 30 | |
Steve Shin | 99a0e60 | 2017-07-01 04:16:20 +0000 | [diff] [blame] | 31 | #include <vnet/vnet_msg_enum.h> |
| 32 | |
| 33 | #define vl_typedefs /* define message structures */ |
| 34 | #include <vnet/vnet_all_api_h.h> |
| 35 | #undef vl_typedefs |
| 36 | |
| 37 | #define vl_endianfun /* define message structures */ |
| 38 | #include <vnet/vnet_all_api_h.h> |
| 39 | #undef vl_endianfun |
| 40 | |
| 41 | /* instantiate all the print functions we know about */ |
| 42 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 43 | #define vl_printfun |
| 44 | #include <vnet/vnet_all_api_h.h> |
| 45 | #undef vl_printfun |
| 46 | |
| 47 | #include <vlibapi/api_helper_macros.h> |
| 48 | |
| 49 | #define foreach_vpe_api_msg \ |
| 50 | _(LLDP_CONFIG, lldp_config) \ |
| 51 | _(SW_INTERFACE_SET_LLDP, sw_interface_set_lldp) |
| 52 | |
| 53 | static void |
| 54 | vl_api_lldp_config_t_handler (vl_api_lldp_config_t * mp) |
| 55 | { |
| 56 | vl_api_lldp_config_reply_t *rmp; |
| 57 | int rv = 0; |
| 58 | u8 *sys_name = 0; |
| 59 | |
Jakub Grajciar | 1c684f9 | 2020-01-30 14:01:17 +0100 | [diff] [blame^] | 60 | sys_name = vl_api_from_api_to_new_vec (&mp->system_name); |
Steve Shin | 99a0e60 | 2017-07-01 04:16:20 +0000 | [diff] [blame] | 61 | |
Steve Shin | 9a6fcef | 2017-10-11 13:55:16 -0700 | [diff] [blame] | 62 | if (lldp_cfg_set (&sys_name, ntohl (mp->tx_hold), ntohl (mp->tx_interval)) |
| 63 | != lldp_ok) |
Steve Shin | 99a0e60 | 2017-07-01 04:16:20 +0000 | [diff] [blame] | 64 | { |
| 65 | vec_free (sys_name); |
| 66 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 67 | } |
| 68 | |
| 69 | REPLY_MACRO (VL_API_LLDP_CONFIG_REPLY); |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | vl_api_sw_interface_set_lldp_t_handler (vl_api_sw_interface_set_lldp_t * mp) |
| 74 | { |
| 75 | vl_api_sw_interface_set_lldp_reply_t *rmp; |
| 76 | int rv = 0; |
Jakub Grajciar | 1c684f9 | 2020-01-30 14:01:17 +0100 | [diff] [blame^] | 77 | u8 *mgmt_oid = 0, *mgmt_ip4 = 0, *mgmt_ip6 = 0; |
| 78 | char *port_desc = 0; |
| 79 | u8 no_data[128]; |
| 80 | ip4_address_t ip4; |
| 81 | ip6_address_t ip6; |
Steve Shin | 99a0e60 | 2017-07-01 04:16:20 +0000 | [diff] [blame] | 82 | |
Jakub Grajciar | 1c684f9 | 2020-01-30 14:01:17 +0100 | [diff] [blame^] | 83 | if (vl_api_string_len (&mp->port_desc) > 0) |
Steve Shin | 9a6fcef | 2017-10-11 13:55:16 -0700 | [diff] [blame] | 84 | { |
Jakub Grajciar | 1c684f9 | 2020-01-30 14:01:17 +0100 | [diff] [blame^] | 85 | port_desc = vl_api_from_api_to_new_c_string (&mp->port_desc); |
Steve Shin | 9a6fcef | 2017-10-11 13:55:16 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Jakub Grajciar | 1c684f9 | 2020-01-30 14:01:17 +0100 | [diff] [blame^] | 88 | ip4_address_decode (mp->mgmt_ip4, &ip4); |
| 89 | |
| 90 | if (ip4.as_u32 != 0) |
Steve Shin | 9a6fcef | 2017-10-11 13:55:16 -0700 | [diff] [blame] | 91 | { |
Jakub Grajciar | 1c684f9 | 2020-01-30 14:01:17 +0100 | [diff] [blame^] | 92 | vec_validate (mgmt_ip4, sizeof (ip4_address_t) - 1); |
| 93 | clib_memcpy (mgmt_ip4, &ip4, vec_len (mgmt_ip4)); |
Steve Shin | 9a6fcef | 2017-10-11 13:55:16 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Jakub Grajciar | 1c684f9 | 2020-01-30 14:01:17 +0100 | [diff] [blame^] | 96 | ip6_address_decode (mp->mgmt_ip6, &ip6); |
| 97 | |
| 98 | if (!ip6_address_is_zero (&ip6)) |
Steve Shin | 9a6fcef | 2017-10-11 13:55:16 -0700 | [diff] [blame] | 99 | { |
Jakub Grajciar | 1c684f9 | 2020-01-30 14:01:17 +0100 | [diff] [blame^] | 100 | vec_validate (mgmt_ip6, sizeof (ip6_address_t) - 1); |
| 101 | clib_memcpy (mgmt_ip6, &ip6, vec_len (mgmt_ip6)); |
Steve Shin | 9a6fcef | 2017-10-11 13:55:16 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | if (memcmp (mp->mgmt_oid, no_data, strlen ((char *) mp->mgmt_oid)) != 0) |
| 105 | { |
| 106 | vec_validate (mgmt_oid, strlen ((char *) mp->mgmt_oid) - 1); |
| 107 | strncpy ((char *) mgmt_oid, (char *) mp->mgmt_oid, vec_len (mgmt_oid)); |
| 108 | } |
Steve Shin | 99a0e60 | 2017-07-01 04:16:20 +0000 | [diff] [blame] | 109 | |
| 110 | VALIDATE_SW_IF_INDEX (mp); |
| 111 | |
Jakub Grajciar | 1c684f9 | 2020-01-30 14:01:17 +0100 | [diff] [blame^] | 112 | if (lldp_cfg_intf_set (ntohl (mp->sw_if_index), (u8 **) & port_desc, |
Steve Shin | 9a6fcef | 2017-10-11 13:55:16 -0700 | [diff] [blame] | 113 | &mgmt_ip4, &mgmt_ip6, &mgmt_oid, |
Steve Shin | 99a0e60 | 2017-07-01 04:16:20 +0000 | [diff] [blame] | 114 | mp->enable) != lldp_ok) |
| 115 | { |
| 116 | vec_free (port_desc); |
Steve Shin | 9a6fcef | 2017-10-11 13:55:16 -0700 | [diff] [blame] | 117 | vec_free (mgmt_ip4); |
| 118 | vec_free (mgmt_ip6); |
| 119 | vec_free (mgmt_oid); |
Steve Shin | 99a0e60 | 2017-07-01 04:16:20 +0000 | [diff] [blame] | 120 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 121 | } |
| 122 | |
| 123 | BAD_SW_IF_INDEX_LABEL; |
| 124 | |
| 125 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_LLDP_REPLY); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /* |
| 130 | * * lldp_api_hookup |
| 131 | * * Add vpe's API message handlers to the table. |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame] | 132 | * * vlib has already mapped shared memory and |
Steve Shin | 99a0e60 | 2017-07-01 04:16:20 +0000 | [diff] [blame] | 133 | * * added the client registration handlers. |
| 134 | * * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 135 | * */ |
| 136 | #define vl_msg_name_crc_list |
| 137 | #include <vnet/vnet_all_api_h.h> |
| 138 | #undef vl_msg_name_crc_list |
| 139 | |
| 140 | static void |
| 141 | setup_message_id_table (api_main_t * am) |
| 142 | { |
| 143 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 144 | foreach_vl_msg_name_crc_lldp; |
| 145 | #undef _ |
| 146 | } |
| 147 | |
| 148 | static clib_error_t * |
| 149 | lldp_api_hookup (vlib_main_t * vm) |
| 150 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 151 | api_main_t *am = vlibapi_get_main (); |
Steve Shin | 99a0e60 | 2017-07-01 04:16:20 +0000 | [diff] [blame] | 152 | |
| 153 | #define _(N,n) \ |
| 154 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 155 | vl_api_##n##_t_handler, \ |
| 156 | vl_noop_handler, \ |
| 157 | vl_api_##n##_t_endian, \ |
| 158 | vl_api_##n##_t_print, \ |
| 159 | sizeof(vl_api_##n##_t), 1); |
| 160 | foreach_vpe_api_msg; |
| 161 | #undef _ |
| 162 | |
| 163 | /* |
| 164 | * * Set up the (msg_name, crc, message-id) table |
| 165 | * */ |
| 166 | setup_message_id_table (am); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | VLIB_API_INIT_FUNCTION (lldp_api_hookup); |
| 172 | |
| 173 | /* |
| 174 | * fd.io coding-style-patch-verification: ON |
| 175 | * |
| 176 | * Local Variables: |
| 177 | * eval: (c-set-style "gnu") |
| 178 | * End: |
| 179 | */ |