mactime: remove unnecessary function declaration
[vpp.git] / src / plugins / mactime / mactime_test.c
1 /*
2  * mactime.c - skeleton vpp-api-test plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
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 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21 #include <vppinfra/time_range.h>
22 #include <vnet/ethernet/ethernet.h>
23
24 /* Declare message IDs */
25 #include <mactime/mactime.api_enum.h>
26 #include <mactime/mactime.api_types.h>
27
28 typedef struct
29 {
30   /* API message ID base */
31   u16 msg_id_base;
32   vat_main_t *vat_main;
33 } mactime_test_main_t;
34
35 mactime_test_main_t mactime_test_main;
36
37 #define __plugin_msg_base mactime_test_main.msg_id_base
38 #include <vlibapi/vat_helper_macros.h>
39
40 static int
41 api_mactime_enable_disable (vat_main_t * vam)
42 {
43   unformat_input_t *i = vam->input;
44   int enable_disable = 1;
45   u32 sw_if_index = ~0;
46   vl_api_mactime_enable_disable_t *mp;
47   int ret;
48
49   /* Parse args required to build the message */
50   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
51     {
52       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
53         ;
54       else if (unformat (i, "sw_if_index %d", &sw_if_index))
55         ;
56       else if (unformat (i, "disable"))
57         enable_disable = 0;
58       else
59         break;
60     }
61
62   if (sw_if_index == ~0)
63     {
64       errmsg ("missing interface name / explicit sw_if_index number \n");
65       return -99;
66     }
67
68   /* Construct the API message */
69   M (MACTIME_ENABLE_DISABLE, mp);
70   mp->sw_if_index = ntohl (sw_if_index);
71   mp->enable_disable = enable_disable;
72
73   /* send it... */
74   S (mp);
75
76   /* Wait for a reply... */
77   W (ret);
78   return ret;
79 }
80
81 /* These two ought to be in a library somewhere but they aren't */
82 static uword
83 my_unformat_mac_address (unformat_input_t * input, va_list * args)
84 {
85   u8 *a = va_arg (*args, u8 *);
86   return unformat (input, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3],
87                    &a[4], &a[5]);
88 }
89
90 static u8 *
91 my_format_mac_address (u8 * s, va_list * args)
92 {
93   u8 *a = va_arg (*args, u8 *);
94   return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
95                  a[0], a[1], a[2], a[3], a[4], a[5]);
96 }
97
98 static int
99 api_mactime_add_del_range (vat_main_t * vam)
100 {
101   unformat_input_t *i = vam->input;
102   vl_api_mactime_add_del_range_t *mp;
103   u8 mac_address[8];
104   u8 *device_name = 0;
105   clib_timebase_range_t *rp = 0;
106   int name_set = 0;
107   int mac_set = 0;
108   u8 is_add = 1;
109   u8 allow = 0;
110   u8 allow_quota = 0;
111   u8 drop = 0;
112   u8 no_udp_10001 = 0;
113   u64 data_quota = 0;
114   int ret;
115   int ii;
116
117   /* Parse args required to build the message */
118   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
119     {
120       if (unformat (i, "name %s", &device_name))
121         {
122           vec_add1 (device_name, 0);
123           name_set = 1;
124         }
125       else if (unformat (i, "allow-range %U",
126                          unformat_clib_timebase_range_vector, &rp))
127         allow = 1;
128       else if (unformat (i, "allow-quota-range %U",
129                          unformat_clib_timebase_range_vector, &rp))
130         allow_quota = 1;
131       else if (unformat (i, "drop-range %U",
132                          unformat_clib_timebase_range_vector, &rp))
133         drop = 1;
134       else if (unformat (i, "allow-static"))
135         allow = 1;
136       else if (unformat (i, "drop-static"))
137         drop = 1;
138       else if (unformat (i, "no-udp-10001"))
139         no_udp_10001 = 1;
140       else if (unformat (i, "mac %U", my_unformat_mac_address, mac_address))
141         mac_set = 1;
142       else if (unformat (i, "del"))
143         is_add = 0;
144       else if (unformat (i, "data-quota %lldM", &data_quota))
145         data_quota <<= 20;
146       else if (unformat (i, "data-quota %lldG", &data_quota))
147         data_quota <<= 30;
148       else
149         break;
150     }
151
152   /* Sanity checks */
153   if (mac_set == 0)
154     {
155       vec_free (rp);
156       vec_free (device_name);
157       errmsg ("mac address required, not set\n");
158       return -99;
159     }
160
161   /* allow-range / drop-range parse errors cause this condition */
162   if (is_add && allow == 0 && drop == 0 && allow_quota == 0)
163     {
164       vec_free (rp);
165       vec_free (device_name);
166       errmsg ("parse error...\n");
167       return -99;
168     }
169
170   /* Unlikely, but check anyhow */
171   if (vec_len (device_name) > ARRAY_LEN (mp->device_name))
172     {
173       vec_free (rp);
174       vec_free (device_name);
175       errmsg ("device name too long, max %d\n", ARRAY_LEN (mp->device_name));
176       return -99;
177     }
178
179   /* Cough up a device name if none set */
180   if (name_set == 0)
181     {
182       device_name = format (0, "mac %U%c", my_format_mac_address,
183                             mac_address, 0);
184     }
185
186   /* Construct the API message */
187   M2 (MACTIME_ADD_DEL_RANGE, mp, sizeof (rp[0]) * vec_len (rp));
188   mp->is_add = is_add;
189   mp->drop = drop;
190   mp->allow = allow;
191   mp->allow_quota = allow_quota;
192   mp->no_udp_10001 = no_udp_10001;
193   mp->data_quota = clib_host_to_net_u64 (data_quota);
194   memcpy (mp->mac_address, mac_address, sizeof (mp->mac_address));
195   memcpy (mp->device_name, device_name, vec_len (device_name));
196   mp->count = clib_host_to_net_u32 (vec_len (rp));
197
198   for (ii = 0; ii < vec_len (rp); ii++)
199     {
200       mp->ranges[ii].start = rp[ii].start;
201       mp->ranges[ii].end = rp[ii].end;
202     }
203
204   vec_free (rp);
205   vec_free (device_name);
206
207   /* send it... */
208   S (mp);
209
210   /* Wait for a reply... */
211   W (ret);
212   return ret;
213 }
214
215 #include <mactime/mactime.api_test.c>
216
217 /*
218  * fd.io coding-style-patch-verification: ON
219  *
220  * Local Variables:
221  * eval: (c-set-style "gnu")
222  * End:
223  */