d314ddeaafdd91eaa13eacf404a69dc34b1ed8d5
[vpp.git] / src / vnet / classify / classify_api.c
1 /*
2  *------------------------------------------------------------------
3  * classify_api.c - classify 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/classify/vnet_classify.h>
27 #include <vnet/classify/input_acl.h>
28 #include <vnet/classify/policer_classify.h>
29 #include <vnet/classify/flow_classify.h>
30 #include <vnet/l2/l2_classify.h>
31
32 #include <vnet/vnet_msg_enum.h>
33
34 #define vl_typedefs             /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_typedefs
37
38 #define vl_endianfun            /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <vnet/vnet_all_api_h.h>
46 #undef vl_printfun
47
48 #include <vlibapi/api_helper_macros.h>
49
50 #define foreach_vpe_api_msg                                             \
51 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table)                       \
52 _(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session)                   \
53 _(CLASSIFY_TABLE_IDS,classify_table_ids)                                \
54 _(CLASSIFY_TABLE_BY_INTERFACE, classify_table_by_interface)             \
55 _(CLASSIFY_TABLE_INFO,classify_table_info)                              \
56 _(CLASSIFY_SESSION_DUMP,classify_session_dump)                          \
57 _(POLICER_CLASSIFY_SET_INTERFACE, policer_classify_set_interface)       \
58 _(POLICER_CLASSIFY_DUMP, policer_classify_dump)                         \
59 _(FLOW_CLASSIFY_SET_INTERFACE, flow_classify_set_interface)             \
60 _(FLOW_CLASSIFY_DUMP, flow_classify_dump)                               \
61 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface)                     \
62 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table)     \
63 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)
64
65 #define foreach_classify_add_del_table_field    \
66 _(table_index)                                  \
67 _(nbuckets)                                     \
68 _(memory_size)                                  \
69 _(skip_n_vectors)                               \
70 _(match_n_vectors)                              \
71 _(next_table_index)                             \
72 _(miss_next_index)                              \
73 _(current_data_flag)                            \
74 _(current_data_offset)
75
76 static void vl_api_classify_add_del_table_t_handler
77   (vl_api_classify_add_del_table_t * mp)
78 {
79   vl_api_classify_add_del_table_reply_t *rmp;
80   vnet_classify_main_t *cm = &vnet_classify_main;
81   vnet_classify_table_t *t;
82   int rv;
83
84 #define _(a) u32 a;
85   foreach_classify_add_del_table_field;
86 #undef _
87
88 #define _(a) a = ntohl(mp->a);
89   foreach_classify_add_del_table_field;
90 #undef _
91
92   /* The underlying API fails silently, on purpose, so check here */
93   if (mp->is_add == 0)          /* delete */
94     {
95       if (pool_is_free_index (cm->tables, table_index))
96         {
97           rv = VNET_API_ERROR_NO_SUCH_TABLE;
98           goto out;
99         }
100     }
101   else                          /* add or update */
102     {
103       if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
104         table_index = ~0;
105     }
106
107   rv = vnet_classify_add_del_table
108     (cm, mp->mask, nbuckets, memory_size,
109      skip_n_vectors, match_n_vectors,
110      next_table_index, miss_next_index, &table_index,
111      current_data_flag, current_data_offset, mp->is_add, mp->del_chain);
112
113 out:
114   /* *INDENT-OFF* */
115   REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
116   ({
117     if (rv == 0 && mp->is_add)
118       {
119         t = pool_elt_at_index (cm->tables, table_index);
120         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
121         rmp->match_n_vectors = ntohl(t->match_n_vectors);
122         rmp->new_table_index = ntohl(table_index);
123       }
124     else
125       {
126         rmp->skip_n_vectors = ~0;
127         rmp->match_n_vectors = ~0;
128         rmp->new_table_index = ~0;
129       }
130   }));
131   /* *INDENT-ON* */
132 }
133
134 static void vl_api_classify_add_del_session_t_handler
135   (vl_api_classify_add_del_session_t * mp)
136 {
137   vnet_classify_main_t *cm = &vnet_classify_main;
138   vl_api_classify_add_del_session_reply_t *rmp;
139   int rv;
140   u32 table_index, hit_next_index, opaque_index, metadata;
141   i32 advance;
142   u8 action;
143
144   table_index = ntohl (mp->table_index);
145   hit_next_index = ntohl (mp->hit_next_index);
146   opaque_index = ntohl (mp->opaque_index);
147   advance = ntohl (mp->advance);
148   action = mp->action;
149   metadata = ntohl (mp->metadata);
150
151   rv = vnet_classify_add_del_session
152     (cm, table_index, mp->match, hit_next_index, opaque_index,
153      advance, action, metadata, mp->is_add);
154
155   REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
156 }
157
158 static void
159   vl_api_policer_classify_set_interface_t_handler
160   (vl_api_policer_classify_set_interface_t * mp)
161 {
162   vlib_main_t *vm = vlib_get_main ();
163   vl_api_policer_classify_set_interface_reply_t *rmp;
164   int rv;
165   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
166
167   ip4_table_index = ntohl (mp->ip4_table_index);
168   ip6_table_index = ntohl (mp->ip6_table_index);
169   l2_table_index = ntohl (mp->l2_table_index);
170   sw_if_index = ntohl (mp->sw_if_index);
171
172   VALIDATE_SW_IF_INDEX (mp);
173
174   rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
175                                         ip6_table_index, l2_table_index,
176                                         mp->is_add);
177
178   BAD_SW_IF_INDEX_LABEL;
179
180   REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
181 }
182
183 static void
184 send_policer_classify_details (u32 sw_if_index,
185                                u32 table_index,
186                                unix_shared_memory_queue_t * q, u32 context)
187 {
188   vl_api_policer_classify_details_t *mp;
189
190   mp = vl_msg_api_alloc (sizeof (*mp));
191   memset (mp, 0, sizeof (*mp));
192   mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
193   mp->context = context;
194   mp->sw_if_index = htonl (sw_if_index);
195   mp->table_index = htonl (table_index);
196
197   vl_msg_api_send_shmem (q, (u8 *) & mp);
198 }
199
200 static void
201 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
202 {
203   unix_shared_memory_queue_t *q;
204   policer_classify_main_t *pcm = &policer_classify_main;
205   u32 *vec_tbl;
206   int i;
207
208   q = vl_api_client_index_to_input_queue (mp->client_index);
209   if (q == 0)
210     return;
211
212   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
213
214   if (vec_len (vec_tbl))
215     {
216       for (i = 0; i < vec_len (vec_tbl); i++)
217         {
218           if (vec_elt (vec_tbl, i) == ~0)
219             continue;
220
221           send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
222                                          mp->context);
223         }
224     }
225 }
226
227 static void
228 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
229 {
230   unix_shared_memory_queue_t *q;
231
232   q = vl_api_client_index_to_input_queue (mp->client_index);
233   if (q == 0)
234     return;
235
236   vnet_classify_main_t *cm = &vnet_classify_main;
237   vnet_classify_table_t *t;
238   u32 *table_ids = 0;
239   u32 count;
240
241    /* *INDENT-OFF* */
242    pool_foreach (t, cm->tables,
243    ({
244      vec_add1 (table_ids, ntohl(t - cm->tables));
245    }));
246    /* *INDENT-ON* */
247   count = vec_len (table_ids);
248
249   vl_api_classify_table_ids_reply_t *rmp;
250   rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
251   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
252   rmp->context = mp->context;
253   rmp->count = ntohl (count);
254   clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
255   rmp->retval = 0;
256
257   vl_msg_api_send_shmem (q, (u8 *) & rmp);
258
259   vec_free (table_ids);
260 }
261
262 static void
263   vl_api_classify_table_by_interface_t_handler
264   (vl_api_classify_table_by_interface_t * mp)
265 {
266   vl_api_classify_table_by_interface_reply_t *rmp;
267   int rv = 0;
268
269   u32 sw_if_index = ntohl (mp->sw_if_index);
270   u32 *acl = 0;
271
272   vec_validate (acl, INPUT_ACL_N_TABLES - 1);
273   vec_set (acl, ~0);
274
275   VALIDATE_SW_IF_INDEX (mp);
276
277   input_acl_main_t *am = &input_acl_main;
278
279   int if_idx;
280   u32 type;
281
282   for (type = 0; type < INPUT_ACL_N_TABLES; type++)
283     {
284       u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
285       if (vec_len (vec_tbl))
286         {
287           for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
288             {
289               if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
290                 {
291                   continue;
292                 }
293               acl[type] = vec_elt (vec_tbl, if_idx);
294             }
295         }
296     }
297
298   BAD_SW_IF_INDEX_LABEL;
299
300    /* *INDENT-OFF* */
301    REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
302    ({
303      rmp->sw_if_index = ntohl(sw_if_index);
304      rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
305      rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
306      rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
307    }));
308    /* *INDENT-ON* */
309   vec_free (acl);
310 }
311
312 static void
313 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
314 {
315   unix_shared_memory_queue_t *q;
316
317   q = vl_api_client_index_to_input_queue (mp->client_index);
318   if (q == 0)
319     return;
320
321   vl_api_classify_table_info_reply_t *rmp = 0;
322
323   vnet_classify_main_t *cm = &vnet_classify_main;
324   u32 table_id = ntohl (mp->table_id);
325   vnet_classify_table_t *t;
326
327    /* *INDENT-OFF* */
328    pool_foreach (t, cm->tables,
329    ({
330      if (table_id == t - cm->tables)
331        {
332          rmp = vl_msg_api_alloc_as_if_client
333            (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
334          rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
335          rmp->context = mp->context;
336          rmp->table_id = ntohl(table_id);
337          rmp->nbuckets = ntohl(t->nbuckets);
338          rmp->match_n_vectors = ntohl(t->match_n_vectors);
339          rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
340          rmp->active_sessions = ntohl(t->active_elements);
341          rmp->next_table_index = ntohl(t->next_table_index);
342          rmp->miss_next_index = ntohl(t->miss_next_index);
343          rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
344          clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
345          rmp->retval = 0;
346          break;
347        }
348    }));
349    /* *INDENT-ON* */
350
351   if (rmp == 0)
352     {
353       rmp = vl_msg_api_alloc (sizeof (*rmp));
354       rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
355       rmp->context = mp->context;
356       rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
357     }
358
359   vl_msg_api_send_shmem (q, (u8 *) & rmp);
360 }
361
362 static void
363 send_classify_session_details (unix_shared_memory_queue_t * q,
364                                u32 table_id,
365                                u32 match_length,
366                                vnet_classify_entry_t * e, u32 context)
367 {
368   vl_api_classify_session_details_t *rmp;
369
370   rmp = vl_msg_api_alloc (sizeof (*rmp));
371   memset (rmp, 0, sizeof (*rmp));
372   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
373   rmp->context = context;
374   rmp->table_id = ntohl (table_id);
375   rmp->hit_next_index = ntohl (e->next_index);
376   rmp->advance = ntohl (e->advance);
377   rmp->opaque_index = ntohl (e->opaque_index);
378   rmp->match_length = ntohl (match_length);
379   clib_memcpy (rmp->match, e->key, match_length);
380
381   vl_msg_api_send_shmem (q, (u8 *) & rmp);
382 }
383
384 static void
385 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
386 {
387   vnet_classify_main_t *cm = &vnet_classify_main;
388   unix_shared_memory_queue_t *q;
389
390   u32 table_id = ntohl (mp->table_id);
391   vnet_classify_table_t *t;
392
393   q = vl_api_client_index_to_input_queue (mp->client_index);
394   if (!q)
395     return;
396
397   /* *INDENT-OFF* */
398   pool_foreach (t, cm->tables,
399   ({
400     if (table_id == t - cm->tables)
401       {
402         vnet_classify_bucket_t * b;
403         vnet_classify_entry_t * v, * save_v;
404         int i, j, k;
405
406         for (i = 0; i < t->nbuckets; i++)
407           {
408             b = &t->buckets [i];
409             if (b->offset == 0)
410               continue;
411
412             save_v = vnet_classify_get_entry (t, b->offset);
413             for (j = 0; j < (1<<b->log2_pages); j++)
414               {
415                 for (k = 0; k < t->entries_per_page; k++)
416                   {
417                     v = vnet_classify_entry_at_index
418                       (t, save_v, j*t->entries_per_page + k);
419                     if (vnet_classify_entry_is_free (v))
420                       continue;
421
422                     send_classify_session_details
423                       (q, table_id, t->match_n_vectors * sizeof (u32x4),
424                        v, mp->context);
425                   }
426               }
427           }
428         break;
429       }
430   }));
431   /* *INDENT-ON* */
432 }
433
434 static void
435   vl_api_flow_classify_set_interface_t_handler
436   (vl_api_flow_classify_set_interface_t * mp)
437 {
438   vlib_main_t *vm = vlib_get_main ();
439   vl_api_flow_classify_set_interface_reply_t *rmp;
440   int rv;
441   u32 sw_if_index, ip4_table_index, ip6_table_index;
442
443   ip4_table_index = ntohl (mp->ip4_table_index);
444   ip6_table_index = ntohl (mp->ip6_table_index);
445   sw_if_index = ntohl (mp->sw_if_index);
446
447   VALIDATE_SW_IF_INDEX (mp);
448
449   rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
450                                      ip6_table_index, mp->is_add);
451
452   BAD_SW_IF_INDEX_LABEL;
453
454   REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
455 }
456
457 static void
458 send_flow_classify_details (u32 sw_if_index,
459                             u32 table_index,
460                             unix_shared_memory_queue_t * q, u32 context)
461 {
462   vl_api_flow_classify_details_t *mp;
463
464   mp = vl_msg_api_alloc (sizeof (*mp));
465   memset (mp, 0, sizeof (*mp));
466   mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
467   mp->context = context;
468   mp->sw_if_index = htonl (sw_if_index);
469   mp->table_index = htonl (table_index);
470
471   vl_msg_api_send_shmem (q, (u8 *) & mp);
472 }
473
474 static void
475 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
476 {
477   unix_shared_memory_queue_t *q;
478   flow_classify_main_t *pcm = &flow_classify_main;
479   u32 *vec_tbl;
480   int i;
481
482   q = vl_api_client_index_to_input_queue (mp->client_index);
483   if (q == 0)
484     return;
485
486   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
487
488   if (vec_len (vec_tbl))
489     {
490       for (i = 0; i < vec_len (vec_tbl); i++)
491         {
492           if (vec_elt (vec_tbl, i) == ~0)
493             continue;
494
495           send_flow_classify_details (i, vec_elt (vec_tbl, i), q,
496                                       mp->context);
497         }
498     }
499 }
500
501 static void vl_api_classify_set_interface_ip_table_t_handler
502   (vl_api_classify_set_interface_ip_table_t * mp)
503 {
504   vlib_main_t *vm = vlib_get_main ();
505   vl_api_classify_set_interface_ip_table_reply_t *rmp;
506   int rv;
507
508   VALIDATE_SW_IF_INDEX (mp);
509
510   u32 table_index = ntohl (mp->table_index);
511   u32 sw_if_index = ntohl (mp->sw_if_index);
512
513   if (mp->is_ipv6)
514     rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
515   else
516     rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
517
518   BAD_SW_IF_INDEX_LABEL;
519
520   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
521 }
522
523 static void vl_api_classify_set_interface_l2_tables_t_handler
524   (vl_api_classify_set_interface_l2_tables_t * mp)
525 {
526   vl_api_classify_set_interface_l2_tables_reply_t *rmp;
527   int rv;
528   u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
529   int enable;
530
531   ip4_table_index = ntohl (mp->ip4_table_index);
532   ip6_table_index = ntohl (mp->ip6_table_index);
533   other_table_index = ntohl (mp->other_table_index);
534   sw_if_index = ntohl (mp->sw_if_index);
535
536   VALIDATE_SW_IF_INDEX (mp);
537
538   if (mp->is_input)
539     rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
540                                             ip6_table_index,
541                                             other_table_index);
542   else
543     rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
544                                              ip6_table_index,
545                                              other_table_index);
546
547   if (rv == 0)
548     {
549       if (ip4_table_index != ~0 || ip6_table_index != ~0
550           || other_table_index != ~0)
551         enable = 1;
552       else
553         enable = 0;
554
555       if (mp->is_input)
556         vnet_l2_input_classify_enable_disable (sw_if_index, enable);
557       else
558         vnet_l2_output_classify_enable_disable (sw_if_index, enable);
559     }
560
561   BAD_SW_IF_INDEX_LABEL;
562
563   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
564 }
565
566 static void vl_api_input_acl_set_interface_t_handler
567   (vl_api_input_acl_set_interface_t * mp)
568 {
569   vlib_main_t *vm = vlib_get_main ();
570   vl_api_input_acl_set_interface_reply_t *rmp;
571   int rv;
572
573   VALIDATE_SW_IF_INDEX (mp);
574
575   u32 ip4_table_index = ntohl (mp->ip4_table_index);
576   u32 ip6_table_index = ntohl (mp->ip6_table_index);
577   u32 l2_table_index = ntohl (mp->l2_table_index);
578   u32 sw_if_index = ntohl (mp->sw_if_index);
579
580   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
581                                  ip6_table_index, l2_table_index, mp->is_add);
582
583   BAD_SW_IF_INDEX_LABEL;
584
585   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
586 }
587
588 /*
589  * classify_api_hookup
590  * Add vpe's API message handlers to the table.
591  * vlib has alread mapped shared memory and
592  * added the client registration handlers.
593  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
594  */
595 #define vl_msg_name_crc_list
596 #include <vnet/vnet_all_api_h.h>
597 #undef vl_msg_name_crc_list
598
599 static void
600 setup_message_id_table (api_main_t * am)
601 {
602 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
603   foreach_vl_msg_name_crc_classify;
604 #undef _
605 }
606
607 static clib_error_t *
608 classify_api_hookup (vlib_main_t * vm)
609 {
610   api_main_t *am = &api_main;
611
612 #define _(N,n)                                                  \
613     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
614                            vl_api_##n##_t_handler,              \
615                            vl_noop_handler,                     \
616                            vl_api_##n##_t_endian,               \
617                            vl_api_##n##_t_print,                \
618                            sizeof(vl_api_##n##_t), 1);
619   foreach_vpe_api_msg;
620 #undef _
621
622   /*
623    * Set up the (msg_name, crc, message-id) table
624    */
625   setup_message_id_table (am);
626
627   return 0;
628 }
629
630 VLIB_API_INIT_FUNCTION (classify_api_hookup);
631
632 /*
633  * fd.io coding-style-patch-verification: ON
634  *
635  * Local Variables:
636  * eval: (c-set-style "gnu")
637  * End:
638  */