FD.io VPP  v20.05-21-gb1500e9ff
Vector Packet Processing
sr_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * sr_api.c - ipv6 segment routing api
4  *
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:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *------------------------------------------------------------------
18  */
19 
20 #include <vnet/vnet.h>
21 #include <vnet/srv6/sr.h>
22 #include <vlibmemory/api.h>
23 
24 #include <vnet/interface.h>
25 #include <vnet/api_errno.h>
26 #include <vnet/feature/feature.h>
27 #include <vnet/fib/fib_table.h>
28 
29 #include <vnet/ip/ip_types_api.h>
30 
31 #include <vnet/vnet_msg_enum.h>
32 
33 #define vl_typedefs /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
35 #undef vl_typedefs
36 
37 #define vl_endianfun /* define message structures */
38 #include <vnet/vnet_all_api_h.h>
39 #undef vl_endianfun
40 
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #define vl_printfun
44 #include <vnet/vnet_all_api_h.h>
45 #undef vl_printfun
46 
48 
49 #define foreach_vpe_api_msg \
50 _(SR_LOCALSID_ADD_DEL, sr_localsid_add_del) \
51 _(SR_POLICY_ADD, sr_policy_add) \
52 _(SR_POLICY_MOD, sr_policy_mod) \
53 _(SR_POLICY_DEL, sr_policy_del) \
54 _(SR_STEERING_ADD_DEL, sr_steering_add_del) \
55 _(SR_SET_ENCAP_SOURCE, sr_set_encap_source) \
56 _(SR_SET_ENCAP_HOP_LIMIT, sr_set_encap_hop_limit) \
57 _(SR_LOCALSIDS_DUMP, sr_localsids_dump) \
58 _(SR_POLICIES_DUMP, sr_policies_dump) \
59 _(SR_STEERING_POL_DUMP, sr_steering_pol_dump)
60 
63 {
64  vl_api_sr_localsid_add_del_reply_t *rmp;
65  int rv = 0;
66  ip46_address_t prefix;
67  ip6_address_t localsid;
68 /*
69  * int sr_cli_localsid (char is_del, ip6_address_t *localsid_addr,
70  * char end_psp, u8 behavior, u32 sw_if_index, u32 vlan_index, u32 fib_table,
71  * ip46_address_t *nh_addr, void *ls_plugin_mem)
72  */
73  if (mp->behavior == SR_BEHAVIOR_X ||
74  mp->behavior == SR_BEHAVIOR_DX6 ||
77 
78  ip6_address_decode (mp->localsid, &localsid);
79  ip_address_decode (&mp->nh_addr, &prefix);
80 
81  rv = sr_cli_localsid (mp->is_del,
82  &localsid, 128,
83  mp->end_psp,
84  mp->behavior,
85  ntohl (mp->sw_if_index),
86  ntohl (mp->vlan_index),
87  ntohl (mp->fib_table), &prefix, 0, NULL);
88 
90  REPLY_MACRO (VL_API_SR_LOCALSID_ADD_DEL_REPLY);
91 }
92 
93 static void
95 {
96  vl_api_sr_policy_add_reply_t *rmp;
97  ip6_address_t *segments = 0, *seg;
98  ip6_address_t bsid_addr;
99 
100  int i;
101  for (i = 0; i < mp->sids.num_sids; i++)
102  {
103  vec_add2 (segments, seg, 1);
104  ip6_address_decode (mp->sids.sids[i], seg);
105  }
106 
107  ip6_address_decode (mp->bsid_addr, &bsid_addr);
108 
109 /*
110  * sr_policy_add (ip6_address_t *bsid, ip6_address_t *segments,
111  * u32 weight, u8 behavior, u32 fib_table, u8 is_encap,
112  * u16 behavior, void *plugin_mem)
113  */
114  int rv = 0;
115  rv = sr_policy_add (&bsid_addr,
116  segments,
117  ntohl (mp->sids.weight),
118  mp->is_spray, ntohl (mp->fib_table), mp->is_encap, 0,
119  NULL);
120  vec_free (segments);
121 
122  REPLY_MACRO (VL_API_SR_POLICY_ADD_REPLY);
123 }
124 
125 static void
127 {
128  vl_api_sr_policy_mod_reply_t *rmp;
129  ip6_address_t *segments = 0, *seg;
130  ip6_address_t bsid_addr;
131 
132  int i;
133  for (i = 0; i < mp->sids.num_sids; i++)
134  {
135  vec_add2 (segments, seg, 1);
136  ip6_address_decode (mp->sids.sids[i], seg);
137  }
138 
139  ip6_address_decode (mp->bsid_addr, &bsid_addr);
140 
141  int rv = 0;
142 /*
143  * int
144  * sr_policy_mod(ip6_address_t *bsid, u32 index, u32 fib_table,
145  * u8 operation, ip6_address_t *segments, u32 sl_index,
146  * u32 weight, u8 is_encap)
147  */
148  rv = sr_policy_mod (&bsid_addr,
149  ntohl (mp->sr_policy_index),
150  ntohl (mp->fib_table),
151  mp->operation,
152  segments, ntohl (mp->sl_index),
153  ntohl (mp->sids.weight));
154  vec_free (segments);
155 
156  REPLY_MACRO (VL_API_SR_POLICY_MOD_REPLY);
157 }
158 
159 static void
161 {
162  vl_api_sr_policy_del_reply_t *rmp;
163  int rv = 0;
164  ip6_address_t bsid_addr;
165 /*
166  * int
167  * sr_policy_del (ip6_address_t *bsid, u32 index)
168  */
169  ip6_address_decode (mp->bsid_addr, &bsid_addr);
170  rv = sr_policy_del (&bsid_addr, ntohl (mp->sr_policy_index));
171 
172  REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY);
173 }
174 
175 static void
177 {
178  vl_api_sr_set_encap_source_reply_t *rmp;
179  int rv = 0;
180  ip6_address_t encaps_source;
181 
182  ip6_address_decode (mp->encaps_source, &encaps_source);
183  sr_set_source (&encaps_source);
184 
185  REPLY_MACRO (VL_API_SR_SET_ENCAP_SOURCE_REPLY);
186 }
187 
188 static void
190 {
191  vl_api_sr_set_encap_hop_limit_reply_t *rmp;
192  int rv = 0;
193 
194  if (mp->hop_limit == 0)
195  rv = VNET_API_ERROR_INVALID_VALUE;
196  else
198 
199  REPLY_MACRO (VL_API_SR_SET_ENCAP_HOP_LIMIT_REPLY);
200 }
201 
204 {
205  vl_api_sr_steering_add_del_reply_t *rmp;
206  int rv = 0;
207  ip6_address_t bsid_addr;
208  ip46_address_t prefix_addr;
209 /*
210  * int
211  * sr_steering_policy(int is_del, ip6_address_t *bsid, u32 sr_policy_index,
212  * u32 table_id, ip46_address_t *prefix, u32 mask_width, u32 sw_if_index,
213  * u8 traffic_type)
214  */
215 
216  ip6_address_decode (mp->bsid_addr, &bsid_addr);
217  ip_address_decode (&mp->prefix.address, &prefix_addr);
218 
219  if (mp->traffic_type == SR_STEER_L2)
221 
222  rv = sr_steering_policy (mp->is_del,
223  &bsid_addr,
224  ntohl (mp->sr_policy_index),
225  ntohl (mp->table_id),
226  &prefix_addr,
227  mp->prefix.len,
228  ntohl (mp->sw_if_index), mp->traffic_type);
229 
231  REPLY_MACRO (VL_API_SR_STEERING_ADD_DEL_REPLY);
232 }
233 
234 static void send_sr_localsid_details
236 {
238 
239  rmp = vl_msg_api_alloc (sizeof (*rmp));
240  clib_memset (rmp, 0, sizeof (*rmp));
241  rmp->_vl_msg_id = ntohs (VL_API_SR_LOCALSIDS_DETAILS);
242  ip6_address_encode (&t->localsid, rmp->addr);
243  rmp->end_psp = t->end_psp;
244  rmp->behavior = htons (t->behavior);
245  rmp->fib_table = htonl (t->fib_table);
246  rmp->vlan_index = htonl (t->vlan_index);
248 
249  if (t->behavior == SR_BEHAVIOR_T || t->behavior == SR_BEHAVIOR_DT6)
252  else if (t->behavior == SR_BEHAVIOR_DT4)
255  else
256  rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index);
257 
258  rmp->context = context;
259 
260  vl_api_send_msg (reg, (u8 *) rmp);
261 }
262 
265 {
267  ip6_sr_main_t *sm = &sr_main;
269 
271  if (!reg)
272  return;
273 
274  /* *INDENT-OFF* */
275  pool_foreach (t, sm->localsids,
276  ({
277  send_sr_localsid_details(t, reg, mp->context);
278  }));
279  /* *INDENT-ON* */
280 }
281 
282 static void send_sr_policies_details
284 {
286  ip6_sr_main_t *sm = &sr_main;
287 
288  u32 *sl_index, slidx = 0;
289  ip6_sr_sl_t *segment_list = 0;
290  ip6_address_t *segment;
291  vl_api_srv6_sid_list_t *api_sid_list;
292 
293  rmp = vl_msg_api_alloc (sizeof (*rmp) +
294  vec_len (t->segments_lists) *
295  sizeof (vl_api_srv6_sid_list_t));
296  clib_memset (rmp, 0,
297  (sizeof (*rmp) +
298  vec_len (t->segments_lists) *
299  sizeof (vl_api_srv6_sid_list_t)));
300 
301  rmp->_vl_msg_id = ntohs (VL_API_SR_POLICIES_DETAILS);
302  ip6_address_encode (&t->bsid, rmp->bsid);
303  rmp->is_encap = t->is_encap;
304  rmp->is_spray = t->type;
305  rmp->fib_table = htonl (t->fib_table);
307 
308  /* Fill in all the segments lists */
309  vec_foreach (sl_index, t->segments_lists)
310  {
311  segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
312 
313  api_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
314 
315  api_sid_list->num_sids = vec_len (segment_list->segments);
316  api_sid_list->weight = htonl (segment_list->weight);
317  slidx = 0;
318  vec_foreach (segment, segment_list->segments)
319  {
320  ip6_address_encode (segment, api_sid_list->sids[slidx++]);
321  }
322  }
323 
324  rmp->context = context;
325  vl_api_send_msg (reg, (u8 *) rmp);
326 }
327 
328 static void
330 {
332  ip6_sr_main_t *sm = &sr_main;
333  ip6_sr_policy_t *t;
334 
336  if (!reg)
337  return;
338 
339  /* *INDENT-OFF* */
340  pool_foreach (t, sm->sr_policies,
341  ({
342  send_sr_policies_details(t, reg, mp->context);
343  }));
344  /* *INDENT-ON* */
345 }
346 
349 {
351  ip6_sr_main_t *sm = &sr_main;
352 
353  rmp = vl_msg_api_alloc (sizeof (*rmp));
354  clib_memset (rmp, 0, sizeof (*rmp));
355  rmp->_vl_msg_id = ntohs (VL_API_SR_STEERING_POL_DETAILS);
356 
357  //Get the SR policy BSID
358  ip6_sr_policy_t *p;
360  ip6_address_encode (&p->bsid, rmp->bsid);
361 
362  //Get the steering
364  rmp->fib_table = htonl (t->classify.l3.fib_table);
366  &rmp->prefix.address);
367  rmp->prefix.len = t->classify.l3.mask_width;
368 
369  rmp->sw_if_index = htonl (t->classify.l2.sw_if_index);
370 
371  rmp->context = context;
372  vl_api_send_msg (reg, (u8 *) rmp);
373 }
374 
377 {
379  ip6_sr_main_t *sm = &sr_main;
381 
383  if (!reg)
384  return;
385 
386  /* *INDENT-OFF* */
388  ({
389  send_sr_steering_pol_details(t, reg, mp->context);
390  }));
391  /* *INDENT-ON* */
392 }
393 
394 /*
395  * sr_api_hookup
396  * Add vpe's API message handlers to the table.
397  * vlib has already mapped shared memory and
398  * added the client registration handlers.
399  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
400  */
401 #define vl_msg_name_crc_list
402 #include <vnet/vnet_all_api_h.h>
403 #undef vl_msg_name_crc_list
404 
405 static void
407 {
408 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
409  foreach_vl_msg_name_crc_sr;
410 #undef _
411 }
412 
413 static clib_error_t *
415 {
416  api_main_t *am = vlibapi_get_main ();
417 
418 #define _(N,n) \
419  vl_msg_api_set_handlers(VL_API_##N, #n, \
420  vl_api_##n##_t_handler, \
421  vl_noop_handler, \
422  vl_api_##n##_t_endian, \
423  vl_api_##n##_t_print, \
424  sizeof(vl_api_##n##_t), 1);
426 #undef _
427 
428  /*
429  * Set up the (msg_name, crc, message-id) table
430  */
432 
433  return 0;
434 }
435 
437 
438 /*
439  * fd.io coding-style-patch-verification: ON
440  *
441  * Local Variables:
442  * eval: (c-set-style "gnu")
443  * End:
444  */
ip6_sr_main_t sr_main
Definition: sr.c:31
struct sr_steering_key_t::@515::@517 l3
u8 type
Type (default is 0)
Definition: sr.h:99
u32 vlan_index
VLAN tag (not an index)
Definition: sr.h:138
void sr_set_source(ip6_address_t *address)
vl_api_ip6_address_t addr
Definition: sr.api:181
#define SR_BEHAVIOR_X
Definition: sr.h:38
vl_api_sr_steer_t traffic_type
Definition: sr.api:165
SR LocalSID.
Definition: sr.h:120
u32 fib_table
FIB table.
Definition: sr.h:107
vl_api_ip6_address_t bsid
Definition: sr.api:203
int sr_steering_policy(int is_del, ip6_address_t *bsid, u32 sr_policy_index, u32 table_id, ip46_address_t *prefix, u32 mask_width, u32 sw_if_index, u8 traffic_type)
Steer traffic L2 and L3 traffic through a given SR policy.
Definition: sr_steering.c:60
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void send_sr_policies_details(ip6_sr_policy_t *t, vl_api_registration_t *reg, u32 context)
Definition: sr_api.c:283
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
vl_api_interface_index_t sw_if_index
Definition: sr.api:227
void ip6_address_decode(const vl_api_ip6_address_t in, ip6_address_t *out)
Definition: ip_types_api.c:117
ip6_address_t * segments
SIDs (key)
Definition: sr.h:72
#define SR_BEHAVIOR_DT6
Definition: sr.h:44
vl_api_prefix_t prefix
Definition: sr.api:163
vl_api_address_t nh_addr
Definition: sr.api:55
static void vl_api_sr_policy_add_t_handler(vl_api_sr_policy_add_t *mp)
Definition: sr_api.c:94
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:628
vl_api_address_t xconnect_nh_addr
Definition: sr.api:186
u16 behavior
Behavior associated to this localsid.
Definition: sr.h:128
vl_api_prefix_t prefix
Definition: ip.api:144
vl_api_sr_behavior_t behavior
Definition: sr.api:183
void * vl_msg_api_alloc(int nbytes)
ip6_sr_steering_policy_t * steer_policies
Definition: sr.h:276
unsigned char u8
Definition: types.h:56
IPv6 SR Set SRv6 encapsulation hop-limit.
Definition: sr.api:136
vl_api_srv6_sid_list_t sids
Definition: sr.api:101
#define SR_BEHAVIOR_DT4
Definition: sr.h:45
vl_api_interface_index_t sw_if_index[default=0xffffffff]
Definition: sr.api:52
static void vl_api_sr_steering_add_del_t_handler(vl_api_sr_steering_add_del_t *mp)
Definition: sr_api.c:203
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:513
vl_api_srv6_sid_list_t sids
Definition: sr.api:77
static void vl_api_sr_policy_mod_t_handler(vl_api_sr_policy_mod_t *mp)
Definition: sr_api.c:126
vl_api_sr_policy_op_t operation
Definition: sr.api:98
SR Segment List (SID list)
Definition: sr.h:70
vl_api_sr_behavior_t behavior
Definition: sr.api:51
unsigned int u32
Definition: types.h:88
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Decode/Encode for struct/union types.
Definition: ip_types_api.c:160
static void send_sr_localsid_details(ip6_sr_localsid_t *t, vl_api_registration_t *reg, u32 context)
Definition: sr_api.c:235
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
static void vl_api_sr_localsids_dump_t_handler(vl_api_sr_localsids_dump_t *mp)
Definition: sr_api.c:264
bool is_del[default=false]
Definition: sr.api:159
static void setup_message_id_table(api_main_t *am)
Definition: sr_api.c:406
vl_api_interface_index_t sw_if_index
Definition: sr.api:164
static void send_sr_steering_pol_details(ip6_sr_steering_policy_t *t, vl_api_registration_t *reg, u32 context)
Definition: sr_api.c:348
int sr_cli_localsid(char is_del, ip6_address_t *localsid_addr, u16 localsid_prefix_len, char end_psp, u8 behavior, u32 sw_if_index, u32 vlan_index, u32 fib_table, ip46_address_t *nh_addr, int usid_len, void *ls_plugin_mem)
SR localsid add/del.
Definition: sr_localsid.c:77
char end_psp
Combined with End.PSP?
Definition: sr.h:126
sr_steering_key_t classify
Traffic classification.
Definition: sr.h:241
#define REPLY_MACRO(t)
#define SR_BEHAVIOR_DX4
Definition: sr.h:43
static void vl_api_sr_steering_pol_dump_t_handler(vl_api_sr_policies_dump_t *mp)
Definition: sr_api.c:376
u8 is_encap
Mode (0 is SRH insert, 1 Encaps)
Definition: sr.h:109
vlib_main_t * vm
Definition: in2out_ed.c:1599
u32 sr_policy
SR Policy index.
Definition: sr.h:242
u32 weight
SID list weight (wECMP / UCMP)
Definition: sr.h:74
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:226
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
ip6_sr_localsid_t * localsids
Definition: sr.h:270
#define BAD_SW_IF_INDEX_LABEL
VLIB_API_INIT_FUNCTION(sr_api_hookup)
vl_api_srv6_sid_list_t sid_lists[num_sid_lists]
Definition: sr.api:208
vl_api_ip6_address_t bsid
Definition: sr.api:228
vl_api_sr_steer_t traffic_type
Definition: sr.api:224
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
vl_api_ip6_address_t bsid_addr
Definition: sr.api:160
int sr_policy_del(ip6_address_t *bsid, u32 index)
Delete a SR policy.
u32 * segments_lists
SID lists indexes (vector)
Definition: sr.h:95
static void vl_api_sr_set_encap_hop_limit_t_handler(vl_api_sr_set_encap_hop_limit_t *mp)
Definition: sr_api.c:189
IPv6 SR Set SRv6 encapsulation source.
Definition: sr.api:124
static void vl_api_sr_policies_dump_t_handler(vl_api_sr_policies_dump_t *mp)
Definition: sr_api.c:329
IPv6 SR steering add/del.
Definition: sr.api:155
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
vl_api_ip6_address_t localsid
Definition: sr.api:49
u32 fib_table_get_table_id(u32 fib_index, fib_protocol_t proto)
Get the Table-ID of the FIB from protocol and index.
Definition: fib_table.c:1086
int sr_policy_add(ip6_address_t *bsid, ip6_address_t *segments, u32 weight, u8 behavior, u32 fib_table, u8 is_encap, u16 plugin, void *plugin_mem)
Create a new SR policy.
IPv6 SR policy deletion.
Definition: sr.api:110
void ip6_address_encode(const ip6_address_t *in, vl_api_ip6_address_t out)
Definition: ip_types_api.c:111
#define foreach_vpe_api_msg
Definition: sr_api.c:49
u32 fib_table
FIB table where localsid is registered.
Definition: sr.h:136
#define SR_BEHAVIOR_DX2
Definition: sr.h:41
IPv6 SR policy add.
Definition: sr.api:68
struct sr_steering_key_t::@515::@518 l2
#define SR_BEHAVIOR_T
Definition: sr.h:39
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
ip6_sr_policy_t * sr_policies
Definition: sr.h:264
SR Policy.
Definition: sr.h:93
static void vl_api_sr_policy_del_t_handler(vl_api_sr_policy_del_t *mp)
Definition: sr_api.c:160
vl_api_ip6_address_t bsid_addr
Definition: sr.api:95
vl_api_ip6_address_t bsid_addr
Definition: sr.api:114
vl_api_ip6_address_t encaps_source
Definition: sr.api:128
static clib_error_t * sr_api_hookup(vlib_main_t *vm)
Definition: sr_api.c:414
bool is_del[default=false]
Definition: sr.api:48
ip6_sr_sl_t * sid_lists
Definition: sr.h:261
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:194
void sr_set_hop_limit(u8 hop_limit)
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:379
Segment Routing data structures definitions.
Segment Routing main datastructure.
Definition: sr.h:255
IPv6 SR policy modification.
Definition: sr.api:91
#define vec_foreach(var, vec)
Vector iterator.
u8 traffic_type
Traffic type (IPv4, IPv6, L2)
Definition: sr.h:235
ip46_address_t next_hop
Next_hop for xconnect usage only.
Definition: sr.h:140
static void vl_api_sr_localsid_add_del_t_handler(vl_api_sr_localsid_add_del_t *mp)
Definition: sr_api.c:62
Dump the list of SR policies.
Definition: sr.api:194
#define SR_STEER_L2
Definition: sr.h:50
vl_api_prefix_t prefix
Definition: sr.api:226
u32 sw_if_index
xconnect only
Definition: sr.h:132
vl_api_ip6_address_t bsid_addr
Definition: sr.api:72
IPv6 SR LocalSID add/del request.
Definition: sr.api:44
ip6_address_t localsid
LocalSID IPv6 address.
Definition: sr.h:122
static void vl_api_sr_set_encap_source_t_handler(vl_api_sr_set_encap_source_t *mp)
Definition: sr_api.c:176
int sr_policy_mod(ip6_address_t *bsid, u32 index, u32 fib_table, u8 operation, ip6_address_t *segments, u32 sl_index, u32 weight)
Modify an existing SR policy.
ip6_address_t bsid
BindingSID (key)
Definition: sr.h:97
#define SR_BEHAVIOR_DX6
Definition: sr.h:42
#define VALIDATE_SW_IF_INDEX(mp)
Dump the list of SR LocalSIDs.
Definition: sr.api:172