lldp: Move to plugin
[vpp.git] / src / plugins / lldp / lldp_test.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vat/vat.h>
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 #include <vppinfra/error.h>
20
21 #include <vnet/ip/ip.h>
22 #include <vnet/ip/ip_format_fns.h>
23 #include <vnet/ethernet/ethernet_format_fns.h>
24
25 /* define message IDs */
26 #include <lldp/lldp.api_enum.h>
27 #include <lldp/lldp.api_types.h>
28
29 typedef struct
30 {
31   /* API message ID base */
32   u16 msg_id_base;
33   vat_main_t *vat_main;
34 } lldp_test_main_t;
35
36 lldp_test_main_t lldp_test_main;
37
38 #define __plugin_msg_base lldp_test_main.msg_id_base
39 #include <vlibapi/vat_helper_macros.h>
40
41 /* Macro to finish up custom dump fns */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #define FINISH                                  \
44     vec_add1 (s, 0);                            \
45     vl_print (handle, (char *)s);               \
46     vec_free (s);                               \
47     return handle;
48
49 static int
50 api_lldp_config (vat_main_t * vam)
51 {
52   unformat_input_t *i = vam->input;
53   vl_api_lldp_config_t *mp;
54   int tx_hold = 0;
55   int tx_interval = 0;
56   u8 *sys_name = NULL;
57   int ret;
58
59   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
60     {
61       if (unformat (i, "system-name %s", &sys_name))
62         ;
63       else if (unformat (i, "tx-hold %d", &tx_hold))
64         ;
65       else if (unformat (i, "tx-interval %d", &tx_interval))
66         ;
67       else
68         {
69           clib_warning ("parse error '%U'", format_unformat_error, i);
70           return -99;
71         }
72     }
73
74   vec_add1 (sys_name, 0);
75
76   M (LLDP_CONFIG, mp);
77   mp->tx_hold = htonl (tx_hold);
78   mp->tx_interval = htonl (tx_interval);
79   vl_api_vec_to_api_string (sys_name, &mp->system_name);
80   vec_free (sys_name);
81
82   S (mp);
83   W (ret);
84   return ret;
85 }
86
87 static int
88 api_sw_interface_set_lldp (vat_main_t * vam)
89 {
90   unformat_input_t *i = vam->input;
91   vl_api_sw_interface_set_lldp_t *mp;
92   u32 sw_if_index = ~0;
93   u32 enable = 1;
94   u8 *port_desc = NULL, *mgmt_oid = NULL;
95   ip4_address_t ip4_addr;
96   ip6_address_t ip6_addr;
97   int ret;
98
99   clib_memset (&ip4_addr, 0, sizeof (ip4_addr));
100   clib_memset (&ip6_addr, 0, sizeof (ip6_addr));
101
102   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
103     {
104       if (unformat (i, "disable"))
105         enable = 0;
106       else
107         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
108         ;
109       else if (unformat (i, "sw_if_index %d", &sw_if_index))
110         ;
111       else if (unformat (i, "port-desc %s", &port_desc))
112         ;
113       else if (unformat (i, "mgmt-ip4 %U", unformat_ip4_address, &ip4_addr))
114         ;
115       else if (unformat (i, "mgmt-ip6 %U", unformat_ip6_address, &ip6_addr))
116         ;
117       else if (unformat (i, "mgmt-oid %s", &mgmt_oid))
118         ;
119       else
120         break;
121     }
122
123   if (sw_if_index == ~0)
124     {
125       errmsg ("missing interface name or sw_if_index");
126       return -99;
127     }
128
129   /* Construct the API message */
130   vec_add1 (port_desc, 0);
131   vec_add1 (mgmt_oid, 0);
132   M (SW_INTERFACE_SET_LLDP, mp);
133   mp->sw_if_index = ntohl (sw_if_index);
134   mp->enable = enable;
135   vl_api_vec_to_api_string (port_desc, &mp->port_desc);
136   clib_memcpy (mp->mgmt_oid, mgmt_oid, vec_len (mgmt_oid));
137   clib_memcpy (mp->mgmt_ip4, &ip4_addr, sizeof (ip4_addr));
138   clib_memcpy (mp->mgmt_ip6, &ip6_addr, sizeof (ip6_addr));
139   vec_free (port_desc);
140   vec_free (mgmt_oid);
141
142   S (mp);
143   W (ret);
144   return ret;
145 }
146
147 #include <lldp/lldp.api_test.c>