2 * ------------------------------------------------------------------
3 * sr_api.c - ipv6 segment routing api
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ------------------------------------------------------------------
19 #include <vnet/vnet.h>
20 #include <vnet/srmpls/sr_mpls.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/feature/feature.h>
27 #include <vnet/vnet_msg_enum.h>
29 #define vl_typedefs /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
33 #define vl_endianfun /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40 #include <vnet/vnet_all_api_h.h>
43 #include <vlibapi/api_helper_macros.h>
45 #define foreach_vpe_api_msg \
46 _(SR_MPLS_POLICY_DEL, sr_mpls_policy_del) \
47 _(SR_MPLS_STEERING_ADD_DEL, sr_mpls_steering_add_del) \
48 _(SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR, sr_mpls_policy_assign_endpoint_color)
52 vl_api_sr_mpls_policy_add_t_handler (vl_api_sr_mpls_policy_add_t * mp)
54 vl_api_sr_mpls_policy_add_reply_t *rmp;
56 mpls_label_t *segments = 0, *seg;
57 mpls_label_t this_address = 0;
60 for (i = 0; i < mp->n_segments; i++)
62 vec_add2 (segments, seg, 1);
63 this_address = ntohl (mp->segments[i]);
64 clib_memcpy (seg, &this_address, sizeof (this_address));
68 rv = sr_mpls_policy_add (ntohl (mp->bsid),
69 segments, mp->type, ntohl (mp->weight));
71 REPLY_MACRO (VL_API_SR_MPLS_POLICY_ADD_REPLY);
75 vl_api_sr_mpls_policy_mod_t_handler (vl_api_sr_mpls_policy_mod_t * mp)
77 vl_api_sr_mpls_policy_mod_reply_t *rmp;
79 mpls_label_t *segments = 0, *seg;
80 mpls_label_t this_address = 0;
83 for (i = 0; i < mp->n_segments; i++)
85 vec_add2 (segments, seg, 1);
86 this_address = ntohl (mp->segments[i]);
87 clib_memcpy (seg, &this_address, sizeof (this_address));
91 rv = sr_mpls_policy_mod (ntohl (mp->bsid),
92 mp->operation, segments, ntohl (mp->sl_index),
95 REPLY_MACRO (VL_API_SR_MPLS_POLICY_MOD_REPLY);
99 vl_api_sr_mpls_policy_del_t_handler (vl_api_sr_mpls_policy_del_t * mp)
101 vl_api_sr_mpls_policy_del_reply_t *rmp;
103 rv = sr_mpls_policy_del (ntohl (mp->bsid));
105 REPLY_MACRO (VL_API_SR_MPLS_POLICY_DEL_REPLY);
108 static void vl_api_sr_mpls_steering_add_del_t_handler
109 (vl_api_sr_mpls_steering_add_del_t * mp)
111 vl_api_sr_mpls_steering_add_del_reply_t *rmp;
112 ip46_address_t prefix;
113 memset (&prefix, 0, sizeof (ip46_address_t));
114 if (mp->traffic_type == SR_STEER_IPV4)
115 memcpy (&prefix.ip4, mp->prefix_addr, sizeof (prefix.ip4));
117 memcpy (&prefix, mp->prefix_addr, sizeof (prefix.ip6));
121 rv = sr_mpls_steering_policy_del (&prefix,
122 ntohl (mp->mask_width),
124 ntohl (mp->table_id),
127 rv = sr_mpls_steering_policy_add (ntohl (mp->bsid),
128 ntohl (mp->table_id),
130 ntohl (mp->mask_width),
132 (ip46_address_t *) & mp->next_hop,
134 ntohl (mp->color), mp->co_bits,
135 ntohl (mp->vpn_label));
137 REPLY_MACRO (VL_API_SR_MPLS_STEERING_ADD_DEL_REPLY);
140 static void vl_api_sr_mpls_policy_assign_endpoint_color_t_handler
141 (vl_api_sr_mpls_policy_assign_endpoint_color_t * mp)
143 vl_api_sr_mpls_policy_assign_endpoint_color_reply_t *rmp;
146 ip46_address_t endpoint;
147 memset (&endpoint, 0, sizeof (ip46_address_t));
148 if (mp->endpoint_type == SR_STEER_IPV4)
149 memcpy (&endpoint.ip4, mp->endpoint, sizeof (endpoint.ip4));
151 memcpy (&endpoint, mp->endpoint, sizeof (endpoint.ip6));
153 rv = sr_mpls_policy_assign_endpoint_color (ntohl (mp->bsid),
154 &endpoint, mp->endpoint_type,
157 REPLY_MACRO (VL_API_SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR_REPLY);
161 * sr_mpls_api_hookup Add vpe's API message handlers to the table. vlib has
162 * alread mapped shared memory and added the client registration handlers.
163 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
165 #define vl_msg_name_crc_list
166 #include <vnet/vnet_all_api_h.h>
167 #undef vl_msg_name_crc_list
170 setup_message_id_table (api_main_t * am)
172 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
173 foreach_vl_msg_name_crc_sr_mpls;
177 static clib_error_t *
178 sr_mpls_api_hookup (vlib_main_t * vm)
180 api_main_t *am = &api_main;
183 vl_msg_api_set_handlers(VL_API_##N, #n, \
184 vl_api_##n##_t_handler, \
186 vl_api_##n##_t_endian, \
187 vl_api_##n##_t_print, \
188 sizeof(vl_api_##n##_t), 1);
193 * Manually register the sr policy add msg, so we trace enough bytes
194 * to capture a typical segment list
196 vl_msg_api_set_handlers (VL_API_SR_MPLS_POLICY_ADD,
197 "sr_mpls_policy_add",
198 vl_api_sr_mpls_policy_add_t_handler,
200 vl_api_sr_mpls_policy_add_t_endian,
201 vl_api_sr_mpls_policy_add_t_print, 256, 1);
204 * Manually register the sr policy mod msg, so we trace enough bytes
205 * to capture a typical segment list
207 vl_msg_api_set_handlers (VL_API_SR_MPLS_POLICY_MOD,
208 "sr_mpls_policy_mod",
209 vl_api_sr_mpls_policy_mod_t_handler,
211 vl_api_sr_mpls_policy_mod_t_endian,
212 vl_api_sr_mpls_policy_mod_t_print, 256, 1);
215 * Set up the (msg_name, crc, message-id) table
217 setup_message_id_table (am);
222 VLIB_API_INIT_FUNCTION (sr_mpls_api_hookup);
225 * fd.io coding-style-patch-verification: ON
227 * Local Variables: eval: (c-set-style "gnu") End: