FD.io VPP  v19.08.2-294-g37e99c22d
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/vnet_msg_enum.h>
30 
31 #define vl_typedefs /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
33 #undef vl_typedefs
34 
35 #define vl_endianfun /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_endianfun
38 
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #define vl_printfun
42 #include <vnet/vnet_all_api_h.h>
43 #undef vl_printfun
44 
46 
47 #define foreach_vpe_api_msg \
48 _(SR_LOCALSID_ADD_DEL, sr_localsid_add_del) \
49 _(SR_POLICY_ADD, sr_policy_add) \
50 _(SR_POLICY_MOD, sr_policy_mod) \
51 _(SR_POLICY_DEL, sr_policy_del) \
52 _(SR_STEERING_ADD_DEL, sr_steering_add_del) \
53 _(SR_SET_ENCAP_SOURCE, sr_set_encap_source) \
54 _(SR_LOCALSIDS_DUMP, sr_localsids_dump) \
55 _(SR_POLICIES_DUMP, sr_policies_dump) \
56 _(SR_STEERING_POL_DUMP, sr_steering_pol_dump)
57 
60 {
61  vl_api_sr_localsid_add_del_reply_t *rmp;
62  int rv = 0;
63 /*
64  * int sr_cli_localsid (char is_del, ip6_address_t *localsid_addr,
65  * char end_psp, u8 behavior, u32 sw_if_index, u32 vlan_index, u32 fib_table,
66  * ip46_address_t *nh_addr, void *ls_plugin_mem)
67  */
68  if (mp->behavior == SR_BEHAVIOR_X ||
69  mp->behavior == SR_BEHAVIOR_DX6 ||
72 
73  ip46_address_t prefix;
74 
75  clib_memset (&prefix, 0, sizeof (ip46_address_t));
76  if ((mp->nh_addr4[0] | mp->nh_addr4[1] | mp->
77  nh_addr4[2] | mp->nh_addr4[3]) != 0)
78  memcpy (&prefix.ip4, mp->nh_addr4, sizeof (prefix.ip4));
79  else
80  memcpy (&prefix.ip6, mp->nh_addr6, sizeof (prefix.ip6));
81 
82  rv = sr_cli_localsid (mp->is_del,
83  (ip6_address_t *) & mp->localsid,
84  mp->end_psp,
85  mp->behavior,
86  ntohl (mp->sw_if_index),
87  ntohl (mp->vlan_index),
88  ntohl (mp->fib_table), &prefix, NULL);
89 
91  REPLY_MACRO (VL_API_SR_LOCALSID_ADD_DEL_REPLY);
92 }
93 
94 static void
96 {
97  vl_api_sr_policy_add_reply_t *rmp;
98  ip6_address_t *segments = 0, *seg;
99  ip6_address_t *this_address = (ip6_address_t *) mp->sids.sids;
100 
101  int i;
102  for (i = 0; i < mp->sids.num_sids; i++)
103  {
104  vec_add2 (segments, seg, 1);
105  clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
106  this_address++;
107  }
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  */
113  int rv = 0;
114  rv = sr_policy_add ((ip6_address_t *) & mp->bsid_addr,
115  segments,
116  ntohl (mp->sids.weight),
117  mp->type, ntohl (mp->fib_table), mp->is_encap);
118  vec_free (segments);
119 
120  REPLY_MACRO (VL_API_SR_POLICY_ADD_REPLY);
121 }
122 
123 static void
125 {
126  vl_api_sr_policy_mod_reply_t *rmp;
127 
128  ip6_address_t *segments = 0, *seg;
129  ip6_address_t *this_address = (ip6_address_t *) mp->sids.sids;
130 
131  int i;
132  for (i = 0; i < mp->sids.num_sids; i++)
133  {
134  vec_add2 (segments, seg, 1);
135  clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
136  this_address++;
137  }
138 
139  int rv = 0;
140 /*
141  * int
142  * sr_policy_mod(ip6_address_t *bsid, u32 index, u32 fib_table,
143  * u8 operation, ip6_address_t *segments, u32 sl_index,
144  * u32 weight, u8 is_encap)
145  */
146  rv = sr_policy_mod ((ip6_address_t *) & mp->bsid_addr,
147  ntohl (mp->sr_policy_index),
148  ntohl (mp->fib_table),
149  mp->operation,
150  segments, ntohl (mp->sl_index),
151  ntohl (mp->sids.weight));
152  vec_free (segments);
153 
154  REPLY_MACRO (VL_API_SR_POLICY_MOD_REPLY);
155 }
156 
157 static void
159 {
160  vl_api_sr_policy_del_reply_t *rmp;
161  int rv = 0;
162 /*
163  * int
164  * sr_policy_del (ip6_address_t *bsid, u32 index)
165  */
166  rv = sr_policy_del ((ip6_address_t *) & mp->bsid_addr,
167  ntohl (mp->sr_policy_index));
168 
169  REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY);
170 }
171 
172 static void
174 {
175  vl_api_sr_set_encap_source_reply_t *rmp;
176  int rv = 0;
178 
179  REPLY_MACRO (VL_API_SR_SET_ENCAP_SOURCE_REPLY);
180 }
181 
184 {
185  vl_api_sr_steering_add_del_reply_t *rmp;
186  int rv = 0;
187 /*
188  * int
189  * sr_steering_policy(int is_del, ip6_address_t *bsid, u32 sr_policy_index,
190  * u32 table_id, ip46_address_t *prefix, u32 mask_width, u32 sw_if_index,
191  * u8 traffic_type)
192  */
193  if (mp->traffic_type == SR_STEER_L2)
195 
196  rv = sr_steering_policy (mp->is_del,
197  (ip6_address_t *) & mp->bsid_addr,
198  ntohl (mp->sr_policy_index),
199  ntohl (mp->table_id),
200  (ip46_address_t *) & mp->prefix_addr,
201  ntohl (mp->mask_width),
202  ntohl (mp->sw_if_index), mp->traffic_type);
203 
205  REPLY_MACRO (VL_API_SR_STEERING_ADD_DEL_REPLY);
206 }
207 
208 static void send_sr_localsid_details
210 {
212 
213  rmp = vl_msg_api_alloc (sizeof (*rmp));
214  clib_memset (rmp, 0, sizeof (*rmp));
215  rmp->_vl_msg_id = ntohs (VL_API_SR_LOCALSIDS_DETAILS);
216  clib_memcpy (rmp->addr.addr, &t->localsid, sizeof (ip6_address_t));
217  rmp->end_psp = t->end_psp;
218  rmp->behavior = htons (t->behavior);
219  rmp->fib_table = htonl (t->fib_table);
220  rmp->vlan_index = htonl (t->vlan_index);
221  if (ip46_address_is_ip4 (&t->next_hop))
222  clib_memcpy (rmp->xconnect_nh_addr4, &t->next_hop.ip4,
223  sizeof (ip4_address_t));
224  else
225  clib_memcpy (rmp->xconnect_nh_addr6, &t->next_hop.ip6,
226  sizeof (ip6_address_t));
227 
228  if (t->behavior == SR_BEHAVIOR_T || t->behavior == SR_BEHAVIOR_DT6)
231  else if (t->behavior == SR_BEHAVIOR_DT4)
234  else
235  rmp->xconnect_iface_or_vrf_table = htonl (t->sw_if_index);
236 
237  rmp->context = context;
238 
239  vl_api_send_msg (reg, (u8 *) rmp);
240 }
241 
244 {
246  ip6_sr_main_t *sm = &sr_main;
248 
250  if (!reg)
251  return;
252 
253  /* *INDENT-OFF* */
254  pool_foreach (t, sm->localsids,
255  ({
256  send_sr_localsid_details(t, reg, mp->context);
257  }));
258  /* *INDENT-ON* */
259 }
260 
261 static void send_sr_policies_details
263 {
265  ip6_sr_main_t *sm = &sr_main;
266 
267  u32 *sl_index;
268  ip6_sr_sl_t *segment_list = 0;
269  vl_api_srv6_sid_list_t *write_sid_list;
270 
271  rmp = vl_msg_api_alloc (sizeof (*rmp) +
272  vec_len (t->segments_lists) *
273  sizeof (vl_api_srv6_sid_list_t));
274  clib_memset (rmp, 0,
275  (sizeof (*rmp) +
276  vec_len (t->segments_lists) *
277  sizeof (vl_api_srv6_sid_list_t)));
278 
279  rmp->_vl_msg_id = ntohs (VL_API_SR_POLICIES_DETAILS);
280  clib_memcpy (rmp->bsid.addr, &t->bsid, sizeof (ip6_address_t));
281  rmp->is_encap = t->is_encap;
282  rmp->type = t->type;
283  rmp->fib_table = htonl (t->fib_table);
285 
286  /* Fill in all the segments lists */
287  vec_foreach (sl_index, t->segments_lists)
288  {
289  segment_list = pool_elt_at_index (sm->sid_lists, *sl_index);
290  write_sid_list = &rmp->sid_lists[sl_index - t->segments_lists];
291  write_sid_list->num_sids = vec_len (segment_list->segments);
292  write_sid_list->weight = htonl (segment_list->weight);
293  clib_memcpy (write_sid_list->sids, segment_list->segments,
294  vec_len (segment_list->segments) * sizeof (ip6_address_t));
295  }
296 
297  rmp->context = context;
298  vl_api_send_msg (reg, (u8 *) rmp);
299 }
300 
301 static void
303 {
305  ip6_sr_main_t *sm = &sr_main;
306  ip6_sr_policy_t *t;
307 
309  if (!reg)
310  return;
311 
312  /* *INDENT-OFF* */
313  pool_foreach (t, sm->sr_policies,
314  ({
315  send_sr_policies_details(t, reg, mp->context);
316  }));
317  /* *INDENT-ON* */
318 }
319 
322 {
324  ip6_sr_main_t *sm = &sr_main;
325 
326  rmp = vl_msg_api_alloc (sizeof (*rmp));
327  clib_memset (rmp, 0, sizeof (*rmp));
328  rmp->_vl_msg_id = ntohs (VL_API_SR_STEERING_POL_DETAILS);
329 
330  //Get the SR policy BSID
331  ip6_sr_policy_t *p;
333  clib_memcpy (rmp->bsid.addr, &p->bsid, sizeof (ip6_address_t));
334 
335  //Get the steering
337  rmp->fib_table = htonl (t->classify.l3.fib_table);
338  rmp->mask_width = htonl (t->classify.l3.mask_width);
339  if (ip46_address_is_ip4 (&t->classify.l3.prefix))
340  clib_memcpy (rmp->prefix_addr, &t->classify.l3.prefix.ip4,
341  sizeof (ip4_address_t));
342  else
343  clib_memcpy (rmp->prefix_addr, &t->classify.l3.prefix.ip6,
344  sizeof (ip6_address_t));
345 
346  rmp->sw_if_index = htonl (t->classify.l2.sw_if_index);
347 
348  rmp->context = context;
349  vl_api_send_msg (reg, (u8 *) rmp);
350 }
351 
354 {
356  ip6_sr_main_t *sm = &sr_main;
358 
360  if (!reg)
361  return;
362 
363  /* *INDENT-OFF* */
365  ({
366  send_sr_steering_pol_details(t, reg, mp->context);
367  }));
368  /* *INDENT-ON* */
369 }
370 
371 /*
372  * sr_api_hookup
373  * Add vpe's API message handlers to the table.
374  * vlib has already mapped shared memory and
375  * added the client registration handlers.
376  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
377  */
378 #define vl_msg_name_crc_list
379 #include <vnet/vnet_all_api_h.h>
380 #undef vl_msg_name_crc_list
381 
382 static void
384 {
385 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
386  foreach_vl_msg_name_crc_sr;
387 #undef _
388 }
389 
390 static clib_error_t *
392 {
393  api_main_t *am = &api_main;
394 
395 #define _(N,n) \
396  vl_msg_api_set_handlers(VL_API_##N, #n, \
397  vl_api_##n##_t_handler, \
398  vl_noop_handler, \
399  vl_api_##n##_t_endian, \
400  vl_api_##n##_t_print, \
401  sizeof(vl_api_##n##_t), 1);
403 #undef _
404 
405  /*
406  * Set up the (msg_name, crc, message-id) table
407  */
409 
410  return 0;
411 }
412 
414 
415 /*
416  * fd.io coding-style-patch-verification: ON
417  *
418  * Local Variables:
419  * eval: (c-set-style "gnu")
420  * End:
421  */
ip6_sr_main_t sr_main
Definition: sr.c:31
u8 type
Type (default is 0)
Definition: sr.h:86
u32 vlan_index
VLAN tag (not an index)
Definition: sr.h:118
void sr_set_source(ip6_address_t *address)
int sr_cli_localsid(char is_del, ip6_address_t *localsid_addr, char end_psp, u8 behavior, u32 sw_if_index, u32 vlan_index, u32 fib_table, ip46_address_t *nh_addr, void *ls_plugin_mem)
SR localsid add/del.
Definition: sr_localsid.c:66
#define SR_BEHAVIOR_X
Definition: sr.h:38
SR LocalSID.
Definition: sr.h:102
u32 fib_table
FIB table.
Definition: sr.h:94
u8 as_u8[16]
Definition: ip6_packet.h:48
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:262
vl_api_srv6_sid_t bsid
Definition: sr.api:228
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
ip6_address_t * segments
SIDs (key)
Definition: sr.h:62
#define SR_BEHAVIOR_DT6
Definition: sr.h:44
u8 bsid_addr[16]
Definition: sr.api:81
static void vl_api_sr_policy_add_t_handler(vl_api_sr_policy_add_t *mp)
Definition: sr_api.c:95
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:560
for(i=1;i<=collision_buckets;i++)
int i
u16 behavior
Behavior associated to this localsid.
Definition: sr.h:108
vl_api_mprefix_t prefix
Definition: ip.api:456
void * vl_msg_api_alloc(int nbytes)
ip6_sr_steering_policy_t * steer_policies
Definition: sr.h:210
unsigned char u8
Definition: types.h:56
vl_api_srv6_sid_list_t sids
Definition: sr.api:110
#define SR_BEHAVIOR_DT4
Definition: sr.h:45
#define clib_memcpy(d, s, n)
Definition: string.h:180
static void vl_api_sr_steering_add_del_t_handler(vl_api_sr_steering_add_del_t *mp)
Definition: sr_api.c:183
struct sr_steering_key_t::@365::@367 l3
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
vl_api_srv6_sid_list_t sids
Definition: sr.api:86
static void vl_api_sr_policy_mod_t_handler(vl_api_sr_policy_mod_t *mp)
Definition: sr_api.c:124
SR Segment List (SID list)
Definition: sr.h:60
unsigned int u32
Definition: types.h:88
vl_api_srv6_sid_t addr
Definition: sr.api:179
struct sr_steering_key_t::@365::@368 l2
static void send_sr_localsid_details(ip6_sr_localsid_t *t, vl_api_registration_t *reg, u32 context)
Definition: sr_api.c:209
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
static void vl_api_sr_localsids_dump_t_handler(vl_api_sr_localsids_dump_t *mp)
Definition: sr_api.c:243
static void setup_message_id_table(api_main_t *am)
Definition: sr_api.c:383
static void send_sr_steering_pol_details(ip6_sr_steering_policy_t *t, vl_api_registration_t *reg, u32 context)
Definition: sr_api.c:321
char end_psp
Combined with End.PSP?
Definition: sr.h:106
sr_steering_key_t classify
Traffic classification.
Definition: sr.h:182
#define REPLY_MACRO(t)
#define SR_BEHAVIOR_DX4
Definition: sr.h:43
vl_api_srv6_sid_t bsid
Definition: sr.api:202
static void vl_api_sr_steering_pol_dump_t_handler(vl_api_sr_policies_dump_t *mp)
Definition: sr_api.c:353
u8 is_encap
Mode (0 is SRH insert, 1 Encaps)
Definition: sr.h:96
u32 sr_policy
SR Policy index.
Definition: sr.h:183
u32 weight
SID list weight (wECMP / UCMP)
Definition: sr.h:64
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:203
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:88
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
ip6_sr_localsid_t * localsids
Definition: sr.h:204
#define BAD_SW_IF_INDEX_LABEL
vl_api_srv6_sid_t localsid
Definition: sr.api:52
VLIB_API_INIT_FUNCTION(sr_api_hookup)
vl_api_srv6_sid_list_t sid_lists[num_sid_lists]
Definition: sr.api:207
vlib_main_t * vm
Definition: buffer.c:323
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
int sr_policy_del(ip6_address_t *bsid, u32 index)
Delete a SR policy.
int sr_policy_add(ip6_address_t *bsid, ip6_address_t *segments, u32 weight, u8 behavior, u32 fib_table, u8 is_encap)
Create a new SR policy.
u32 * segments_lists
SID lists indexes (vector)
Definition: sr.h:82
IPv6 SR Set SRv6 encapsulation source.
Definition: sr.api:133
static void vl_api_sr_policies_dump_t_handler(vl_api_sr_policies_dump_t *mp)
Definition: sr_api.c:302
IPv6 SR steering add/del.
Definition: sr.api:152
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
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:1069
IPv6 SR policy deletion.
Definition: sr.api:119
vl_api_srv6_sid_t bsid_addr
Definition: sr.api:123
#define foreach_vpe_api_msg
Definition: sr_api.c:47
u32 fib_table
FIB table where localsid is registered.
Definition: sr.h:116
#define SR_BEHAVIOR_DX2
Definition: sr.h:41
vl_api_srv6_sid_t sids[16]
Definition: sr.api:30
IPv6 SR policy add.
Definition: sr.api:77
#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:198
SR Policy.
Definition: sr.h:80
static void vl_api_sr_policy_del_t_handler(vl_api_sr_policy_del_t *mp)
Definition: sr_api.c:158
static clib_error_t * sr_api_hookup(vlib_main_t *vm)
Definition: sr_api.c:391
ip6_sr_sl_t * sid_lists
Definition: sr.h:195
Segment Routing data structures definitions.
Segment Routing main datastructure.
Definition: sr.h:189
IPv6 SR policy modification.
Definition: sr.api:100
#define vec_foreach(var, vec)
Vector iterator.
u8 traffic_type
Traffic type (IPv4, IPv6, L2)
Definition: sr.h:176
ip46_address_t next_hop
Next_hop for xconnect usage only.
Definition: sr.h:120
static void vl_api_sr_localsid_add_del_t_handler(vl_api_sr_localsid_add_del_t *mp)
Definition: sr_api.c:59
Dump the list of SR policies.
Definition: sr.api:193
#define SR_STEER_L2
Definition: sr.h:48
u32 sw_if_index
xconnect only
Definition: sr.h:112
IPv6 SR LocalSID add/del request.
Definition: sr.api:47
u32 context
Definition: gre.api:45
api_main_t api_main
Definition: api_shared.c:35
ip6_address_t localsid
LocalSID IPv6 address.
Definition: sr.h:104
u8 addr[16]
Definition: sr.api:23
static void vl_api_sr_set_encap_source_t_handler(vl_api_sr_set_encap_source_t *mp)
Definition: sr_api.c:173
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:84
#define SR_BEHAVIOR_DX6
Definition: sr.h:42
#define VALIDATE_SW_IF_INDEX(mp)
Dump the list of SR LocalSIDs.
Definition: sr.api:170