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 | |
| 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 | _(LLDP_CONFIG, lldp_config) \ |
| 47 | _(SW_INTERFACE_SET_LLDP, sw_interface_set_lldp) |
| 48 | |
| 49 | static void |
| 50 | vl_api_lldp_config_t_handler (vl_api_lldp_config_t * mp) |
| 51 | { |
| 52 | vl_api_lldp_config_reply_t *rmp; |
| 53 | int rv = 0; |
| 54 | u8 *sys_name = 0; |
| 55 | |
| 56 | vec_validate (sys_name, strlen ((char *) mp->system_name) - 1); |
| 57 | strncpy ((char *) sys_name, (char *) mp->system_name, vec_len (sys_name)); |
| 58 | |
| 59 | if (lldp_cfg_set (&sys_name, ntohl (mp->tx_hold), |
| 60 | ntohl (mp->tx_interval)) != lldp_ok) |
| 61 | { |
| 62 | vec_free (sys_name); |
| 63 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 64 | } |
| 65 | |
| 66 | REPLY_MACRO (VL_API_LLDP_CONFIG_REPLY); |
| 67 | } |
| 68 | |
| 69 | static void |
| 70 | vl_api_sw_interface_set_lldp_t_handler (vl_api_sw_interface_set_lldp_t * mp) |
| 71 | { |
| 72 | vl_api_sw_interface_set_lldp_reply_t *rmp; |
| 73 | int rv = 0; |
| 74 | u8 *port_desc = 0; |
| 75 | |
| 76 | vec_validate (port_desc, strlen ((char *) mp->port_desc) - 1); |
| 77 | strncpy ((char *) port_desc, (char *) mp->port_desc, vec_len (port_desc)); |
| 78 | |
| 79 | VALIDATE_SW_IF_INDEX (mp); |
| 80 | |
| 81 | if (lldp_cfg_intf_set (ntohl (mp->sw_if_index), &port_desc, |
| 82 | mp->enable) != lldp_ok) |
| 83 | { |
| 84 | vec_free (port_desc); |
| 85 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 86 | } |
| 87 | |
| 88 | BAD_SW_IF_INDEX_LABEL; |
| 89 | |
| 90 | REPLY_MACRO (VL_API_SW_INTERFACE_SET_LLDP_REPLY); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /* |
| 95 | * * lldp_api_hookup |
| 96 | * * Add vpe's API message handlers to the table. |
| 97 | * * vlib has alread mapped shared memory and |
| 98 | * * added the client registration handlers. |
| 99 | * * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 100 | * */ |
| 101 | #define vl_msg_name_crc_list |
| 102 | #include <vnet/vnet_all_api_h.h> |
| 103 | #undef vl_msg_name_crc_list |
| 104 | |
| 105 | static void |
| 106 | setup_message_id_table (api_main_t * am) |
| 107 | { |
| 108 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 109 | foreach_vl_msg_name_crc_lldp; |
| 110 | #undef _ |
| 111 | } |
| 112 | |
| 113 | static clib_error_t * |
| 114 | lldp_api_hookup (vlib_main_t * vm) |
| 115 | { |
| 116 | api_main_t *am = &api_main; |
| 117 | |
| 118 | #define _(N,n) \ |
| 119 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 120 | vl_api_##n##_t_handler, \ |
| 121 | vl_noop_handler, \ |
| 122 | vl_api_##n##_t_endian, \ |
| 123 | vl_api_##n##_t_print, \ |
| 124 | sizeof(vl_api_##n##_t), 1); |
| 125 | foreach_vpe_api_msg; |
| 126 | #undef _ |
| 127 | |
| 128 | /* |
| 129 | * * Set up the (msg_name, crc, message-id) table |
| 130 | * */ |
| 131 | setup_message_id_table (am); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | VLIB_API_INIT_FUNCTION (lldp_api_hookup); |
| 137 | |
| 138 | /* |
| 139 | * fd.io coding-style-patch-verification: ON |
| 140 | * |
| 141 | * Local Variables: |
| 142 | * eval: (c-set-style "gnu") |
| 143 | * End: |
| 144 | */ |