ae2a3edf64a874987ebbfc0c262442dab7a35d30
[vpp.git] / src / plugins / flowprobe / flowprobe_test.c
1 /*
2  * flowprobe.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 <flowprobe/flowprobe.h>
22
23 #define __plugin_msg_base flowprobe_test_main.msg_id_base
24 #include <vlibapi/vat_helper_macros.h>
25
26 /**
27  * @file vpp_api_test plugin
28  */
29
30 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
31
32 /* Declare message IDs */
33 #include <flowprobe/flowprobe.api_enum.h>
34 #include <flowprobe/flowprobe.api_types.h>
35
36 typedef struct
37 {
38     /** API message ID base */
39   u16 msg_id_base;
40     /** vat_main_t pointer */
41   vat_main_t *vat_main;
42 } flowprobe_test_main_t;
43
44 flowprobe_test_main_t flowprobe_test_main;
45
46 static int
47 api_flowprobe_tx_interface_add_del (vat_main_t * vam)
48 {
49   unformat_input_t *i = vam->input;
50   int enable_disable = 1;
51   u8 which = FLOW_VARIANT_IP4;
52   u32 sw_if_index = ~0;
53   vl_api_flowprobe_tx_interface_add_del_t *mp;
54   int ret;
55
56   /* Parse args required to build the message */
57   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
58     {
59       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
60         ;
61       else if (unformat (i, "sw_if_index %d", &sw_if_index))
62         ;
63       else if (unformat (i, "disable"))
64         enable_disable = 0;
65       else if (unformat (i, "ip4"))
66         which = FLOW_VARIANT_IP4;
67       else if (unformat (i, "ip6"))
68         which = FLOW_VARIANT_IP6;
69       else if (unformat (i, "l2"))
70         which = FLOW_VARIANT_L2;
71       else
72         break;
73     }
74
75   if (sw_if_index == ~0)
76     {
77       errmsg ("missing interface name / explicit sw_if_index number \n");
78       return -99;
79     }
80
81   /* Construct the API message */
82   M (FLOWPROBE_TX_INTERFACE_ADD_DEL, mp);
83   mp->sw_if_index = ntohl (sw_if_index);
84   mp->is_add = enable_disable;
85   mp->which = which;
86
87   /* send it... */
88   S (mp);
89
90   /* Wait for a reply... */
91   W (ret);
92   return ret;
93 }
94
95 static int
96 api_flowprobe_interface_add_del (vat_main_t *vam)
97 {
98   unformat_input_t *i = vam->input;
99   int enable_disable = 1;
100   u8 which = FLOWPROBE_WHICH_IP4;
101   u8 direction = FLOWPROBE_DIRECTION_TX;
102   u32 sw_if_index = ~0;
103   vl_api_flowprobe_interface_add_del_t *mp;
104   int ret;
105
106   /* Parse args required to build the message */
107   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
108     {
109       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
110         ;
111       else if (unformat (i, "sw_if_index %d", &sw_if_index))
112         ;
113       else if (unformat (i, "disable"))
114         enable_disable = 0;
115       else if (unformat (i, "ip4"))
116         which = FLOWPROBE_WHICH_IP4;
117       else if (unformat (i, "ip6"))
118         which = FLOWPROBE_WHICH_IP6;
119       else if (unformat (i, "l2"))
120         which = FLOWPROBE_WHICH_L2;
121       else if (unformat (i, "rx"))
122         direction = FLOWPROBE_DIRECTION_RX;
123       else if (unformat (i, "tx"))
124         direction = FLOWPROBE_DIRECTION_TX;
125       else if (unformat (i, "both"))
126         direction = FLOWPROBE_DIRECTION_BOTH;
127       else
128         break;
129     }
130
131   if (sw_if_index == ~0)
132     {
133       errmsg ("Missing interface name / explicit sw_if_index number\n");
134       return -99;
135     }
136
137   /* Construct the API message */
138   M (FLOWPROBE_INTERFACE_ADD_DEL, mp);
139   mp->sw_if_index = ntohl (sw_if_index);
140   mp->is_add = enable_disable;
141   mp->which = which;
142   mp->direction = direction;
143
144   /* Send it... */
145   S (mp);
146
147   /* Wait for a reply... */
148   W (ret);
149   return ret;
150 }
151
152 static int
153 api_flowprobe_params (vat_main_t * vam)
154 {
155   unformat_input_t *i = vam->input;
156   u32 active_timer = ~0;
157   u32 passive_timer = ~0;
158   vl_api_flowprobe_params_t *mp;
159   int ret;
160   u8 record_flags = 0;
161
162   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
163     {
164       if (unformat (i, "active %d", &active_timer))
165         ;
166       else if (unformat (i, "passive %d", &passive_timer))
167         ;
168       else if (unformat (i, "record"))
169         while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
170           {
171             if (unformat (i, "l2"))
172               record_flags |= FLOWPROBE_RECORD_FLAG_L2;
173             else if (unformat (i, "l3"))
174               record_flags |= FLOWPROBE_RECORD_FLAG_L3;
175             else if (unformat (i, "l4"))
176               record_flags |= FLOWPROBE_RECORD_FLAG_L4;
177             else
178               break;
179           }
180       else
181         break;
182     }
183
184   if (passive_timer > 0 && active_timer > passive_timer)
185     {
186       errmsg ("Passive timer has to be greater than active one...\n");
187       return -99;
188     }
189
190   /* Construct the API message */
191   M (FLOWPROBE_PARAMS, mp);
192   mp->record_flags = record_flags;
193   mp->active_timer = ntohl (active_timer);
194   mp->passive_timer = ntohl (passive_timer);
195
196   /* send it... */
197   S (mp);
198
199   /* Wait for a reply... */
200   W (ret);
201
202   return ret;
203 }
204
205 /*
206  * List of messages that the api test plugin sends,
207  * and that the data plane plugin processes
208  */
209 #include <flowprobe/flowprobe.api_test.c>
210
211 /*
212  * fd.io coding-style-patch-verification: ON
213  *
214  * Local Variables:
215  * eval: (c-set-style "gnu")
216  * End:
217  */