FD.io VPP  v20.05-21-gb1500e9ff
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/ip/ip4_packet.h>
28 #include <vnet/ip/ip6_packet.h>
29 #include <vnet/ip/ip_types_api.h>
30 
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 
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
55 {
56  vl_api_lldp_config_reply_t *rmp;
57  int rv = 0;
58  u8 *sys_name = 0;
59 
60  sys_name = vl_api_from_api_to_new_vec (mp, &mp->system_name);
61 
62  if (lldp_cfg_set (&sys_name, ntohl (mp->tx_hold), ntohl (mp->tx_interval))
63  != lldp_ok)
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
74 {
75  vl_api_sw_interface_set_lldp_reply_t *rmp;
76  int rv = 0;
77  u8 *mgmt_oid = 0, *mgmt_ip4 = 0, *mgmt_ip6 = 0;
78  char *port_desc = 0;
79  u8 no_data[128] = { 0 };
81  ip6_address_t ip6;
82 
83  if (vl_api_string_len (&mp->port_desc) > 0)
84  {
85  port_desc = vl_api_from_api_to_new_c_string (&mp->port_desc);
86  }
87 
88  ip4_address_decode (mp->mgmt_ip4, &ip4);
89 
90  if (ip4.as_u32 != 0)
91  {
92  vec_validate (mgmt_ip4, sizeof (ip4_address_t) - 1);
93  clib_memcpy (mgmt_ip4, &ip4, vec_len (mgmt_ip4));
94  }
95 
96  ip6_address_decode (mp->mgmt_ip6, &ip6);
97 
98  if (!ip6_address_is_zero (&ip6))
99  {
100  vec_validate (mgmt_ip6, sizeof (ip6_address_t) - 1);
101  clib_memcpy (mgmt_ip6, &ip6, vec_len (mgmt_ip6));
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  }
109 
111 
112  if (lldp_cfg_intf_set (ntohl (mp->sw_if_index), (u8 **) & port_desc,
113  &mgmt_ip4, &mgmt_ip6, &mgmt_oid,
114  mp->enable) != lldp_ok)
115  {
116  vec_free (port_desc);
117  vec_free (mgmt_ip4);
118  vec_free (mgmt_ip6);
119  vec_free (mgmt_oid);
120  rv = VNET_API_ERROR_INVALID_VALUE;
121  }
122 
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.
132  * * vlib has already mapped shared memory and
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
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 *
150 {
151  api_main_t *am = vlibapi_get_main ();
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);
161 #undef _
162 
163  /*
164  * * Set up the (msg_name, crc, message-id) table
165  * */
167 
168  return 0;
169 }
170 
172 
173 /*
174  * fd.io coding-style-patch-verification: ON
175  *
176  * Local Variables:
177  * eval: (c-set-style "gnu")
178  * End:
179  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:507
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:47
void ip6_address_decode(const vl_api_ip6_address_t in, ip6_address_t *out)
Definition: ip_types_api.c:117
unsigned char u8
Definition: types.h:56
#define clib_memcpy(d, s, n)
Definition: string.h:180
vl_api_ip6_address_t mgmt_ip6
Definition: lldp.api:54
vl_api_interface_index_t sw_if_index
Definition: lldp.api:52
vl_api_ip6_address_t ip6
Definition: one.api:424
string system_name[]
Definition: lldp.api:35
#define foreach_vpe_api_msg
Definition: lldp_api.c:49
VLIB_API_INIT_FUNCTION(lldp_api_hookup)
#define REPLY_MACRO(t)
vl_api_ip4_address_t ip4
Definition: one.api:376
u32 vl_api_string_len(vl_api_string_t *astr)
Definition: api_shared.c:1138
LLDP external definition.
vlib_main_t * vm
Definition: in2out_ed.c:1599
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:226
#define BAD_SW_IF_INDEX_LABEL
static void vl_api_lldp_config_t_handler(vl_api_lldp_config_t *mp)
Definition: lldp_api.c:54
vl_api_ip4_address_t mgmt_ip4
Definition: lldp.api:53
static uword ip6_address_is_zero(const ip6_address_t *a)
Definition: ip6_packet.h:226
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
char * vl_api_from_api_to_new_c_string(vl_api_string_t *astr)
Definition: api_shared.c:1172
static void vl_api_sw_interface_set_lldp_t_handler(vl_api_sw_interface_set_lldp_t *mp)
Definition: lldp_api.c:73
#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:141
void ip4_address_decode(const vl_api_ip4_address_t in, ip4_address_t *out)
Definition: ip_types_api.c:129
Interface set LLDP request.
Definition: lldp.api:48
bool enable[default=true]
Definition: lldp.api:56
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:379
configure global parameter for LLDP
Definition: lldp.api:29
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:149
u8 * vl_api_from_api_to_new_vec(void *mp, vl_api_string_t *astr)
Definition: api_shared.c:1156
#define VALIDATE_SW_IF_INDEX(mp)