FD.io VPP  v18.07-rc0-415-g6c78436
Vector Packet Processing
lldp_api.c
Go to the documentation of this file.
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 
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
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), ntohl (mp->tx_interval))
60  != 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
71 {
72  vl_api_sw_interface_set_lldp_reply_t *rmp;
73  int rv = 0;
74  u8 *port_desc = 0, *mgmt_ip4 = 0, *mgmt_ip6 = 0, *mgmt_oid = 0;
75  u8 no_data[256];
76 
77  memset (no_data, 0, 256);
78 
79  if (memcmp (mp->port_desc, no_data, strlen ((char *) mp->port_desc)) != 0)
80  {
81  vec_validate (port_desc, strlen ((char *) mp->port_desc) - 1);
82  strncpy ((char *) port_desc, (char *) mp->port_desc,
83  vec_len (port_desc));
84  }
85 
86  if (memcmp (mp->mgmt_ip4, no_data, sizeof (mp->mgmt_ip4)) != 0)
87  {
88  vec_validate (mgmt_ip4, sizeof (mp->mgmt_ip4) - 1);
89  clib_memcpy (mgmt_ip4, mp->mgmt_ip4, vec_len (mgmt_ip4));
90  }
91 
92  if (memcmp (mp->mgmt_ip6, no_data, sizeof (mp->mgmt_ip6)) != 0)
93  {
94  vec_validate (mgmt_ip6, sizeof (mp->mgmt_ip6) - 1);
95  clib_memcpy (mgmt_ip6, mp->mgmt_ip6, vec_len (mgmt_ip6));
96  }
97 
98  if (memcmp (mp->mgmt_oid, no_data, strlen ((char *) mp->mgmt_oid)) != 0)
99  {
100  vec_validate (mgmt_oid, strlen ((char *) mp->mgmt_oid) - 1);
101  strncpy ((char *) mgmt_oid, (char *) mp->mgmt_oid, vec_len (mgmt_oid));
102  }
103 
105 
106  if (lldp_cfg_intf_set (ntohl (mp->sw_if_index), &port_desc,
107  &mgmt_ip4, &mgmt_ip6, &mgmt_oid,
108  mp->enable) != lldp_ok)
109  {
110  vec_free (port_desc);
111  vec_free (mgmt_ip4);
112  vec_free (mgmt_ip6);
113  vec_free (mgmt_oid);
114  rv = VNET_API_ERROR_INVALID_VALUE;
115  }
116 
118 
119  REPLY_MACRO (VL_API_SW_INTERFACE_SET_LLDP_REPLY);
120 }
121 
122 
123 /*
124  * * lldp_api_hookup
125  * * Add vpe's API message handlers to the table.
126  * * vlib has alread mapped shared memory and
127  * * added the client registration handlers.
128  * * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
129  * */
130 #define vl_msg_name_crc_list
131 #include <vnet/vnet_all_api_h.h>
132 #undef vl_msg_name_crc_list
133 
134 static void
136 {
137 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
138  foreach_vl_msg_name_crc_lldp;
139 #undef _
140 }
141 
142 static clib_error_t *
144 {
145  api_main_t *am = &api_main;
146 
147 #define _(N,n) \
148  vl_msg_api_set_handlers(VL_API_##N, #n, \
149  vl_api_##n##_t_handler, \
150  vl_noop_handler, \
151  vl_api_##n##_t_endian, \
152  vl_api_##n##_t_print, \
153  sizeof(vl_api_##n##_t), 1);
155 #undef _
156 
157  /*
158  * * Set up the (msg_name, crc, message-id) table
159  * */
161 
162  return 0;
163 }
164 
166 
167 /*
168  * fd.io coding-style-patch-verification: ON
169  *
170  * Local Variables:
171  * eval: (c-set-style "gnu")
172  * End:
173  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:437
Definition: lldp.h:24
lldp_cfg_err_t lldp_cfg_intf_set(u32 hw_if_index, u8 **port_desc, u8 **mgmt_ip4, u8 **mgmt_ip6, u8 **mgmt_oid, int enable)
Definition: lldp_cli.c:46
unsigned char u8
Definition: types.h:56
#define foreach_vpe_api_msg
Definition: lldp_api.c:45
VLIB_API_INIT_FUNCTION(lldp_api_hookup)
u8 system_name[256]
Definition: lldp.api:30
#define REPLY_MACRO(t)
LLDP external definition.
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:201
#define BAD_SW_IF_INDEX_LABEL
static void vl_api_lldp_config_t_handler(vl_api_lldp_config_t *mp)
Definition: lldp_api.c:50
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
#define clib_memcpy(a, b, c)
Definition: string.h:75
static void vl_api_sw_interface_set_lldp_t_handler(vl_api_sw_interface_set_lldp_t *mp)
Definition: lldp_api.c:70
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static void setup_message_id_table(api_main_t *am)
Definition: lldp_api.c:135
Interface set LLDP request.
Definition: lldp.api:45
configure global parameter for LLDP
Definition: lldp.api:26
lldp_cfg_err_t lldp_cfg_set(u8 **host, int hold_time, int tx_interval)
Definition: lldp_cli.c:170
static clib_error_t * lldp_api_hookup(vlib_main_t *vm)
Definition: lldp_api.c:143
api_main_t api_main
Definition: api_shared.c:35
#define VALIDATE_SW_IF_INDEX(mp)