FD.io VPP  v19.04.4-rc0-5-ge88582fac
Vector Packet Processing
stn_api.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <vlibapi/api.h>
17 #include <vlibmemory/api.h>
18 
19 #include <plugins/stn/stn.h>
20 #include <vnet/ip/format.h>
21 
22 #include <vppinfra/byte_order.h>
23 
24 /* define message IDs */
25 #include <stn/stn_msg_enum.h>
26 
27 /* define message structures */
28 #define vl_typedefs
29 #include <stn/stn_all_api_h.h>
30 #undef vl_typedefs
31 
32 /* define generated endian-swappers */
33 #define vl_endianfun
34 #include <stn/stn_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 <stn/stn_all_api_h.h>
41 #undef vl_printfun
42 
43 /* Get the API version number */
44 #define vl_api_version(n,v) static u32 api_version=(v);
45 #include <stn/stn_all_api_h.h>
46 #undef vl_api_version
47 
48 #define REPLY_MSG_ID_BASE stn_main.msg_id_base
50 
51 /* Macro to finish up custom dump fns */
52 #define FINISH \
53  vec_add1 (s, 0); \
54  vl_print (handle, (char *)s); \
55  vec_free (s); \
56  return handle;
57 
58 /**
59  * @brief API message custom-dump function
60  * @param mp vl_api_stn_add_del_rule_t * mp the api message
61  * @param handle void * print function handle
62  * @returns u8 * output string
63  */
65  (vl_api_stn_add_del_rule_t * mp, void *handle)
66 {
67  u8 *s;
68 
69  s = format (0, "SCRIPT: stn_add_del_rule ");
70  if (mp->is_ip4)
71  s = format (s, "address %U ", format_ip4_address, mp->ip_address);
72  else
73  s = format (s, "address %U ", format_ip6_address, mp->ip_address);
74  s = format (s, "sw_if_index %d is_add %d", mp->sw_if_index, mp->is_add);
75 
76  FINISH;
77 }
78 
79 static void
81 {
83  vl_api_stn_add_del_rule_reply_t *rmp;
84  int rv = 0;
85 
86  if (mp->is_ip4)
87  {
89  memcpy (&a, mp->ip_address, sizeof (a));
90  ip46_address_set_ip4 (&args.address, &a);
91  }
92  else
93  memcpy (&args.address.ip6, mp->ip_address, sizeof (ip6_address_t));
94 
95  args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
96  args.del = !mp->is_add;
97 
98  rv = stn_rule_add_del (&args);
99 
100  REPLY_MACRO (VL_API_STN_ADD_DEL_RULE_REPLY);
101 }
102 
103 static void
105  u32 context)
106 {
108 
109  rmp = vl_msg_api_alloc (sizeof (*rmp));
110  clib_memset (rmp, 0, sizeof (*rmp));
111  rmp->_vl_msg_id =
112  clib_host_to_net_u16 (VL_API_STN_RULES_DETAILS + stn_main.msg_id_base);
113 
114  if (ip46_address_is_ip4 (&r->address))
115  {
116  clib_memcpy (rmp->ip_address, &r->address.ip4, sizeof (ip4_address_t));
117  rmp->is_ip4 = 1;
118  }
119  else
120  {
121  clib_memcpy (rmp->ip_address, &r->address.ip6, sizeof (ip6_address_t));
122  }
123 
124  rmp->context = context;
125  rmp->sw_if_index = clib_host_to_net_u32 (r->sw_if_index);
126 
127  vl_api_send_msg (reg, (u8 *) rmp);
128 }
129 
130 static void
132 {
134  stn_main_t *stn = &stn_main;
135  stn_rule_t *r;
136 
138  if (reg == 0)
139  return;
140 
141  /* *INDENT-OFF* */
142  pool_foreach (r, stn->rules,({
143  send_stn_rules_details (r, reg, mp->context);
144  }));
145  /* *INDENT-ON* */
146 }
147 
148 
149 /* List of message types that this plugin understands */
150 #define foreach_stn_plugin_api_msg \
151 _(STN_ADD_DEL_RULE, stn_add_del_rule) \
152 _(STN_RULES_DUMP, stn_rules_dump)
153 
154 /**
155  * @brief Set up the API message handling tables
156  * @param vm vlib_main_t * vlib main data structure pointer
157  * @returns 0 to indicate all is well
158  */
159 static clib_error_t *
161 {
162  stn_main_t *stn = &stn_main;
163 #define _(N,n) \
164  vl_msg_api_set_handlers((VL_API_##N + stn->msg_id_base), \
165  #n, \
166  vl_api_##n##_t_handler, \
167  vl_noop_handler, \
168  vl_api_##n##_t_endian, \
169  vl_api_##n##_t_print, \
170  sizeof(vl_api_##n##_t), 1);
172 #undef _
173 
174  return 0;
175 }
176 
177 #define vl_msg_name_crc_list
178 #include <stn/stn.api.h>
179 #undef vl_msg_name_crc_list
180 
181 static void
183 {
184 #define _(id,n,crc) \
185  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + stn->msg_id_base);
186  foreach_vl_msg_name_crc_stn;
187 #undef _
188 }
189 
190 static void
192 {
193 #define _(n,f) api_main.msg_print_handlers \
194  [VL_API_##n + stn->msg_id_base] \
195  = (void *) vl_api_##f##_t_print;
197 #undef _
198 }
199 
200 clib_error_t *
202 {
203  u8 *name;
204  clib_error_t *error = 0;
205 
206  name = format (0, "stn_%08x%c", api_version, 0);
207 
208  /* Ask for a correctly-sized block of API message decode slots */
209  sm->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
211 
212  error = stn_plugin_api_hookup (vm);
213 
214  /* Add our API messages to the global name_crc hash table */
216 
218 
219  vec_free (name);
220 
221  return error;
222 }
223 
224 /*
225  * fd.io coding-style-patch-verification: ON
226  *
227  * Local Variables:
228  * eval: (c-set-style "gnu")
229  * End:
230  */
a
Definition: bitmap.h:538
static void setup_message_id_table(stn_main_t *stn, api_main_t *am)
Definition: stn_api.c:182
Definition: stn.h:24
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:34
Definition: stn.h:30
u32 sw_if_index
TX interface to send packets to.
Definition: stn.h:50
int stn_rule_add_del(stn_rule_add_del_args_t *args)
Add or delete an stn rule.
Definition: stn.c:286
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
stn_main_t stn_main
Definition: stn.c:25
void * vl_msg_api_alloc(int nbytes)
Dump STN rules.
Definition: stn.api:45
unsigned char u8
Definition: types.h:56
#define clib_memcpy(d, s, n)
Definition: string.h:180
ip46_address_t address
Destination address of intercepted packets.
Definition: stn.h:48
Add/del STN rules.
Definition: stn.api:32
stn_rule_t * rules
Definition: stn.h:32
format_function_t format_ip4_address
Definition: format.h:75
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
u8 del
Whether to delete the rule.
Definition: stn.h:52
unsigned int u32
Definition: types.h:88
static void vl_api_stn_add_del_rule_t_handler(vl_api_stn_add_del_rule_t *mp)
Definition: stn_api.c:80
STN response to rules request.
Definition: stn.api:56
static void vl_api_stn_rules_dump_t_handler(vl_api_stn_rules_dump_t *mp)
Definition: stn_api.c:131
#define REPLY_MACRO(t)
static void plugin_custom_dump_configure(stn_main_t *stn)
Definition: stn_api.c:191
u8 name[64]
Definition: memclnt.api:152
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:202
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:88
An API client registration, only in vpp/vlib.
Definition: api_common.h:45
format_function_t format_ip6_address
Definition: format.h:93
vlib_main_t * vm
Definition: buffer.c:312
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
u32 context
Definition: ipsec_gre.api:33
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:56
static void * vl_api_stn_add_del_rule_t_print(vl_api_stn_add_del_rule_t *mp, void *handle)
API message custom-dump function.
Definition: stn_api.c:65
#define ip46_address_set_ip4(ip46, ip)
Definition: ip6_packet.h:90
static clib_error_t * stn_plugin_api_hookup(vlib_main_t *vm)
Set up the API message handling tables.
Definition: stn_api.c:160
u16 msg_id_base
Definition: stn.h:43
u32 sw_if_index
Definition: stn.h:27
clib_error_t * stn_api_init(vlib_main_t *vm, stn_main_t *sm)
Definition: stn_api.c:201
#define FINISH
Definition: stn_api.c:52
ip46_address_t address
Definition: stn.h:25
#define foreach_stn_plugin_api_msg
Definition: stn_api.c:150
static void send_stn_rules_details(stn_rule_t *r, vl_api_registration_t *reg, u32 context)
Definition: stn_api.c:104
api_main_t api_main
Definition: api_shared.c:35
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:880