FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
cdp.c
Go to the documentation of this file.
1 /*
2  * cdp.c - cdp protocol plug-in
3  *
4  * Copyright (c) 2011-2018 by Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <cdp/cdp.h>
21 
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
25 
26 /* define message IDs */
27 #include <cdp/cdp_msg_enum.h>
28 
29 /* define message structures */
30 #define vl_typedefs
31 #include <cdp/cdp_all_api_h.h>
32 #undef vl_typedefs
33 
34 /* define generated endian-swappers */
35 #define vl_endianfun
36 #include <cdp/cdp_all_api_h.h>
37 #undef vl_endianfun
38 
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #define vl_printfun
42 #include <cdp/cdp_all_api_h.h>
43 #undef vl_printfun
44 
45 /* Get the API version number */
46 #define vl_api_version(n,v) static u32 api_version=(v);
47 #include <cdp/cdp_all_api_h.h>
48 #undef vl_api_version
49 
50 #define REPLY_MSG_ID_BASE cm->msg_id_base
52 
53 /* List of message types that this plugin understands */
54 
55 #define foreach_cdp_plugin_api_msg \
56 _(CDP_ENABLE_DISABLE, cdp_enable_disable)
57 
58 /* Action function shared between message handler and debug CLI */
59 
60 int
61 cdp_enable_disable (cdp_main_t * cm, int enable_disable)
62 {
63  int rv = 0;
64 
65  if (enable_disable)
67  CDP_EVENT_ENABLE, 0);
68  else
71  cm->enabled = enable_disable;
72 
73  return rv;
74 }
75 
76 static clib_error_t *
78  unformat_input_t * input, vlib_cli_command_t * cmd)
79 {
80  cdp_main_t *cm = &cdp_main;
81  int enable_disable = 1;
82 
83  int rv;
84 
86  {
87  if (unformat (input, "disable"))
88  enable_disable = 0;
89  else if (unformat (input, "enable"))
90  enable_disable = 1;
91  else
92  break;
93  }
94 
95  rv = cdp_enable_disable (cm, enable_disable);
96 
97  switch (rv)
98  {
99  case 0:
100  break;
101 
102  default:
103  return clib_error_return (0, "cdp_enable_disable returned %d", rv);
104  }
105  return 0;
106 }
107 
108 /* *INDENT-OFF* */
109 VLIB_CLI_COMMAND (cdp_command, static) =
110 {
111  .path = "cdp",
112  .short_help = "cdp enable | disable",
113  .function = cdp_command_fn,
114 };
115 /* *INDENT-ON* */
116 
117 /* API message handler */
120 {
121  vl_api_cdp_enable_disable_reply_t *rmp;
122  cdp_main_t *cm = &cdp_main;
123  int rv;
124 
125  rv = cdp_enable_disable (cm, (int) (mp->enable_disable));
126 
127  REPLY_MACRO (VL_API_CDP_ENABLE_DISABLE_REPLY);
128 }
129 
130 /* Set up the API message handling tables */
131 static clib_error_t *
133 {
134  cdp_main_t *cm = &cdp_main;
135 #define _(N,n) \
136  vl_msg_api_set_handlers((VL_API_##N + cm->msg_id_base), \
137  #n, \
138  vl_api_##n##_t_handler, \
139  vl_noop_handler, \
140  vl_api_##n##_t_endian, \
141  vl_api_##n##_t_print, \
142  sizeof(vl_api_##n##_t), 1);
144 #undef _
145 
146  return 0;
147 }
148 
149 #define vl_msg_name_crc_list
150 #include <cdp/cdp_all_api_h.h>
151 #undef vl_msg_name_crc_list
152 
153 static void
155 {
156 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + cm->msg_id_base);
157  foreach_vl_msg_name_crc_cdp;
158 #undef _
159 }
160 
161 static clib_error_t *
163 {
164  cdp_main_t *cm = &cdp_main;
165  clib_error_t *error = 0;
166  u8 *name;
167 
168  cm->vlib_main = vm;
169 
170  name = format (0, "cdp_%08x%c", api_version, 0);
171 
172  /* Ask for a correctly-sized block of API message decode slots */
174  ((char *) name, VL_MSG_FIRST_AVAILABLE);
175 
176  error = cdp_plugin_api_hookup (vm);
177 
178  /* Add our API messages to the global name_crc hash table */
180 
181  vec_free (name);
182 
183  return error;
184 }
185 
187 
188 /* *INDENT-OFF* */
190 {
191  .version = VPP_BUILD_VER,
192  .description = "Cisco Discovery Protocol (CDP)",
193 };
194 /* *INDENT-ON* */
195 
196 /*
197  * fd.io coding-style-patch-verification: ON
198  *
199  * Local Variables:
200  * eval: (c-set-style "gnu")
201  * End:
202  */
u16 msg_id_base
Definition: cdp.h:85
#define foreach_cdp_plugin_api_msg
Definition: cdp.c:55
int enabled
Definition: cdp.h:97
cdp_main_t cdp_main
Definition: cdp_input.c:17
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static void setup_message_id_table(cdp_main_t *cm, api_main_t *am)
Definition: cdp.c:154
unsigned char u8
Definition: types.h:56
static clib_error_t * cdp_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: cdp.c:77
VLIB_PLUGIN_REGISTER()
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
#define clib_error_return(e, args...)
Definition: error.h:99
static void vl_api_cdp_enable_disable_t_handler(vl_api_cdp_enable_disable_t *mp)
Definition: cdp.c:119
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:934
struct _unformat_input_t unformat_input_t
#define REPLY_MACRO(t)
u8 name[64]
Definition: memclnt.api:152
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:202
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
vlib_main_t * vm
Definition: buffer.c:301
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
vlib_main_t * vlib_main
Definition: cdp.h:104
vlib_node_registration_t cdp_process_node
(constructor) VLIB_REGISTER_NODE (cdp_process_node)
Definition: cdp_node.c:207
Definition: cdp.h:79
static clib_error_t * cdp_plugin_api_hookup(vlib_main_t *vm)
Definition: cdp.c:132
api_main_t api_main
Definition: api_shared.c:35
int cdp_enable_disable(cdp_main_t *cm, int enable_disable)
Definition: cdp.c:61
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:865
static clib_error_t * cdp_init(vlib_main_t *vm)
Definition: cdp.c:162