lldp: Move to plugin
[vpp.git] / src / plugins / lldp / lldp_api.c
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 <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 /* define message IDs */
32 #include <vnet/format_fns.h>
33 #include <lldp/lldp.api_enum.h>
34 #include <lldp/lldp.api_types.h>
35
36 /**
37  * Base message ID fot the plugin
38  */
39 static u32 lldp_base_msg_id;
40 #define REPLY_MSG_ID_BASE lldp_base_msg_id
41
42 #include <vlibapi/api_helper_macros.h>
43
44 static void
45 vl_api_lldp_config_t_handler (vl_api_lldp_config_t * mp)
46 {
47   vl_api_lldp_config_reply_t *rmp;
48   int rv = 0;
49   u8 *sys_name = 0;
50
51   sys_name = vl_api_from_api_to_new_vec (mp, &mp->system_name);
52
53   if (lldp_cfg_set (&sys_name, ntohl (mp->tx_hold), ntohl (mp->tx_interval))
54       != lldp_ok)
55     {
56       vec_free (sys_name);
57       rv = VNET_API_ERROR_INVALID_VALUE;
58     }
59
60   REPLY_MACRO (VL_API_LLDP_CONFIG_REPLY);
61 }
62
63 static void
64 vl_api_sw_interface_set_lldp_t_handler (vl_api_sw_interface_set_lldp_t * mp)
65 {
66   vl_api_sw_interface_set_lldp_reply_t *rmp;
67   int rv = 0;
68   u8 *mgmt_oid = 0, *mgmt_ip4 = 0, *mgmt_ip6 = 0;
69   char *port_desc = 0;
70   u8 no_data[128] = { 0 };
71   ip4_address_t ip4;
72   ip6_address_t ip6;
73
74   if (vl_api_string_len (&mp->port_desc) > 0)
75     {
76       port_desc = vl_api_from_api_to_new_c_string (&mp->port_desc);
77     }
78
79   ip4_address_decode (mp->mgmt_ip4, &ip4);
80
81   if (ip4.as_u32 != 0)
82     {
83       vec_validate (mgmt_ip4, sizeof (ip4_address_t) - 1);
84       clib_memcpy (mgmt_ip4, &ip4, vec_len (mgmt_ip4));
85     }
86
87   ip6_address_decode (mp->mgmt_ip6, &ip6);
88
89   if (!ip6_address_is_zero (&ip6))
90     {
91       vec_validate (mgmt_ip6, sizeof (ip6_address_t) - 1);
92       clib_memcpy (mgmt_ip6, &ip6, vec_len (mgmt_ip6));
93     }
94
95   if (memcmp (mp->mgmt_oid, no_data, strlen ((char *) mp->mgmt_oid)) != 0)
96     {
97       vec_validate (mgmt_oid, strlen ((char *) mp->mgmt_oid) - 1);
98       strncpy ((char *) mgmt_oid, (char *) mp->mgmt_oid, vec_len (mgmt_oid));
99     }
100
101   VALIDATE_SW_IF_INDEX (mp);
102
103   if (lldp_cfg_intf_set (ntohl (mp->sw_if_index), (u8 **) & port_desc,
104                          &mgmt_ip4, &mgmt_ip6, &mgmt_oid,
105                          mp->enable) != lldp_ok)
106     {
107       vec_free (port_desc);
108       vec_free (mgmt_ip4);
109       vec_free (mgmt_ip6);
110       vec_free (mgmt_oid);
111       rv = VNET_API_ERROR_INVALID_VALUE;
112     }
113
114   BAD_SW_IF_INDEX_LABEL;
115
116   REPLY_MACRO (VL_API_SW_INTERFACE_SET_LLDP_REPLY);
117 }
118
119
120 /*
121  *  * lldp_api_hookup
122  *   * Add vpe's API message handlers to the table.
123  *    * vlib has already mapped shared memory and
124  *     * added the client registration handlers.
125  *      * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
126  *       */
127 #include <lldp/lldp.api.c>
128
129 static clib_error_t *
130 lldp_api_hookup (vlib_main_t * vm)
131 {
132   /*
133    * Set up the (msg_name, crc, message-id) table
134    */
135   lldp_base_msg_id = setup_message_id_table ();
136
137   return 0;
138 }
139
140 VLIB_API_INIT_FUNCTION (lldp_api_hookup);
141
142 #include <vlib/unix/plugin.h>
143 #include <vpp/app/version.h>
144
145 /* *INDENT-OFF* */
146 VLIB_PLUGIN_REGISTER () = {
147     .version = VPP_BUILD_VER,
148     .description = "Link Layer Discovery Protocol (LLDP)",
149 };
150 /* *INDENT-ON* */
151
152
153 /*
154  * fd.io coding-style-patch-verification: ON
155  *
156  * Local Variables:
157  * eval: (c-set-style "gnu")
158  * End:
159  */