sr_mpls_api.c revision 39d69112
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/vnet_msg_enum.h>
28
29#define vl_typedefs		/* define message structures */
30#include <vnet/vnet_all_api_h.h>
31#undef vl_typedefs
32
33#define vl_endianfun		/* define message structures */
34#include <vnet/vnet_all_api_h.h>
35#undef vl_endianfun
36
37/* instantiate all the print functions we know about */
38#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39#define vl_printfun
40#include <vnet/vnet_all_api_h.h>
41#undef vl_printfun
42
43#include <vlibapi/api_helper_macros.h>
44
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)
49
50
51static void
52vl_api_sr_mpls_policy_add_t_handler (vl_api_sr_mpls_policy_add_t * mp)
53{
54  vl_api_sr_mpls_policy_add_reply_t *rmp;
55
56  mpls_label_t *segments = 0, *seg;
57  mpls_label_t this_address = 0;
58
59  int i;
60  for (i = 0; i < mp->n_segments; i++)
61    {
62      vec_add2 (segments, seg, 1);
63      this_address = ntohl (mp->segments[i]);
64      clib_memcpy (seg, &this_address, sizeof (this_address));
65    }
66
67  int rv = 0;
68  rv = sr_mpls_policy_add (ntohl (mp->bsid),
69			   segments, mp->type, ntohl (mp->weight));
70  vec_free (segments);
71
72  REPLY_MACRO (VL_API_SR_MPLS_POLICY_ADD_REPLY);
73}
74
75static void
76vl_api_sr_mpls_policy_mod_t_handler (vl_api_sr_mpls_policy_mod_t * mp)
77{
78  vl_api_sr_mpls_policy_mod_reply_t *rmp;
79
80  mpls_label_t *segments = 0, *seg;
81  mpls_label_t this_address = 0;
82
83  int i;
84  for (i = 0; i < mp->n_segments; i++)
85    {
86      vec_add2 (segments, seg, 1);
87      this_address = ntohl (mp->segments[i]);
88      clib_memcpy (seg, &this_address, sizeof (this_address));
89    }
90
91  int rv = 0;
92  rv = sr_mpls_policy_mod (ntohl (mp->bsid),
93			   mp->operation, segments, ntohl (mp->sl_index),
94			   ntohl (mp->weight));
95  vec_free (segments);
96
97  REPLY_MACRO (VL_API_SR_MPLS_POLICY_MOD_REPLY);
98}
99
100static void
101vl_api_sr_mpls_policy_del_t_handler (vl_api_sr_mpls_policy_del_t * mp)
102{
103  vl_api_sr_mpls_policy_del_reply_t *rmp;
104  int rv = 0;
105  rv = sr_mpls_policy_del (ntohl (mp->bsid));
106
107  REPLY_MACRO (VL_API_SR_MPLS_POLICY_DEL_REPLY);
108}
109
110static void vl_api_sr_mpls_steering_add_del_t_handler
111  (vl_api_sr_mpls_steering_add_del_t * mp)
112{
113  vl_api_sr_mpls_steering_add_del_reply_t *rmp;
114  ip46_address_t prefix;
115  clib_memset (&prefix, 0, sizeof (ip46_address_t));
116  if (mp->traffic_type == SR_STEER_IPV4)
117    memcpy (&prefix.ip4, mp->prefix_addr, sizeof (prefix.ip4));
118  else
119    memcpy (&prefix, mp->prefix_addr, sizeof (prefix.ip6));
120
121  int rv = 0;
122  if (mp->is_del)
123    rv = sr_mpls_steering_policy_del (&prefix,
124				      ntohl (mp->mask_width),
125				      mp->traffic_type,
126				      ntohl (mp->table_id),
127				      ntohl (mp->color));
128  else
129    rv = sr_mpls_steering_policy_add (ntohl (mp->bsid),
130				      ntohl (mp->table_id),
131				      &prefix,
132				      ntohl (mp->mask_width),
133				      mp->traffic_type,
134				      (ip46_address_t *) & mp->next_hop,
135				      mp->nh_type,
136				      ntohl (mp->color), mp->co_bits,
137				      ntohl (mp->vpn_label));
138
139  REPLY_MACRO (VL_API_SR_MPLS_STEERING_ADD_DEL_REPLY);
140}
141
142static void vl_api_sr_mpls_policy_assign_endpoint_color_t_handler
143  (vl_api_sr_mpls_policy_assign_endpoint_color_t * mp)
144{
145  vl_api_sr_mpls_policy_assign_endpoint_color_reply_t *rmp;
146  int rv = 0;
147
148  ip46_address_t endpoint;
149  clib_memset (&endpoint, 0, sizeof (ip46_address_t));
150  if (mp->endpoint_type == SR_STEER_IPV4)
151    memcpy (&endpoint.ip4, mp->endpoint, sizeof (endpoint.ip4));
152  else
153    memcpy (&endpoint, mp->endpoint, sizeof (endpoint.ip6));
154
155  rv = sr_mpls_policy_assign_endpoint_color (ntohl (mp->bsid),
156					     &endpoint, mp->endpoint_type,
157					     ntohl (mp->color));
158
159  REPLY_MACRO (VL_API_SR_MPLS_POLICY_ASSIGN_ENDPOINT_COLOR_REPLY);
160}
161
162/*
163 * sr_mpls_api_hookup Add vpe's API message handlers to the table. vlib has
164 * already mapped shared memory and added the client registration handlers.
165 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
166 */
167#define vl_msg_name_crc_list
168#include <vnet/vnet_all_api_h.h>
169#undef vl_msg_name_crc_list
170
171static void
172setup_message_id_table (api_main_t * am)
173{
174#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
175  foreach_vl_msg_name_crc_sr_mpls;
176#undef _
177}
178
179static clib_error_t *
180sr_mpls_api_hookup (vlib_main_t * vm)
181{
182  api_main_t *am = vlibapi_get_main ();
183
184#define _(N,n)                                                  \
185    vl_msg_api_set_handlers(VL_API_##N, #n,                     \
186                           vl_api_##n##_t_handler,              \
187                           vl_noop_handler,                     \
188                           vl_api_##n##_t_endian,               \
189                           vl_api_##n##_t_print,                \
190                           sizeof(vl_api_##n##_t), 1);
191  foreach_vpe_api_msg;
192#undef _
193
194  /*
195   * Manually register the sr policy add msg, so we trace enough bytes
196   * to capture a typical segment list
197   */
198  vl_msg_api_set_handlers (VL_API_SR_MPLS_POLICY_ADD,
199			   "sr_mpls_policy_add",
200			   vl_api_sr_mpls_policy_add_t_handler,
201			   vl_noop_handler,
202			   vl_api_sr_mpls_policy_add_t_endian,
203			   vl_api_sr_mpls_policy_add_t_print, 256, 1);
204
205  /*
206   * Manually register the sr policy mod msg, so we trace enough bytes
207   * to capture a typical segment list
208   */
209  vl_msg_api_set_handlers (VL_API_SR_MPLS_POLICY_MOD,
210			   "sr_mpls_policy_mod",
211			   vl_api_sr_mpls_policy_mod_t_handler,
212			   vl_noop_handler,
213			   vl_api_sr_mpls_policy_mod_t_endian,
214			   vl_api_sr_mpls_policy_mod_t_print, 256, 1);
215
216  /*
217   * Set up the (msg_name, crc, message-id) table
218   */
219  setup_message_id_table (am);
220
221  return 0;
222}
223
224VLIB_API_INIT_FUNCTION (sr_mpls_api_hookup);
225
226/*
227 * fd.io coding-style-patch-verification: ON
228 *
229 * Local Variables: eval: (c-set-style "gnu") End:
230 */
231