2 *------------------------------------------------------------------
3 * sr_api.c - ipv6 segment routing api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.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_MULTICAST_MAP_ADD_DEL, sr_multicast_map_add_del)
48 static void vl_api_sr_tunnel_add_del_t_handler
49 (vl_api_sr_tunnel_add_del_t * mp)
52 clib_warning ("unimplemented");
54 ip6_sr_add_del_tunnel_args_t _a, *a = &_a;
56 vl_api_sr_tunnel_add_del_reply_t *rmp;
57 ip6_address_t *segments = 0, *seg;
58 ip6_address_t *tags = 0, *tag;
59 ip6_address_t *this_address;
62 if (mp->n_segments == 0)
68 memset (a, 0, sizeof (*a));
69 a->src_address = (ip6_address_t *) & mp->src_address;
70 a->dst_address = (ip6_address_t *) & mp->dst_address;
71 a->dst_mask_width = mp->dst_mask_width;
72 a->flags_net_byte_order = mp->flags_net_byte_order;
73 a->is_del = (mp->is_add == 0);
74 a->rx_table_id = ntohl (mp->outer_vrf_id);
75 a->tx_table_id = ntohl (mp->inner_vrf_id);
77 a->name = format (0, "%s", mp->name);
78 if (!(vec_len (a->name)))
81 a->policy_name = format (0, "%s", mp->policy_name);
82 if (!(vec_len (a->policy_name)))
85 /* Yank segments and tags out of the API message */
86 this_address = (ip6_address_t *) mp->segs_and_tags;
87 for (i = 0; i < mp->n_segments; i++)
89 vec_add2 (segments, seg, 1);
90 clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
93 for (i = 0; i < mp->n_tags; i++)
95 vec_add2 (tags, tag, 1);
96 clib_memcpy (tag->as_u8, this_address->as_u8, sizeof (*this_address));
100 a->segments = segments;
103 rv = ip6_sr_add_del_tunnel (a);
107 REPLY_MACRO (VL_API_SR_TUNNEL_ADD_DEL_REPLY);
111 static void vl_api_sr_policy_add_del_t_handler
112 (vl_api_sr_policy_add_del_t * mp)
115 clib_warning ("unimplemented");
117 ip6_sr_add_del_policy_args_t _a, *a = &_a;
119 vl_api_sr_policy_add_del_reply_t *rmp;
122 memset (a, 0, sizeof (*a));
123 a->is_del = (mp->is_add == 0);
125 a->name = format (0, "%s", mp->name);
126 if (!(vec_len (a->name)))
128 rv = VNET_API_ERROR_NO_SUCH_NODE2;
132 if (!(mp->tunnel_names[0]))
134 rv = VNET_API_ERROR_NO_SUCH_NODE2;
138 // start deserializing tunnel_names
139 int num_tunnels = mp->tunnel_names[0]; //number of tunnels
140 u8 *deser_tun_names = mp->tunnel_names;
141 deser_tun_names += 1; //moving along
144 int tun_name_len = 0;
146 for (i = 0; i < num_tunnels; i++)
148 tun_name_len = *deser_tun_names;
149 deser_tun_names += 1;
150 vec_resize (tun_name, tun_name_len);
151 memcpy (tun_name, deser_tun_names, tun_name_len);
152 vec_add1 (a->tunnel_names, tun_name);
153 deser_tun_names += tun_name_len;
157 rv = ip6_sr_add_del_policy (a);
161 REPLY_MACRO (VL_API_SR_POLICY_ADD_DEL_REPLY);
165 static void vl_api_sr_multicast_map_add_del_t_handler
166 (vl_api_sr_multicast_map_add_del_t * mp)
169 clib_warning ("unimplemented");
171 ip6_sr_add_del_multicastmap_args_t _a, *a = &_a;
173 vl_api_sr_multicast_map_add_del_reply_t *rmp;
175 memset (a, 0, sizeof (*a));
176 a->is_del = (mp->is_add == 0);
178 a->multicast_address = (ip6_address_t *) & mp->multicast_address;
179 a->policy_name = format (0, "%s", mp->policy_name);
181 if (a->multicast_address == 0)
187 if (!(a->policy_name))
193 #if DPDK > 0 /* Cannot call replicate without DPDK */
194 rv = ip6_sr_add_del_multicastmap (a);
196 clib_warning ("multicast replication without DPDK not implemented");
197 rv = VNET_API_ERROR_UNIMPLEMENTED;
202 REPLY_MACRO (VL_API_SR_MULTICAST_MAP_ADD_DEL_REPLY);
208 * Add vpe's API message handlers to the table.
209 * vlib has alread mapped shared memory and
210 * added the client registration handlers.
211 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
213 #define vl_msg_name_crc_list
214 #include <vnet/vnet_all_api_h.h>
215 #undef vl_msg_name_crc_list
218 setup_message_id_table (api_main_t * am)
220 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
221 foreach_vl_msg_name_crc_sr;
225 static clib_error_t *
226 sr_api_hookup (vlib_main_t * vm)
228 api_main_t *am = &api_main;
231 vl_msg_api_set_handlers(VL_API_##N, #n, \
232 vl_api_##n##_t_handler, \
234 vl_api_##n##_t_endian, \
235 vl_api_##n##_t_print, \
236 sizeof(vl_api_##n##_t), 1);
241 * Manually register the sr tunnel add del msg, so we trace
242 * enough bytes to capture a typical segment list
244 vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL,
246 vl_api_sr_tunnel_add_del_t_handler,
248 vl_api_sr_tunnel_add_del_t_endian,
249 vl_api_sr_tunnel_add_del_t_print, 256, 1);
253 * Manually register the sr policy add del msg, so we trace
254 * enough bytes to capture a typical tunnel name list
256 vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD_DEL,
258 vl_api_sr_policy_add_del_t_handler,
260 vl_api_sr_policy_add_del_t_endian,
261 vl_api_sr_policy_add_del_t_print, 256, 1);
264 * Set up the (msg_name, crc, message-id) table
266 setup_message_id_table (am);
271 VLIB_API_INIT_FUNCTION (sr_api_hookup);
274 * fd.io coding-style-patch-verification: ON
277 * eval: (c-set-style "gnu")