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