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/ip/ip_types_api.h>
29 #include <vnet/vnet_msg_enum.h>
31 #define vl_typedefs /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
35 #define vl_endianfun /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #include <vnet/vnet_all_api_h.h>
45 #include <vlibapi/api_helper_macros.h>
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)
54 vl_api_sr_mpls_policy_add_t_handler (vl_api_sr_mpls_policy_add_t * mp)
56 vl_api_sr_mpls_policy_add_reply_t *rmp;
58 mpls_label_t *segments = 0, *seg;
59 mpls_label_t this_address = 0;
62 for (i = 0; i < mp->n_segments; i++)
64 vec_add2 (segments, seg, 1);
65 this_address = ntohl (mp->segments[i]);
66 clib_memcpy (seg, &this_address, sizeof (this_address));
70 rv = sr_mpls_policy_add (ntohl (mp->bsid),
71 segments, mp->is_spray, ntohl (mp->weight));
74 REPLY_MACRO (VL_API_SR_MPLS_POLICY_ADD_REPLY);
78 vl_api_sr_mpls_policy_mod_t_handler (vl_api_sr_mpls_policy_mod_t * mp)
80 vl_api_sr_mpls_policy_mod_reply_t *rmp;
82 mpls_label_t *segments = 0, *seg;
83 mpls_label_t this_address = 0;
86 for (i = 0; i < mp->n_segments; i++)
88 vec_add2 (segments, seg, 1);
89 this_address = ntohl (mp->segments[i]);
90 clib_memcpy (seg, &this_address, sizeof (this_address));
94 rv = sr_mpls_policy_mod (ntohl (mp->bsid),
95 ntohl (mp->operation), segments,
96 ntohl (mp->sl_index), ntohl (mp->weight));
99 REPLY_MACRO (VL_API_SR_MPLS_POLICY_MOD_REPLY);
103 vl_api_sr_mpls_policy_del_t_handler (vl_api_sr_mpls_policy_del_t * mp)
105 vl_api_sr_mpls_policy_del_reply_t *rmp;
107 rv = sr_mpls_policy_del (ntohl (mp->bsid));
109 REPLY_MACRO (VL_API_SR_MPLS_POLICY_DEL_REPLY);
112 static void vl_api_sr_mpls_steering_add_del_t_handler
113 (vl_api_sr_mpls_steering_add_del_t * mp)
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));
120 ip_prefix_decode (&mp->prefix, &prefix);
121 ip_address_decode (&mp->next_hop, &next_hop);
125 rv = sr_mpls_steering_policy_del (&prefix.fp_addr,
127 ip46_address_is_ip4 (&prefix.fp_addr) ?
128 SR_STEER_IPV4 : SR_STEER_IPV6,
129 ntohl (mp->table_id),
132 rv = sr_mpls_steering_policy_add (ntohl (mp->bsid),
133 ntohl (mp->table_id),
136 ip46_address_is_ip4 (&prefix.fp_addr) ?
137 SR_STEER_IPV4 : SR_STEER_IPV6,
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));
144 REPLY_MACRO (VL_API_SR_MPLS_STEERING_ADD_DEL_REPLY);
147 static void vl_api_sr_mpls_policy_assign_endpoint_color_t_handler
148 (vl_api_sr_mpls_policy_assign_endpoint_color_t * mp)
150 vl_api_sr_mpls_policy_assign_endpoint_color_reply_t *rmp;
153 ip46_address_t endpoint;
154 clib_memset (&endpoint, 0, sizeof (ip46_address_t));
155 ip_address_decode (&mp->endpoint, &endpoint);
157 rv = sr_mpls_policy_assign_endpoint_color (ntohl (mp->bsid),
159 ip46_address_is_ip4 (&endpoint) ?
160 SR_STEER_IPV4 : SR_STEER_IPV6,
163 REPLY_MACRO (VL_API_SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR_REPLY);
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()
171 #define vl_msg_name_crc_list
172 #include <vnet/vnet_all_api_h.h>
173 #undef vl_msg_name_crc_list
176 setup_message_id_table (api_main_t * am)
178 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
179 foreach_vl_msg_name_crc_sr_mpls;
183 static clib_error_t *
184 sr_mpls_api_hookup (vlib_main_t * vm)
186 api_main_t *am = vlibapi_get_main ();
189 vl_msg_api_set_handlers(VL_API_##N, #n, \
190 vl_api_##n##_t_handler, \
192 vl_api_##n##_t_endian, \
193 vl_api_##n##_t_print, \
194 sizeof(vl_api_##n##_t), 1);
199 * Manually register the sr policy add msg, so we trace enough bytes
200 * to capture a typical segment list
202 vl_msg_api_set_handlers (VL_API_SR_MPLS_POLICY_ADD,
203 "sr_mpls_policy_add",
204 vl_api_sr_mpls_policy_add_t_handler,
206 vl_api_sr_mpls_policy_add_t_endian,
207 vl_api_sr_mpls_policy_add_t_print, 256, 1);
210 * Manually register the sr policy mod msg, so we trace enough bytes
211 * to capture a typical segment list
213 vl_msg_api_set_handlers (VL_API_SR_MPLS_POLICY_MOD,
214 "sr_mpls_policy_mod",
215 vl_api_sr_mpls_policy_mod_t_handler,
217 vl_api_sr_mpls_policy_mod_t_endian,
218 vl_api_sr_mpls_policy_mod_t_print, 256, 1);
221 * Set up the (msg_name, crc, message-id) table
223 setup_message_id_table (am);
228 VLIB_API_INIT_FUNCTION (sr_mpls_api_hookup);
231 * fd.io coding-style-patch-verification: ON
233 * Local Variables: eval: (c-set-style "gnu") End: