2 *------------------------------------------------------------------
3 * sr_api.c - ipv6 segment routing api
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vnet/srv6/sr.h>
22 #include <vlibmemory/api.h>
24 #include <vnet/interface.h>
25 #include <vnet/api_errno.h>
26 #include <vnet/feature/feature.h>
28 #include <vnet/vnet_msg_enum.h>
30 #define vl_typedefs /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
34 #define vl_endianfun /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #include <vnet/vnet_all_api_h.h>
44 #include <vlibapi/api_helper_macros.h>
46 #define foreach_vpe_api_msg \
47 _(SR_LOCALSID_ADD_DEL, sr_localsid_add_del) \
48 _(SR_POLICY_ADD, sr_policy_add) \
49 _(SR_POLICY_MOD, sr_policy_mod) \
50 _(SR_POLICY_DEL, sr_policy_del) \
51 _(SR_STEERING_ADD_DEL, sr_steering_add_del) \
52 _(SR_SET_ENCAP_SOURCE, sr_set_encap_source) \
53 _(SR_LOCALSIDS_DUMP, sr_localsids_dump) \
54 _(SR_POLICIES_DUMP, sr_policies_dump) \
55 _(SR_STEERING_POL_DUMP, sr_steering_pol_dump)
57 static void vl_api_sr_localsid_add_del_t_handler
58 (vl_api_sr_localsid_add_del_t * mp)
60 vl_api_sr_localsid_add_del_reply_t *rmp;
63 * int sr_cli_localsid (char is_del, ip6_address_t *localsid_addr,
64 * char end_psp, u8 behavior, u32 sw_if_index, u32 vlan_index, u32 fib_table,
65 * ip46_address_t *nh_addr, void *ls_plugin_mem)
67 if (mp->behavior == SR_BEHAVIOR_X ||
68 mp->behavior == SR_BEHAVIOR_DX6 ||
69 mp->behavior == SR_BEHAVIOR_DX4 || mp->behavior == SR_BEHAVIOR_DX2)
70 VALIDATE_SW_IF_INDEX (mp);
72 ip46_address_t prefix;
74 clib_memset (&prefix, 0, sizeof (ip46_address_t));
75 if ((mp->nh_addr4[0] | mp->nh_addr4[1] | mp->
76 nh_addr4[2] | mp->nh_addr4[3]) != 0)
77 memcpy (&prefix.ip4, mp->nh_addr4, sizeof (prefix.ip4));
79 memcpy (&prefix.ip6, mp->nh_addr6, sizeof (prefix.ip6));
81 rv = sr_cli_localsid (mp->is_del,
82 (ip6_address_t *) & mp->localsid,
85 ntohl (mp->sw_if_index),
86 ntohl (mp->vlan_index),
87 ntohl (mp->fib_table), &prefix, NULL);
89 BAD_SW_IF_INDEX_LABEL;
90 REPLY_MACRO (VL_API_SR_LOCALSID_ADD_DEL_REPLY);
94 vl_api_sr_policy_add_t_handler (vl_api_sr_policy_add_t * mp)
96 vl_api_sr_policy_add_reply_t *rmp;
97 ip6_address_t *segments = 0, *seg;
98 ip6_address_t *this_address = (ip6_address_t *) mp->sids.sids;
101 for (i = 0; i < mp->sids.num_sids; i++)
103 vec_add2 (segments, seg, 1);
104 clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
109 * sr_policy_add (ip6_address_t *bsid, ip6_address_t *segments,
110 * u32 weight, u8 behavior, u32 fib_table, u8 is_encap)
113 rv = sr_policy_add ((ip6_address_t *) & mp->bsid_addr,
115 ntohl (mp->sids.weight),
116 mp->type, ntohl (mp->fib_table), mp->is_encap);
119 REPLY_MACRO (VL_API_SR_POLICY_ADD_REPLY);
123 vl_api_sr_policy_mod_t_handler (vl_api_sr_policy_mod_t * mp)
125 vl_api_sr_policy_mod_reply_t *rmp;
127 ip6_address_t *segments = 0, *seg;
128 ip6_address_t *this_address = (ip6_address_t *) mp->sids.sids;
131 for (i = 0; i < mp->sids.num_sids; i++)
133 vec_add2 (segments, seg, 1);
134 clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
141 * sr_policy_mod(ip6_address_t *bsid, u32 index, u32 fib_table,
142 * u8 operation, ip6_address_t *segments, u32 sl_index,
143 * u32 weight, u8 is_encap)
145 rv = sr_policy_mod ((ip6_address_t *) & mp->bsid_addr,
146 ntohl (mp->sr_policy_index),
147 ntohl (mp->fib_table),
149 segments, ntohl (mp->sl_index),
150 ntohl (mp->sids.weight));
153 REPLY_MACRO (VL_API_SR_POLICY_MOD_REPLY);
157 vl_api_sr_policy_del_t_handler (vl_api_sr_policy_del_t * mp)
159 vl_api_sr_policy_del_reply_t *rmp;
163 * sr_policy_del (ip6_address_t *bsid, u32 index)
165 rv = sr_policy_del ((ip6_address_t *) & mp->bsid_addr,
166 ntohl (mp->sr_policy_index));
168 REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY);
172 vl_api_sr_set_encap_source_t_handler (vl_api_sr_set_encap_source_t * mp)
174 vl_api_sr_set_encap_source_reply_t *rmp;
176 sr_set_source ((ip6_address_t *) & mp->encaps_source);
178 REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY);
181 static void vl_api_sr_steering_add_del_t_handler
182 (vl_api_sr_steering_add_del_t * mp)
184 vl_api_sr_steering_add_del_reply_t *rmp;
188 * sr_steering_policy(int is_del, ip6_address_t *bsid, u32 sr_policy_index,
189 * u32 table_id, ip46_address_t *prefix, u32 mask_width, u32 sw_if_index,
192 if (mp->traffic_type == SR_STEER_L2)
193 VALIDATE_SW_IF_INDEX (mp);
195 rv = sr_steering_policy (mp->is_del,
196 (ip6_address_t *) & mp->bsid_addr,
197 ntohl (mp->sr_policy_index),
198 ntohl (mp->table_id),
199 (ip46_address_t *) & mp->prefix_addr,
200 ntohl (mp->mask_width),
201 ntohl (mp->sw_if_index), mp->traffic_type);
203 BAD_SW_IF_INDEX_LABEL;
204 REPLY_MACRO (VL_API_SR_STEERING_ADD_DEL_REPLY);
207 static void send_sr_localsid_details
208 (ip6_sr_localsid_t * t, vl_api_registration_t * reg, u32 context)
210 vl_api_sr_localsids_details_t *rmp;
212 rmp = vl_msg_api_alloc (sizeof (*rmp));
213 clib_memset (rmp, 0, sizeof (*rmp));
214 rmp->_vl_msg_id = ntohs (VL_API_SR_LOCALSIDS_DETAILS);
215 clib_memcpy (rmp->addr.addr, &t->localsid, sizeof (ip6_address_t));
216 rmp->end_psp = t->end_psp;
217 rmp->behavior = htons (t->behavior);
218 rmp->fib_table = htonl (t->fib_table);
219 rmp->vlan_index = htonl (t->vlan_index);
220 if (ip46_address_is_ip4 (&t->next_hop))
221 clib_memcpy (rmp->xconnect_nh_addr4, &t->next_hop.ip4,
222 sizeof (ip4_address_t));
224 clib_memcpy (rmp->xconnect_nh_addr6, &t->next_hop.ip6,
225 sizeof (ip6_address_t));
226 rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index);
227 rmp->context = context;
229 vl_api_send_msg (reg, (u8 *) rmp);
232 static void vl_api_sr_localsids_dump_t_handler
233 (vl_api_sr_localsids_dump_t * mp)
235 vl_api_registration_t *reg;
236 ip6_sr_main_t *sm = &sr_main;
237 ip6_sr_localsid_t *t;
239 reg = vl_api_client_index_to_registration (mp->client_index);
244 pool_foreach (t, sm->localsids,
246 send_sr_localsid_details(t, reg, mp->context);
251 static void send_sr_policies_details
252 (ip6_sr_policy_t * t, vl_api_registration_t * reg, u32 context)
254 vl_api_sr_policies_details_t *rmp;
255 ip6_sr_main_t *sm = &sr_main;
258 ip6_sr_sl_t *segment_list = 0;
259 vl_api_srv6_sid_list_t *write_sid_list;
261 rmp = vl_msg_api_alloc (sizeof (*rmp) +
262 vec_len (t->segments_lists) *
263 sizeof (vl_api_srv6_sid_list_t));
266 vec_len (t->segments_lists) *
267 sizeof (vl_api_srv6_sid_list_t)));
269 rmp->_vl_msg_id = ntohs (VL_API_SR_POLICIES_DETAILS);
270 clib_memcpy (rmp->bsid.addr, &t->bsid, sizeof (ip6_address_t));
271 rmp->is_encap = t->is_encap;
273 rmp->fib_table = htonl (t->fib_table);
274 rmp->num_sid_lists = vec_len (t->segments_lists);
276 /* Fill in all the segments lists */
277 vec_foreach (sl_index, t->segments_lists)
279 segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
280 write_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
281 write_sid_list->num_sids = vec_len (segment_list->segments);
282 write_sid_list->weight = htonl (segment_list->weight);
283 clib_memcpy (write_sid_list->sids, segment_list->segments,
284 vec_len (segment_list->segments) * sizeof (ip6_address_t));
287 rmp->context = context;
288 vl_api_send_msg (reg, (u8 *) rmp);
292 vl_api_sr_policies_dump_t_handler (vl_api_sr_policies_dump_t * mp)
294 vl_api_registration_t *reg;
295 ip6_sr_main_t *sm = &sr_main;
298 reg = vl_api_client_index_to_registration (mp->client_index);
303 pool_foreach (t, sm->sr_policies,
305 send_sr_policies_details(t, reg, mp->context);
310 static void send_sr_steering_pol_details
311 (ip6_sr_steering_policy_t * t, vl_api_registration_t * reg, u32 context)
313 vl_api_sr_steering_pol_details_t *rmp;
314 ip6_sr_main_t *sm = &sr_main;
316 rmp = vl_msg_api_alloc (sizeof (*rmp));
317 clib_memset (rmp, 0, sizeof (*rmp));
318 rmp->_vl_msg_id = ntohs (VL_API_SR_STEERING_POL_DETAILS);
320 //Get the SR policy BSID
322 p = pool_elt_at_index (sm->sr_policies, t->sr_policy);
323 clib_memcpy (rmp->bsid.addr, &p->bsid, sizeof (ip6_address_t));
326 rmp->traffic_type = t->classify.traffic_type;
327 rmp->fib_table = htonl (t->classify.l3.fib_table);
328 rmp->mask_width = htonl (t->classify.l3.mask_width);
329 if (ip46_address_is_ip4 (&t->classify.l3.prefix))
330 clib_memcpy (rmp->prefix_addr, &t->classify.l3.prefix.ip4,
331 sizeof (ip4_address_t));
333 clib_memcpy (rmp->prefix_addr, &t->classify.l3.prefix.ip6,
334 sizeof (ip6_address_t));
336 rmp->sw_if_index = htonl (t->classify.l2.sw_if_index);
338 rmp->context = context;
339 vl_api_send_msg (reg, (u8 *) rmp);
342 static void vl_api_sr_steering_pol_dump_t_handler
343 (vl_api_sr_policies_dump_t * mp)
345 vl_api_registration_t *reg;
346 ip6_sr_main_t *sm = &sr_main;
347 ip6_sr_steering_policy_t *t;
349 reg = vl_api_client_index_to_registration (mp->client_index);
354 pool_foreach (t, sm->steer_policies,
356 send_sr_steering_pol_details(t, reg, mp->context);
363 * Add vpe's API message handlers to the table.
364 * vlib has alread mapped shared memory and
365 * added the client registration handlers.
366 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
368 #define vl_msg_name_crc_list
369 #include <vnet/vnet_all_api_h.h>
370 #undef vl_msg_name_crc_list
373 setup_message_id_table (api_main_t * am)
375 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
376 foreach_vl_msg_name_crc_sr;
380 static clib_error_t *
381 sr_api_hookup (vlib_main_t * vm)
383 api_main_t *am = &api_main;
386 vl_msg_api_set_handlers(VL_API_##N, #n, \
387 vl_api_##n##_t_handler, \
389 vl_api_##n##_t_endian, \
390 vl_api_##n##_t_print, \
391 sizeof(vl_api_##n##_t), 1);
396 * Set up the (msg_name, crc, message-id) table
398 setup_message_id_table (am);
403 VLIB_API_INIT_FUNCTION (sr_api_hookup);
406 * fd.io coding-style-patch-verification: ON
409 * eval: (c-set-style "gnu")