blob: 623f672a20400785ddfb87351f03b942695d27cd [file] [log] [blame]
Pavel Kotucek0f971d82017-01-03 10:48:54 +01001/*
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>
Pablo Camarillo5d73eec2017-04-24 17:51:56 +020021#include <vnet/srv6/sr.h>
Pavel Kotucek0f971d82017-01-03 10:48:54 +010022#include <vlibmemory/api.h>
23
24#include <vnet/interface.h>
25#include <vnet/api_errno.h>
26#include <vnet/feature/feature.h>
27
28#include <vnet/vnet_msg_enum.h>
29
30#define vl_typedefs /* define message structures */
31#include <vnet/vnet_all_api_h.h>
32#undef vl_typedefs
33
34#define vl_endianfun /* define message structures */
35#include <vnet/vnet_all_api_h.h>
36#undef vl_endianfun
37
38/* instantiate all the print functions we know about */
39#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40#define vl_printfun
41#include <vnet/vnet_all_api_h.h>
42#undef vl_printfun
43
44#include <vlibapi/api_helper_macros.h>
45
46#define foreach_vpe_api_msg \
Pablo Camarillofb380952016-12-07 18:34:18 +010047_(SR_LOCALSID_ADD_DEL, sr_localsid_add_del) \
48_(SR_POLICY_DEL, sr_policy_del) \
49_(SR_STEERING_ADD_DEL, sr_steering_add_del)
50//_(SR_LOCALSIDS, sr_localsids_dump)
51//_(SR_LOCALSID_BEHAVIORS, sr_localsid_behaviors_dump)
Pavel Kotucek0f971d82017-01-03 10:48:54 +010052
Pablo Camarillofb380952016-12-07 18:34:18 +010053static void vl_api_sr_localsid_add_del_t_handler
54 (vl_api_sr_localsid_add_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +010055{
Pablo Camarillofb380952016-12-07 18:34:18 +010056 vl_api_sr_localsid_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +010057 int rv = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +010058/*
59 * int sr_cli_localsid (char is_del, ip6_address_t *localsid_addr,
60 * char end_psp, u8 behavior, u32 sw_if_index, u32 vlan_index, u32 fib_table,
61 * ip46_address_t *nh_addr, void *ls_plugin_mem)
62 */
Chris Luke879ace32017-09-26 13:15:16 -040063
64 VALIDATE_SW_IF_INDEX (mp);
65
Pablo Camarillofb380952016-12-07 18:34:18 +010066 rv = sr_cli_localsid (mp->is_del,
67 (ip6_address_t *) & mp->localsid_addr,
68 mp->end_psp,
69 mp->behavior,
70 ntohl (mp->sw_if_index),
71 ntohl (mp->vlan_index),
72 ntohl (mp->fib_table),
73 (ip46_address_t *) & mp->nh_addr, NULL);
74
Chris Luke879ace32017-09-26 13:15:16 -040075 BAD_SW_IF_INDEX_LABEL;
76
Pablo Camarillofb380952016-12-07 18:34:18 +010077 REPLY_MACRO (VL_API_SR_LOCALSID_ADD_DEL_REPLY);
78}
79
80static void
81vl_api_sr_policy_add_t_handler (vl_api_sr_policy_add_t * mp)
82{
83 vl_api_sr_policy_add_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +010084 ip6_address_t *segments = 0, *seg;
Pablo Camarillofb380952016-12-07 18:34:18 +010085 ip6_address_t *this_address = (ip6_address_t *) mp->segments;
86
Pavel Kotucek0f971d82017-01-03 10:48:54 +010087 int i;
Pavel Kotucek0f971d82017-01-03 10:48:54 +010088 for (i = 0; i < mp->n_segments; i++)
89 {
90 vec_add2 (segments, seg, 1);
91 clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
92 this_address++;
93 }
Pablo Camarillofb380952016-12-07 18:34:18 +010094
95/*
96 * sr_policy_add (ip6_address_t *bsid, ip6_address_t *segments,
97 * u32 weight, u8 behavior, u32 fib_table, u8 is_encap)
98 */
99 int rv = 0;
100 rv = sr_policy_add ((ip6_address_t *) & mp->bsid_addr,
101 segments,
102 ntohl (mp->weight),
103 mp->type, ntohl (mp->fib_table), mp->is_encap);
104
105 REPLY_MACRO (VL_API_SR_POLICY_ADD_REPLY);
106}
107
108static void
109vl_api_sr_policy_mod_t_handler (vl_api_sr_policy_mod_t * mp)
110{
111 vl_api_sr_policy_mod_reply_t *rmp;
112
113 ip6_address_t *segments = 0, *seg;
114 ip6_address_t *this_address = (ip6_address_t *) mp->segments;
115
116 int i;
117 for (i = 0; i < mp->n_segments; i++)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100118 {
Pablo Camarillofb380952016-12-07 18:34:18 +0100119 vec_add2 (segments, seg, 1);
120 clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100121 this_address++;
122 }
123
Pablo Camarillofb380952016-12-07 18:34:18 +0100124 int rv = 0;
125/*
126 * int
127 * sr_policy_mod(ip6_address_t *bsid, u32 index, u32 fib_table,
128 * u8 operation, ip6_address_t *segments, u32 sl_index,
129 * u32 weight, u8 is_encap)
130 */
131 rv = sr_policy_mod ((ip6_address_t *) & mp->bsid_addr,
132 ntohl (mp->sr_policy_index),
133 ntohl (mp->fib_table),
134 mp->operation,
135 segments, ntohl (mp->sl_index), ntohl (mp->weight));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100136
Pablo Camarillofb380952016-12-07 18:34:18 +0100137 REPLY_MACRO (VL_API_SR_POLICY_MOD_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100138}
139
Pablo Camarillofb380952016-12-07 18:34:18 +0100140static void
141vl_api_sr_policy_del_t_handler (vl_api_sr_policy_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100142{
Pablo Camarillofb380952016-12-07 18:34:18 +0100143 vl_api_sr_policy_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100144 int rv = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +0100145/*
146 * int
147 * sr_policy_del (ip6_address_t *bsid, u32 index)
148 */
149 rv = sr_policy_del ((ip6_address_t *) & mp->bsid_addr,
150 ntohl (mp->sr_policy_index));
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100151
Pablo Camarillofb380952016-12-07 18:34:18 +0100152 REPLY_MACRO (VL_API_SR_POLICY_DEL_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100153}
154
Pablo Camarillofb380952016-12-07 18:34:18 +0100155static void vl_api_sr_steering_add_del_t_handler
156 (vl_api_sr_steering_add_del_t * mp)
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100157{
Pablo Camarillofb380952016-12-07 18:34:18 +0100158 vl_api_sr_steering_add_del_reply_t *rmp;
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100159 int rv = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +0100160/*
161 * int
162 * sr_steering_policy(int is_del, ip6_address_t *bsid, u32 sr_policy_index,
163 * u32 table_id, ip46_address_t *prefix, u32 mask_width, u32 sw_if_index,
164 * u8 traffic_type)
165 */
Chris Luke879ace32017-09-26 13:15:16 -0400166
167 VALIDATE_SW_IF_INDEX (mp);
168
Pablo Camarillofb380952016-12-07 18:34:18 +0100169 rv = sr_steering_policy (mp->is_del,
170 (ip6_address_t *) & mp->bsid_addr,
171 ntohl (mp->sr_policy_index),
172 ntohl (mp->table_id),
173 (ip46_address_t *) & mp->prefix_addr,
174 ntohl (mp->mask_width),
175 ntohl (mp->sw_if_index), mp->traffic_type);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100176
Chris Luke879ace32017-09-26 13:15:16 -0400177 BAD_SW_IF_INDEX_LABEL;
178
Pablo Camarillofb380952016-12-07 18:34:18 +0100179 REPLY_MACRO (VL_API_SR_STEERING_ADD_DEL_REPLY);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100180}
181
182/*
183 * sr_api_hookup
184 * Add vpe's API message handlers to the table.
185 * vlib has alread mapped shared memory and
186 * added the client registration handlers.
187 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
188 */
189#define vl_msg_name_crc_list
190#include <vnet/vnet_all_api_h.h>
191#undef vl_msg_name_crc_list
192
193static void
194setup_message_id_table (api_main_t * am)
195{
196#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
197 foreach_vl_msg_name_crc_sr;
198#undef _
199}
200
201static clib_error_t *
202sr_api_hookup (vlib_main_t * vm)
203{
204 api_main_t *am = &api_main;
205
206#define _(N,n) \
207 vl_msg_api_set_handlers(VL_API_##N, #n, \
208 vl_api_##n##_t_handler, \
209 vl_noop_handler, \
210 vl_api_##n##_t_endian, \
211 vl_api_##n##_t_print, \
212 sizeof(vl_api_##n##_t), 1);
213 foreach_vpe_api_msg;
214#undef _
215
216 /*
Pablo Camarillofb380952016-12-07 18:34:18 +0100217 * Manually register the sr policy add msg, so we trace
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100218 * enough bytes to capture a typical segment list
219 */
Pablo Camarillofb380952016-12-07 18:34:18 +0100220 vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD,
221 "sr_policy_add",
222 vl_api_sr_policy_add_t_handler,
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100223 vl_noop_handler,
Pablo Camarillofb380952016-12-07 18:34:18 +0100224 vl_api_sr_policy_add_t_endian,
225 vl_api_sr_policy_add_t_print, 256, 1);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100226
227 /*
Pablo Camarillofb380952016-12-07 18:34:18 +0100228 * Manually register the sr policy mod msg, so we trace
229 * enough bytes to capture a typical segment list
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100230 */
Pablo Camarillofb380952016-12-07 18:34:18 +0100231 vl_msg_api_set_handlers (VL_API_SR_POLICY_MOD,
232 "sr_policy_mod",
233 vl_api_sr_policy_mod_t_handler,
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100234 vl_noop_handler,
Pablo Camarillofb380952016-12-07 18:34:18 +0100235 vl_api_sr_policy_mod_t_endian,
236 vl_api_sr_policy_mod_t_print, 256, 1);
Pavel Kotucek0f971d82017-01-03 10:48:54 +0100237
238 /*
239 * Set up the (msg_name, crc, message-id) table
240 */
241 setup_message_id_table (am);
242
243 return 0;
244}
245
246VLIB_API_INIT_FUNCTION (sr_api_hookup);
247
248/*
249 * fd.io coding-style-patch-verification: ON
250 *
251 * Local Variables:
252 * eval: (c-set-style "gnu")
253 * End:
254 */