FD.io VPP  v17.10-9-gd594711
Vector Packet Processing
flow_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * flow_api.c - flow 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>
21 #include <vlibmemory/api.h>
22 
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/flow/flow_report.h>
29 
30 #include <vnet/vnet_msg_enum.h>
31 
32 #define vl_typedefs /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
34 #undef vl_typedefs
35 
36 #define vl_endianfun /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
38 #undef vl_endianfun
39 
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #define vl_printfun
43 #include <vnet/vnet_all_api_h.h>
44 #undef vl_printfun
45 
47 
48 #define foreach_vpe_api_msg \
49 _(SET_IPFIX_EXPORTER, set_ipfix_exporter) \
50 _(IPFIX_EXPORTER_DUMP, ipfix_exporter_dump) \
51 _(SET_IPFIX_CLASSIFY_STREAM, set_ipfix_classify_stream) \
52 _(IPFIX_CLASSIFY_STREAM_DUMP, ipfix_classify_stream_dump) \
53 _(IPFIX_CLASSIFY_TABLE_ADD_DEL, ipfix_classify_table_add_del) \
54 _(IPFIX_CLASSIFY_TABLE_DUMP, ipfix_classify_table_dump)
55 
56 static void
58 {
61  vl_api_set_ipfix_exporter_reply_t *rmp;
62  ip4_address_t collector, src;
63  u16 collector_port = UDP_DST_PORT_ipfix;
64  u32 path_mtu;
65  u32 template_interval;
67  u32 fib_id;
68  u32 fib_index = ~0;
69  int rv = 0;
70 
71  memcpy (collector.data, mp->collector_address, sizeof (collector.data));
72  collector_port = ntohs (mp->collector_port);
73  if (collector_port == (u16) ~ 0)
74  collector_port = UDP_DST_PORT_ipfix;
75  memcpy (src.data, mp->src_address, sizeof (src.data));
76  fib_id = ntohl (mp->vrf_id);
77 
78  ip4_main_t *im = &ip4_main;
79  if (fib_id == ~0)
80  {
81  fib_index = ~0;
82  }
83  else
84  {
85  uword *p = hash_get (im->fib_index_by_table_id, fib_id);
86  if (!p)
87  {
88  rv = VNET_API_ERROR_NO_SUCH_FIB;
89  goto out;
90  }
91  fib_index = p[0];
92  }
93 
94  path_mtu = ntohl (mp->path_mtu);
95  if (path_mtu == ~0)
96  path_mtu = 512; // RFC 7011 section 10.3.3.
97  template_interval = ntohl (mp->template_interval);
98  if (template_interval == ~0)
99  template_interval = 20;
100  udp_checksum = mp->udp_checksum;
101 
102  if (collector.as_u32 == 0)
103  {
104  rv = VNET_API_ERROR_INVALID_VALUE;
105  goto out;
106  }
107 
108  if (src.as_u32 == 0)
109  {
110  rv = VNET_API_ERROR_INVALID_VALUE;
111  goto out;
112  }
113 
114  if (path_mtu > 1450 /* vpp does not support fragmentation */ )
115  {
116  rv = VNET_API_ERROR_INVALID_VALUE;
117  goto out;
118  }
119 
120  if (path_mtu < 68)
121  {
122  rv = VNET_API_ERROR_INVALID_VALUE;
123  goto out;
124  }
125 
126  /* Reset report streams if we are reconfiguring IP addresses */
127  if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
128  frm->src_address.as_u32 != src.as_u32 ||
129  frm->collector_port != collector_port)
131 
132  frm->ipfix_collector.as_u32 = collector.as_u32;
133  frm->collector_port = collector_port;
134  frm->src_address.as_u32 = src.as_u32;
135  frm->fib_index = fib_index;
136  frm->path_mtu = path_mtu;
137  frm->template_interval = template_interval;
138  frm->udp_checksum = udp_checksum;
139 
140  /* Turn on the flow reporting process */
142 
143 out:
144  REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
145 }
146 
147 static void
149 {
153  ip4_main_t *im = &ip4_main;
154  u32 vrf_id;
155 
157  if (!q)
158  return;
159 
160  rmp = vl_msg_api_alloc (sizeof (*rmp));
161  memset (rmp, 0, sizeof (*rmp));
162  rmp->_vl_msg_id = ntohs (VL_API_IPFIX_EXPORTER_DETAILS);
163  rmp->context = mp->context;
164  memcpy (rmp->collector_address, frm->ipfix_collector.data,
165  sizeof (frm->ipfix_collector.data));
166  rmp->collector_port = htons (frm->collector_port);
167  memcpy (rmp->src_address, frm->src_address.data,
168  sizeof (frm->src_address.data));
169  if (frm->fib_index == ~0)
170  vrf_id = ~0;
171  else
172  vrf_id = im->fibs[frm->fib_index].ft_table_id;
173  rmp->vrf_id = htonl (vrf_id);
174  rmp->path_mtu = htonl (frm->path_mtu);
175  rmp->template_interval = htonl (frm->template_interval);
176  rmp->udp_checksum = (frm->udp_checksum != 0);
177 
178  vl_msg_api_send_shmem (q, (u8 *) & rmp);
179 }
180 
181 static void
184 {
185  vl_api_set_ipfix_classify_stream_reply_t *rmp;
188  u32 domain_id = 0;
189  u32 src_port = UDP_DST_PORT_ipfix;
190  int rv = 0;
191 
192  domain_id = ntohl (mp->domain_id);
193  src_port = ntohs (mp->src_port);
194 
195  if (fcm->src_port != 0 &&
196  (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
197  {
198  int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
199  domain_id, (u16) src_port);
200  ASSERT (rv == 0);
201  }
202 
203  fcm->domain_id = domain_id;
204  fcm->src_port = (u16) src_port;
205 
206  REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
207 }
208 
209 static void
212 {
216 
218  if (!q)
219  return;
220 
221  rmp = vl_msg_api_alloc (sizeof (*rmp));
222  memset (rmp, 0, sizeof (*rmp));
223  rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
224  rmp->context = mp->context;
225  rmp->domain_id = htonl (fcm->domain_id);
226  rmp->src_port = htons (fcm->src_port);
227 
228  vl_msg_api_send_shmem (q, (u8 *) & rmp);
229 }
230 
231 static void
234 {
235  vl_api_ipfix_classify_table_add_del_reply_t *rmp;
239  ipfix_classify_table_t *table;
240  int is_add;
241  u32 classify_table_index;
242  u8 ip_version;
243  u8 transport_protocol;
244  int rv = 0;
245 
246  classify_table_index = ntohl (mp->table_id);
247  ip_version = mp->ip_version;
248  transport_protocol = mp->transport_protocol;
249  is_add = mp->is_add;
250 
251  if (fcm->src_port == 0)
252  {
253  /* call set_ipfix_classify_stream first */
254  rv = VNET_API_ERROR_UNSPECIFIED;
255  goto out;
256  }
257 
258  memset (&args, 0, sizeof (args));
259 
260  table = 0;
261  int i;
262  for (i = 0; i < vec_len (fcm->tables); i++)
264  if (fcm->tables[i].classify_table_index == classify_table_index)
265  {
266  table = &fcm->tables[i];
267  break;
268  }
269 
270  if (is_add)
271  {
272  if (table)
273  {
274  rv = VNET_API_ERROR_VALUE_EXIST;
275  goto out;
276  }
277  table = ipfix_classify_add_table ();
278  table->classify_table_index = classify_table_index;
279  }
280  else
281  {
282  if (!table)
283  {
284  rv = VNET_API_ERROR_NO_SUCH_ENTRY;
285  goto out;
286  }
287  }
288 
289  table->ip_version = ip_version;
290  table->transport_protocol = transport_protocol;
291 
292  args.opaque.as_uword = table - fcm->tables;
295  args.is_add = is_add;
296  args.domain_id = fcm->domain_id;
297  args.src_port = fcm->src_port;
298 
299  rv = vnet_flow_report_add_del (frm, &args, NULL);
300 
301  /* If deleting, or add failed */
302  if (is_add == 0 || (rv && is_add))
303  ipfix_classify_delete_table (table - fcm->tables);
304 
305 out:
306  REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
307 }
308 
309 static void
312  u32 context)
313 {
316 
317  ipfix_classify_table_t *table = &fcm->tables[table_index];
318 
319  mp = vl_msg_api_alloc (sizeof (*mp));
320  memset (mp, 0, sizeof (*mp));
321  mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
322  mp->context = context;
323  mp->table_id = htonl (table->classify_table_index);
324  mp->ip_version = table->ip_version;
326 
327  vl_msg_api_send_shmem (q, (u8 *) & mp);
328 }
329 
330 static void
333 {
336  u32 i;
337 
339  if (!q)
340  return;
341 
342  for (i = 0; i < vec_len (fcm->tables); i++)
345 }
346 
347 /*
348  * flow_api_hookup
349  * Add vpe's API message handlers to the table.
350  * vlib has alread mapped shared memory and
351  * added the client registration handlers.
352  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
353  */
354 #define vl_msg_name_crc_list
355 #include <vnet/vnet_all_api_h.h>
356 #undef vl_msg_name_crc_list
357 
358 static void
360 {
361 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
362  foreach_vl_msg_name_crc_flow;
363 #undef _
364 }
365 
366 static clib_error_t *
368 {
369  api_main_t *am = &api_main;
370 
371 #define _(N,n) \
372  vl_msg_api_set_handlers(VL_API_##N, #n, \
373  vl_api_##n##_t_handler, \
374  vl_noop_handler, \
375  vl_api_##n##_t_endian, \
376  vl_api_##n##_t_print, \
377  sizeof(vl_api_##n##_t), 1);
379 #undef _
380 
381  /*
382  * Set up the (msg_name, crc, message-id) table
383  */
385 
386  return 0;
387 }
388 
390 
391 /*
392  * fd.io coding-style-patch-verification: ON
393  *
394  * Local Variables:
395  * eval: (c-set-style "gnu")
396  * End:
397  */
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
IPFIX classify tables dump request.
Definition: flow.api:125
static clib_error_t * flow_api_hookup(vlib_main_t *vm)
Definition: flow_api.c:367
vlib_node_registration_t flow_report_process_node
(constructor) VLIB_REGISTER_NODE (flow_report_process_node)
Definition: flow_report.c:233
u16 udp_checksum(udp_header_t *uh, u32 udp_len, void *ih, u8 version)
Definition: packets.c:114
static_always_inline void ipfix_classify_delete_table(u32 index)
uword as_uword
Definition: flow_report.h:57
static_always_inline u8 ipfix_classify_table_index_valid(u32 index)
#define NULL
Definition: clib.h:55
Reply to IPFIX classify tables dump request.
Definition: flow.api:136
int vnet_flow_report_add_del(flow_report_main_t *frm, vnet_flow_report_add_del_args_t *a, u16 *template_id)
Definition: flow_report.c:239
IPFIX exporter dump request.
Definition: flow.api:44
ip4_address_t src_address
Definition: flow_report.h:96
ip4_address_t ipfix_collector
Definition: flow_report.h:94
static void vl_api_ipfix_classify_table_dump_t_handler(vl_api_ipfix_classify_table_dump_t *mp)
Definition: flow_api.c:332
vnet_flow_rewrite_callback_t * rewrite_callback
Definition: flow_report.h:125
Reply to IPFIX exporter dump request.
Definition: flow.api:60
void * vl_msg_api_alloc(int nbytes)
flow_report_main_t flow_report_main
Definition: flow_report.c:21
#define hash_get(h, key)
Definition: hash.h:248
flow_report_classify_main_t flow_report_classify_main
uword * fib_index_by_table_id
Hash table mapping table id to fib index.
Definition: ip4.h:121
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:950
#define REPLY_MACRO(t)
static void setup_message_id_table(api_main_t *am)
Definition: flow_api.c:359
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:182
IPFIX classify stream configure request.
Definition: flow.api:78
static void vl_api_ipfix_classify_table_add_del_t_handler(vl_api_ipfix_classify_table_add_del_t *mp)
Definition: flow_api.c:233
ipfix_classify_table_t * tables
static void vl_api_set_ipfix_exporter_t_handler(vl_api_set_ipfix_exporter_t *mp)
Definition: flow_api.c:57
api_main_t api_main
Definition: api_shared.c:35
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:50
vlib_main_t * vm
Definition: buffer.c:283
static void vl_api_set_ipfix_classify_stream_t_handler(vl_api_set_ipfix_classify_stream_t *mp)
Definition: flow_api.c:183
unix_shared_memory_queue_t * vl_api_client_index_to_input_queue(u32 index)
static void send_ipfix_classify_table_details(u32 table_index, unix_shared_memory_queue_t *q, u32 context)
Definition: flow_api.c:310
static_always_inline ipfix_classify_table_t * ipfix_classify_add_table(void)
static void vl_api_ipfix_exporter_dump_t_handler(vl_api_ipfix_exporter_dump_t *mp)
Definition: flow_api.c:148
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
IPv4 main type.
Definition: ip4.h:95
static void vl_api_ipfix_classify_stream_dump_t_handler(vl_api_ipfix_classify_stream_dump_t *mp)
Definition: flow_api.c:211
void vnet_flow_reports_reset(flow_report_main_t *frm)
Definition: flow_report.c:336
VLIB_API_INIT_FUNCTION(flow_api_hookup)
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Reply to IPFIX classify stream dump request.
Definition: flow.api:99
unsigned char u8
Definition: types.h:56
int vnet_stream_change(flow_report_main_t *frm, u32 old_domain_id, u16 old_src_port, u32 new_domain_id, u16 new_src_port)
Definition: flow_report.c:365
Configure IPFIX exporter process request.
Definition: flow.api:27
u8 * ipfix_classify_template_rewrite(flow_report_main_t *frm, flow_report_t *fr, ip4_address_t *collector_address, ip4_address_t *src_address, u16 collector_port)
vnet_flow_data_callback_t * flow_data_callback
Definition: flow_report.h:124
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1175
struct fib_table_t_ * fibs
Vector of FIBs.
Definition: ip4.h:100
IPFIX classify stream dump request.
Definition: flow.api:89
#define foreach_vpe_api_msg
Definition: flow_api.c:48
vlib_frame_t * ipfix_classify_send_flows(flow_report_main_t *frm, flow_report_t *fr, vlib_frame_t *f, u32 *to_next, u32 node_index)
IPFIX add or delete classifier table request.
Definition: flow.api:112
struct _unix_shared_memory_queue unix_shared_memory_queue_t