FD.io VPP  v21.01.1
Vector Packet Processing
sr_mpls_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. Licensed under the Apache
6  * License, Version 2.0 (the "License"); you may not use this file except in
7  * compliance with the License. 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, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * under the License.
16  * ------------------------------------------------------------------
17  */
18 
19 #include <vnet/vnet.h>
20 #include <vnet/srmpls/sr_mpls.h>
21 #include <vlibmemory/api.h>
22 
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/feature/feature.h>
26 
27 #include <vnet/ip/ip_types_api.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_MPLS_POLICY_DEL, sr_mpls_policy_del) \
49 _(SR_MPLS_STEERING_ADD_DEL, sr_mpls_steering_add_del) \
50 _(SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR, sr_mpls_policy_assign_endpoint_color)
51 
52 
53 static void
55 {
56  vl_api_sr_mpls_policy_add_reply_t *rmp;
57 
58  mpls_label_t *segments = 0, *seg;
59  mpls_label_t this_address = 0;
60 
61  int i;
62  for (i = 0; i < mp->n_segments; i++)
63  {
64  vec_add2 (segments, seg, 1);
65  this_address = ntohl (mp->segments[i]);
66  clib_memcpy (seg, &this_address, sizeof (this_address));
67  }
68 
69  int rv = 0;
70  rv = sr_mpls_policy_add (ntohl (mp->bsid),
71  segments, mp->is_spray, ntohl (mp->weight));
72  vec_free (segments);
73 
74  REPLY_MACRO (VL_API_SR_MPLS_POLICY_ADD_REPLY);
75 }
76 
77 static void
79 {
80  vl_api_sr_mpls_policy_mod_reply_t *rmp;
81 
82  mpls_label_t *segments = 0, *seg;
83  mpls_label_t this_address = 0;
84 
85  int i;
86  for (i = 0; i < mp->n_segments; i++)
87  {
88  vec_add2 (segments, seg, 1);
89  this_address = ntohl (mp->segments[i]);
90  clib_memcpy (seg, &this_address, sizeof (this_address));
91  }
92 
93  int rv = 0;
94  rv = sr_mpls_policy_mod (ntohl (mp->bsid),
95  ntohl (mp->operation), segments,
96  ntohl (mp->sl_index), ntohl (mp->weight));
97  vec_free (segments);
98 
99  REPLY_MACRO (VL_API_SR_MPLS_POLICY_MOD_REPLY);
100 }
101 
102 static void
104 {
105  vl_api_sr_mpls_policy_del_reply_t *rmp;
106  int rv = 0;
107  rv = sr_mpls_policy_del (ntohl (mp->bsid));
108 
109  REPLY_MACRO (VL_API_SR_MPLS_POLICY_DEL_REPLY);
110 }
111 
114 {
115  vl_api_sr_mpls_steering_add_del_reply_t *rmp;
117  ip46_address_t next_hop;
118  clib_memset (&prefix, 0, sizeof (ip46_address_t));
119 
120  ip_prefix_decode (&mp->prefix, &prefix);
121  ip_address_decode (&mp->next_hop, &next_hop);
122 
123  int rv = 0;
124  if (mp->is_del)
125  rv = sr_mpls_steering_policy_del (&prefix.fp_addr,
126  prefix.fp_len,
127  ip46_address_is_ip4 (&prefix.fp_addr) ?
129  ntohl (mp->table_id),
130  ntohl (mp->color));
131  else
132  rv = sr_mpls_steering_policy_add (ntohl (mp->bsid),
133  ntohl (mp->table_id),
134  &prefix.fp_addr,
135  prefix.fp_len,
136  ip46_address_is_ip4 (&prefix.fp_addr) ?
137  SR_STEER_IPV4 : SR_STEER_IPV6,
138  &next_hop,
139  ip46_address_is_ip4 (&next_hop) ?
140  SR_STEER_IPV4 : SR_STEER_IPV6,
141  ntohl (mp->color), mp->co_bits,
142  ntohl (mp->vpn_label));
143 
144  REPLY_MACRO (VL_API_SR_MPLS_STEERING_ADD_DEL_REPLY);
145 }
146 
149 {
150  vl_api_sr_mpls_policy_assign_endpoint_color_reply_t *rmp;
151  int rv = 0;
152 
153  ip46_address_t endpoint;
154  clib_memset (&endpoint, 0, sizeof (ip46_address_t));
155  ip_address_decode (&mp->endpoint, &endpoint);
156 
157  rv = sr_mpls_policy_assign_endpoint_color (ntohl (mp->bsid),
158  &endpoint,
159  ip46_address_is_ip4 (&endpoint) ?
161  ntohl (mp->color));
162 
163  REPLY_MACRO (VL_API_SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR_REPLY);
164 }
165 
166 /*
167  * sr_mpls_api_hookup Add vpe's API message handlers to the table. vlib has
168  * already mapped shared memory and added the client registration handlers.
169  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
170  */
171 #define vl_msg_name_crc_list
172 #include <vnet/vnet_all_api_h.h>
173 #undef vl_msg_name_crc_list
174 
175 static void
177 {
178 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
179  foreach_vl_msg_name_crc_sr_mpls;
180 #undef _
181 }
182 
183 static clib_error_t *
185 {
186  api_main_t *am = vlibapi_get_main ();
187 
188 #define _(N,n) \
189  vl_msg_api_set_handlers(VL_API_##N, #n, \
190  vl_api_##n##_t_handler, \
191  vl_noop_handler, \
192  vl_api_##n##_t_endian, \
193  vl_api_##n##_t_print, \
194  sizeof(vl_api_##n##_t), 1);
196 #undef _
197 
198  /*
199  * Manually register the sr policy add msg, so we trace enough bytes
200  * to capture a typical segment list
201  */
202  vl_msg_api_set_handlers (VL_API_SR_MPLS_POLICY_ADD,
203  "sr_mpls_policy_add",
206  vl_api_sr_mpls_policy_add_t_endian,
208 
209  /*
210  * Manually register the sr policy mod msg, so we trace enough bytes
211  * to capture a typical segment list
212  */
213  vl_msg_api_set_handlers (VL_API_SR_MPLS_POLICY_MOD,
214  "sr_mpls_policy_mod",
217  vl_api_sr_mpls_policy_mod_t_endian,
218  vl_api_sr_mpls_policy_mod_t_print, 256, 1);
219 
220  /*
221  * Set up the (msg_name, crc, message-id) table
222  */
224 
225  return 0;
226 }
227 
229 
230 /*
231  * fd.io coding-style-patch-verification: ON
232  *
233  * Local Variables: eval: (c-set-style "gnu") End:
234  */
static void * vl_api_sr_mpls_policy_add_t_print(vl_api_sr_mpls_policy_add_t *mp, void *handle)
Definition: custom_dump.c:905
#define SR_STEER_IPV6
Definition: sr_mpls.h:42
bool is_del[default=false]
Definition: sr_mpls.api:94
#define SR_STEER_IPV4
Definition: sr_mpls.h:41
void ip_prefix_decode(const vl_api_prefix_t *in, fib_prefix_t *out)
Definition: ip_types_api.c:259
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
void vl_noop_handler(void *mp)
Definition: api_shared.c:941
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:630
vlib_main_t * vm
Definition: in2out_ed.c:1580
vl_api_prefix_t prefix
Definition: ip.api:144
static u8 ip46_address_is_ip4(const ip46_address_t *ip46)
Definition: ip46_address.h:55
u32 mpls_label_t
A label value only, i.e.
Definition: packet.h:26
MPLS SR steering add/del.
Definition: sr_mpls.api:90
MPLS SR policy modification.
Definition: sr_mpls.api:53
#define clib_memcpy(d, s, n)
Definition: string.h:180
static void vl_api_sr_mpls_policy_mod_t_handler(vl_api_sr_mpls_policy_mod_t *mp)
Definition: sr_mpls_api.c:78
static void vl_api_sr_mpls_steering_add_del_t_handler(vl_api_sr_mpls_steering_add_del_t *mp)
Definition: sr_mpls_api.c:113
static clib_error_t * sr_mpls_api_hookup(vlib_main_t *vm)
Definition: sr_mpls_api.c:184
int sr_mpls_policy_add(mpls_label_t bsid, mpls_label_t *segments, u8 behavior, u32 weight)
Create a new SR policy.
vl_api_sr_policy_op_t operation
Definition: sr_mpls.api:58
Aggregate type for a prefix.
Definition: fib_types.h:202
u16 fp_len
The mask length.
Definition: fib_types.h:206
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:186
static void setup_message_id_table(api_main_t *am)
Definition: sr_mpls_api.c:176
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:225
#define REPLY_MACRO(t)
void vl_msg_api_set_handlers(int msg_id, char *msg_name, void *handler, void *cleanup, void *endian, void *print, int msg_size, int traced)
Definition: api_shared.c:858
int sr_mpls_steering_policy_del(ip46_address_t *prefix, u32 mask_width, u8 traffic_type, u32 table_id, u32 color)
Delete steering rule for an SR-MPLS policy.
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:227
vl_api_address_t endpoint
Definition: wireguard.api:103
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
static void vl_api_sr_mpls_policy_del_t_handler(vl_api_sr_mpls_policy_del_t *mp)
Definition: sr_mpls_api.c:103
u32 segments[n_segments]
Definition: sr_mpls.api:62
u32 segments[n_segments]
Definition: sr_mpls.api:38
int sr_mpls_policy_del(mpls_label_t bsid)
Delete a SR policy.
MPLS SR policy add.
Definition: sr_mpls.api:30
#define foreach_vpe_api_msg
Definition: sr_mpls_api.c:47
static void vl_api_sr_mpls_policy_assign_endpoint_color_t_handler(vl_api_sr_mpls_policy_assign_endpoint_color_t *mp)
Definition: sr_mpls_api.c:148
VLIB_API_INIT_FUNCTION(sr_mpls_api_hookup)
static void vl_api_sr_mpls_policy_add_t_handler(vl_api_sr_mpls_policy_add_t *mp)
Definition: sr_mpls_api.c:54
MPLS SR policy deletion.
Definition: sr_mpls.api:70
int sr_mpls_policy_assign_endpoint_color(mpls_label_t bsid, ip46_address_t *endpoint, u8 endpoint_type, u32 color)
Update the Endpoint,Color tuple of an SR policy.
Segment Routing MPLS data structures definitions.
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:389
int sr_mpls_policy_mod(mpls_label_t bsid, u8 operation, mpls_label_t *segments, u32 sl_index, u32 weight)
Modify an existing SR policy.
int sr_mpls_steering_policy_add(mpls_label_t bsid, u32 table_id, ip46_address_t *prefix, u32 mask_width, u8 traffic_type, ip46_address_t *next_hop, u8 nh_type, u32 color, char co_bits, mpls_label_t vpn_label)
Steer traffic L3 traffic through a given SR-MPLS policy.