1/*
2 * Copyright (c) 2016 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 *------------------------------------------------------------------
17 * vxlan_gpe_test.c - test harness for vxlan_gpe plugin
18 *------------------------------------------------------------------
19 */
20
21#include <vat/vat.h>
22#include <vlibapi/api.h>
23#include <vlibmemory/api.h>
24
25#include <vppinfra/error.h>
26#include <vnet/format_fns.h>
27#include <vnet/ip/ip_types_api.h>
28
29#define __plugin_msg_base ioam_vxlan_gpe_test_main.msg_id_base
30#include <vlibapi/vat_helper_macros.h>
31
32/* Declare message IDs */
33#include <ioam/lib-vxlan-gpe/ioam_vxlan_gpe.api_enum.h>
34#include <ioam/lib-vxlan-gpe/ioam_vxlan_gpe.api_types.h>
35
36#include <ioam/lib-vxlan-gpe/vxlan_gpe_ioam_packet.h>
37#include <ioam/lib-vxlan-gpe/vxlan_gpe_ioam.h>
38
39typedef struct
40{
41  /* API message ID base */
42  u16 msg_id_base;
43  vat_main_t *vat_main;
44} ioam_vxlan_gpe_test_main_t;
45
46ioam_vxlan_gpe_test_main_t ioam_vxlan_gpe_test_main;
47
48static int
49api_vxlan_gpe_ioam_enable (vat_main_t * vam)
50{
51  unformat_input_t *input = vam->input;
52  vl_api_vxlan_gpe_ioam_enable_t *mp;
53  u32 id = 0;
54  int has_trace_option = 0;
55  int has_pow_option = 0;
56  int has_ppc_option = 0;
57  int ret;
58
59  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
60    {
61      if (unformat (input, "trace"))
62	has_trace_option = 1;
63      else if (unformat (input, "pow"))
64	has_pow_option = 1;
65      else if (unformat (input, "ppc encap"))
66	has_ppc_option = PPC_ENCAP;
67      else if (unformat (input, "ppc decap"))
68	has_ppc_option = PPC_DECAP;
69      else if (unformat (input, "ppc none"))
70	has_ppc_option = PPC_NONE;
71      else
72	break;
73    }
74  M (VXLAN_GPE_IOAM_ENABLE, mp);
75  mp->id = htons (id);
76  mp->trace_ppc = has_ppc_option;
77  mp->pow_enable = has_pow_option;
78  mp->trace_enable = has_trace_option;
79
80
81  S (mp);
82  W (ret);
83  return ret;
84}
85
86
87static int
88api_vxlan_gpe_ioam_disable (vat_main_t * vam)
89{
90  vl_api_vxlan_gpe_ioam_disable_t *mp;
91  int ret;
92
93  M (VXLAN_GPE_IOAM_DISABLE, mp);
94  S (mp);
95  W (ret);
96  return ret;
97}
98
99static int
100api_vxlan_gpe_ioam_vni_enable (vat_main_t * vam)
101{
102  unformat_input_t *line_input = vam->input;
103  vl_api_vxlan_gpe_ioam_vni_enable_t *mp;
104  ip46_address_t local, remote;
105  u8 local_set = 0;
106  u8 remote_set = 0;
107  u32 vni;
108  u8 vni_set = 0;
109  int ret;
110
111
112  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
113    {
114      if (unformat (line_input, "local %U", unformat_ip46_address, &local))
115	{
116	  local_set = 1;
117	}
118      else if (unformat (line_input, "remote %U",
119			 unformat_ip46_address, &remote))
120	{
121	  remote_set = 1;
122	}
123      else if (unformat (line_input, "vni %d", &vni))
124	vni_set = 1;
125      else
126	{
127	  errmsg ("parse error '%U'\n", format_unformat_error, line_input);
128	  return -99;
129	}
130    }
131
132  if (local_set == 0)
133    {
134      errmsg ("tunnel local address not specified\n");
135      return -99;
136    }
137  if (remote_set == 0)
138    {
139      errmsg ("tunnel remote address not specified\n");
140      return -99;
141    }
142  if (ip46_address_is_ip4 (&local) != ip46_address_is_ip4 (&remote))
143    {
144      errmsg ("both IPv4 and IPv6 addresses specified");
145      return -99;
146    }
147
148  if (vni_set == 0)
149    {
150      errmsg ("vni not specified\n");
151      return -99;
152    }
153
154  M (VXLAN_GPE_IOAM_VNI_ENABLE, mp);
155
156  ip_address_encode (&local,
157		     ip46_address_is_ip4 (&local) ? IP46_TYPE_IP4 :
158		     IP46_TYPE_IP6, &mp->local);
159  ip_address_encode (&local,
160		     ip46_address_is_ip4 (&remote) ? IP46_TYPE_IP4 :
161		     IP46_TYPE_IP6, &mp->remote);
162
163  mp->vni = ntohl (vni);
164
165  S (mp);
166  W (ret);
167  return ret;
168}
169
170static int
171api_vxlan_gpe_ioam_vni_disable (vat_main_t * vam)
172{
173  unformat_input_t *line_input = vam->input;
174  vl_api_vxlan_gpe_ioam_vni_disable_t *mp;
175  ip46_address_t local, remote;
176  u8 local_set = 0;
177  u8 remote_set = 0;
178  u32 vni;
179  u8 vni_set = 0;
180  int ret;
181
182
183  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
184    {
185      if (unformat (line_input, "local %U", unformat_ip46_address, &local))
186	{
187	  local_set = 1;
188	}
189      else if (unformat (line_input, "remote %U",
190			 unformat_ip46_address, &remote))
191	{
192	  remote_set = 1;
193	}
194      else if (unformat (line_input, "vni %d", &vni))
195	vni_set = 1;
196      else
197	{
198	  errmsg ("parse error '%U'\n", format_unformat_error, line_input);
199	  return -99;
200	}
201    }
202
203  if (local_set == 0)
204    {
205      errmsg ("tunnel local address not specified\n");
206      return -99;
207    }
208  if (remote_set == 0)
209    {
210      errmsg ("tunnel remote address not specified\n");
211      return -99;
212    }
213  if (ip46_address_is_ip4 (&local) != ip46_address_is_ip4 (&remote))
214    {
215      errmsg ("both IPv4 and IPv6 addresses specified");
216      return -99;
217    }
218
219  if (vni_set == 0)
220    {
221      errmsg ("vni not specified\n");
222      return -99;
223    }
224
225  M (VXLAN_GPE_IOAM_VNI_DISABLE, mp);
226
227  ip_address_encode (&local,
228		     ip46_address_is_ip4 (&local) ? IP46_TYPE_IP4 :
229		     IP46_TYPE_IP6, &mp->local);
230  ip_address_encode (&local,
231		     ip46_address_is_ip4 (&remote) ? IP46_TYPE_IP4 :
232		     IP46_TYPE_IP6, &mp->remote);
233
234  mp->vni = ntohl (vni);
235
236  S (mp);
237  W (ret);
238  return ret;
239}
240
241static int
242api_vxlan_gpe_ioam_transit_enable (vat_main_t * vam)
243{
244  unformat_input_t *line_input = vam->input;
245  vl_api_vxlan_gpe_ioam_transit_enable_t *mp;
246  ip46_address_t local;
247  u8 local_set = 0;
248  u32 outer_fib_index = 0;
249  int ret;
250
251
252  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
253    {
254      if (unformat (line_input, "dst-ip %U", unformat_ip46_address, &local))
255	{
256	  local_set = 1;
257	}
258      else if (unformat (line_input, "outer-fib-index %d", &outer_fib_index))
259	;
260      else
261	{
262	  errmsg ("parse error '%U'\n", format_unformat_error, line_input);
263	  return -99;
264	}
265    }
266
267  if (local_set == 0)
268    {
269      errmsg ("destination address not specified\n");
270      return -99;
271    }
272
273  M (VXLAN_GPE_IOAM_TRANSIT_ENABLE, mp);
274
275
276  if (!ip46_address_is_ip4 (&local))
277    {
278      errmsg ("IPv6 currently unsupported");
279      return -1;
280    }
281  ip_address_encode (&local, IP46_TYPE_IP4, &mp->dst_addr);
282  mp->outer_fib_index = htonl (outer_fib_index);
283
284  S (mp);
285  W (ret);
286  return ret;
287}
288
289static int
290api_vxlan_gpe_ioam_transit_disable (vat_main_t * vam)
291{
292  unformat_input_t *line_input = vam->input;
293  vl_api_vxlan_gpe_ioam_transit_disable_t *mp;
294  ip46_address_t local;
295  u8 local_set = 0;
296  u32 outer_fib_index = 0;
297  int ret;
298
299
300  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
301    {
302      if (unformat (line_input, "dst-ip %U", unformat_ip46_address, &local))
303	{
304	  local_set = 1;
305	}
306      else if (unformat (line_input, "outer-fib-index %d", &outer_fib_index))
307	;
308      else
309	{
310	  errmsg ("parse error '%U'\n", format_unformat_error, line_input);
311	  return -99;
312	}
313    }
314
315  if (local_set == 0)
316    {
317      errmsg ("destination address not specified\n");
318      return -99;
319    }
320
321  M (VXLAN_GPE_IOAM_TRANSIT_DISABLE, mp);
322
323
324  if (!ip46_address_is_ip4 (&local))
325    {
326      return -1;
327    }
328  ip_address_encode (&local, IP46_TYPE_IP4, &mp->dst_addr);
329
330  mp->outer_fib_index = htonl (outer_fib_index);
331
332  S (mp);
333  W (ret);
334  return ret;
335}
336
337/* Override generated plugin register symbol */
338#define vat_plugin_register vxlan_gpe_vat_plugin_register
339#include <ioam/lib-vxlan-gpe/ioam_vxlan_gpe.api_test.c>
340
341/*
342 * fd.io coding-style-patch-verification: ON
343 *
344 * Local Variables:
345 * eval: (c-set-style "gnu")
346 * End:
347 */
348