Line data Source code
1 : /*
2 : * Copyright (c) 2017 Intel 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 : #include <vat/vat.h>
17 : #include <vlibapi/api.h>
18 : #include <vlibmemory/api.h>
19 :
20 : #include <vppinfra/error.h>
21 : #include <pppoe/pppoe.h>
22 : #include <vnet/format_fns.h>
23 :
24 : #include <vnet/ip/ip_types_api.h>
25 :
26 : #define __plugin_msg_base pppoe_test_main.msg_id_base
27 : #include <vlibapi/vat_helper_macros.h>
28 :
29 :
30 0 : uword unformat_ip46_address (unformat_input_t * input, va_list * args)
31 : {
32 0 : ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
33 0 : ip46_type_t type = va_arg (*args, ip46_type_t);
34 0 : if ((type != IP46_TYPE_IP6) &&
35 0 : unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
36 0 : ip46_address_mask_ip4(ip46);
37 0 : return 1;
38 0 : } else if ((type != IP46_TYPE_IP4) &&
39 0 : unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
40 0 : return 1;
41 : }
42 0 : return 0;
43 : }
44 0 : uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
45 : {
46 0 : ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
47 0 : u8 *len = va_arg (*args, u8 *);
48 0 : ip46_type_t type = va_arg (*args, ip46_type_t);
49 :
50 : u32 l;
51 0 : if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
52 0 : if (l > 32)
53 0 : return 0;
54 0 : *len = l + 96;
55 0 : ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
56 0 : } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
57 0 : if (l > 128)
58 0 : return 0;
59 0 : *len = l;
60 : } else {
61 0 : return 0;
62 : }
63 0 : return 1;
64 : }
65 : /////////////////////////
66 :
67 : #include <pppoe/pppoe.api_enum.h>
68 : #include <pppoe/pppoe.api_types.h>
69 :
70 : typedef struct {
71 : /* API message ID base */
72 : u16 msg_id_base;
73 : vat_main_t *vat_main;
74 : } pppoe_test_main_t;
75 :
76 : pppoe_test_main_t pppoe_test_main;
77 :
78 0 : static void vl_api_pppoe_add_del_session_reply_t_handler
79 : (vl_api_pppoe_add_del_session_reply_t * mp)
80 : {
81 0 : vat_main_t *vam = &vat_main;
82 0 : i32 retval = ntohl (mp->retval);
83 0 : if (vam->async_mode)
84 : {
85 0 : vam->async_errors += (retval < 0);
86 : }
87 : else
88 : {
89 0 : vam->retval = retval;
90 0 : vam->sw_if_index = ntohl (mp->sw_if_index);
91 0 : vam->result_ready = 1;
92 : }
93 0 : }
94 :
95 : static int
96 0 : api_pppoe_add_del_session (vat_main_t * vam)
97 : {
98 0 : unformat_input_t *line_input = vam->input;
99 : vl_api_pppoe_add_del_session_t *mp;
100 0 : u16 session_id = 0;
101 : ip46_address_t client_ip;
102 0 : u8 is_add = 1;
103 0 : u8 client_ip_set = 0;
104 0 : u8 ipv4_set = 0;
105 0 : u8 ipv6_set = 0;
106 0 : u32 decap_vrf_id = 0;
107 0 : u8 client_mac[6] = { 0 };
108 0 : u8 client_mac_set = 0;
109 : int ret;
110 :
111 : /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
112 0 : clib_memset (&client_ip, 0, sizeof client_ip);
113 :
114 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
115 : {
116 0 : if (unformat (line_input, "del"))
117 : {
118 0 : is_add = 0;
119 : }
120 0 : else if (unformat (line_input, "session_id %d", &session_id))
121 : ;
122 0 : else if (unformat (line_input, "client-ip %U",
123 : unformat_ip4_address, &client_ip.ip4))
124 : {
125 0 : client_ip_set = 1;
126 0 : ipv4_set = 1;
127 : }
128 0 : else if (unformat (line_input, "client-ip %U",
129 : unformat_ip6_address, &client_ip.ip6))
130 : {
131 0 : client_ip_set = 1;
132 0 : ipv6_set = 1;
133 : }
134 0 : else if (unformat (line_input, "decap-vrf-id %d", &decap_vrf_id))
135 : ;
136 0 : else if (unformat (line_input, "client-mac %U", unformat_ethernet_address, client_mac))
137 0 : client_mac_set = 1;
138 : else
139 : {
140 0 : return -99;
141 : }
142 : }
143 :
144 0 : if (client_ip_set == 0)
145 : {
146 0 : errmsg ("session client_ip address not specified");
147 0 : return -99;
148 : }
149 :
150 0 : if (ipv4_set && ipv6_set)
151 : {
152 0 : errmsg ("both IPv4 and IPv6 addresses specified");
153 0 : return -99;
154 : }
155 :
156 0 : if (client_mac_set == 0)
157 : {
158 0 : errmsg("session client mac not specified");
159 0 : return -99;
160 : }
161 :
162 0 : M (PPPOE_ADD_DEL_SESSION, mp);
163 :
164 :
165 0 : if (ipv6_set)
166 : {
167 0 : ip_address_encode(&client_ip, IP46_TYPE_IP6, &mp->client_ip);
168 : }
169 : else
170 : {
171 0 : ip_address_encode(&client_ip, IP46_TYPE_IP4, &mp->client_ip);
172 : }
173 :
174 0 : mp->decap_vrf_id = ntohl (decap_vrf_id);
175 0 : mp->session_id = ntohl (session_id);
176 0 : mp->is_add = is_add;
177 0 : memcpy (mp->client_mac, client_mac, 6);
178 :
179 0 : S (mp);
180 0 : W (ret);
181 0 : return ret;
182 : }
183 :
184 0 : static void vl_api_pppoe_session_details_t_handler
185 : (vl_api_pppoe_session_details_t * mp)
186 : {
187 0 : vat_main_t *vam = &vat_main;
188 : ip46_address_t client_ip;
189 0 : ip_address_decode(&mp->client_ip, &client_ip);
190 0 : print (vam->ofp, "%11d%14d%24U%14d%14d%30U%30U",
191 0 : ntohl (mp->sw_if_index), ntohl (mp->session_id),
192 : format_ip46_address, &client_ip, IP46_TYPE_ANY,
193 : ntohl (mp->encap_if_index), ntohl (mp->decap_vrf_id),
194 0 : format_ethernet_address, mp->local_mac,
195 0 : format_ethernet_address, mp->client_mac);
196 0 : }
197 :
198 : static int
199 0 : api_pppoe_session_dump (vat_main_t * vam)
200 : {
201 0 : unformat_input_t *i = vam->input;
202 : vl_api_pppoe_session_dump_t *mp;
203 : u32 sw_if_index;
204 0 : u8 sw_if_index_set = 0;
205 : int ret;
206 :
207 : /* Parse args required to build the message */
208 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
209 : {
210 0 : if (unformat (i, "sw_if_index %d", &sw_if_index))
211 0 : sw_if_index_set = 1;
212 : else
213 0 : break;
214 : }
215 :
216 0 : if (sw_if_index_set == 0)
217 : {
218 0 : sw_if_index = ~0;
219 : }
220 :
221 0 : if (!vam->json_output)
222 : {
223 0 : print (vam->ofp, "%11s%24s%14s%14s%14s",
224 : "sw_if_index", "client_ip", "session_id",
225 : "encap_if_index", "decap_fib_index",
226 : "local-mac", "client-mac");
227 : }
228 :
229 : /* Get list of pppoe-session interfaces */
230 0 : M (PPPOE_SESSION_DUMP, mp);
231 :
232 0 : mp->sw_if_index = htonl (sw_if_index);
233 :
234 0 : S (mp);
235 :
236 0 : W (ret);
237 0 : return ret;
238 : }
239 :
240 0 : static void vl_api_pppoe_add_del_cp_reply_t_handler
241 : (vl_api_pppoe_add_del_session_reply_t * mp)
242 : {
243 0 : vat_main_t *vam = &vat_main;
244 0 : i32 retval = ntohl (mp->retval);
245 0 : if (vam->async_mode)
246 : {
247 0 : vam->async_errors += (retval < 0);
248 : }
249 : else
250 : {
251 0 : vam->retval = retval;
252 0 : vam->result_ready = 1;
253 : }
254 0 : }
255 :
256 : static int
257 0 : api_pppoe_add_del_cp (vat_main_t * vam)
258 : {
259 0 : unformat_input_t *line_input = vam->input;
260 : vl_api_pppoe_add_del_cp_t *mp;
261 0 : u8 is_add = 1;
262 0 : u32 sw_if_index = ~0;
263 : int ret;
264 :
265 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
266 : {
267 0 : if (unformat (line_input, "del"))
268 : {
269 0 : is_add = 0;
270 : }
271 0 : else if (unformat (line_input, "cp-if-index %d", &sw_if_index))
272 : ;
273 : }
274 :
275 0 : M (PPPOE_ADD_DEL_CP, mp);
276 :
277 0 : mp->is_add = is_add;
278 0 : mp->sw_if_index = sw_if_index;
279 :
280 0 : S (mp);
281 0 : W (ret);
282 0 : return ret;
283 : }
284 :
285 : #include <pppoe/pppoe.api_test.c>
|