api: refactor vlibmemory
[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, svm_queue_t * q, u32 context)
186 {
187   vl_api_policer_classify_details_t *mp;
188
189   mp = vl_msg_api_alloc (sizeof (*mp));
190   memset (mp, 0, sizeof (*mp));
191   mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
192   mp->context = context;
193   mp->sw_if_index = htonl (sw_if_index);
194   mp->table_index = htonl (table_index);
195
196   vl_msg_api_send_shmem (q, (u8 *) & mp);
197 }
198
199 static void
200 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
201 {
202   svm_queue_t *q;
203   policer_classify_main_t *pcm = &policer_classify_main;
204   u32 *vec_tbl;
205   int i;
206
207   q = vl_api_client_index_to_input_queue (mp->client_index);
208   if (q == 0)
209     return;
210
211   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
212
213   if (vec_len (vec_tbl))
214     {
215       for (i = 0; i < vec_len (vec_tbl); i++)
216         {
217           if (vec_elt (vec_tbl, i) == ~0)
218             continue;
219
220           send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
221                                          mp->context);
222         }
223     }
224 }
225
226 static void
227 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
228 {
229   svm_queue_t *q;
230
231   q = vl_api_client_index_to_input_queue (mp->client_index);
232   if (q == 0)
233     return;
234
235   vnet_classify_main_t *cm = &vnet_classify_main;
236   vnet_classify_table_t *t;
237   u32 *table_ids = 0;
238   u32 count;
239
240    /* *INDENT-OFF* */
241    pool_foreach (t, cm->tables,
242    ({
243      vec_add1 (table_ids, ntohl(t - cm->tables));
244    }));
245    /* *INDENT-ON* */
246   count = vec_len (table_ids);
247
248   vl_api_classify_table_ids_reply_t *rmp;
249   rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
250   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
251   rmp->context = mp->context;
252   rmp->count = ntohl (count);
253   clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
254   rmp->retval = 0;
255
256   vl_msg_api_send_shmem (q, (u8 *) & rmp);
257
258   vec_free (table_ids);
259 }
260
261 static void
262   vl_api_classify_table_by_interface_t_handler
263   (vl_api_classify_table_by_interface_t * mp)
264 {
265   vl_api_classify_table_by_interface_reply_t *rmp;
266   int rv = 0;
267
268   u32 sw_if_index = ntohl (mp->sw_if_index);
269   u32 *acl = 0;
270
271   vec_validate (acl, INPUT_ACL_N_TABLES - 1);
272   vec_set (acl, ~0);
273
274   VALIDATE_SW_IF_INDEX (mp);
275
276   input_acl_main_t *am = &input_acl_main;
277
278   int if_idx;
279   u32 type;
280
281   for (type = 0; type < INPUT_ACL_N_TABLES; type++)
282     {
283       u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
284       if (vec_len (vec_tbl))
285         {
286           for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
287             {
288               if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
289                 {
290                   continue;
291                 }
292               acl[type] = vec_elt (vec_tbl, if_idx);
293             }
294         }
295     }
296
297   BAD_SW_IF_INDEX_LABEL;
298
299    /* *INDENT-OFF* */
300    REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
301    ({
302      rmp->sw_if_index = ntohl(sw_if_index);
303      rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
304      rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
305      rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
306    }));
307    /* *INDENT-ON* */
308   vec_free (acl);
309 }
310
311 static void
312 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
313 {
314   svm_queue_t *q;
315
316   q = vl_api_client_index_to_input_queue (mp->client_index);
317   if (q == 0)
318     return;
319
320   vl_api_classify_table_info_reply_t *rmp = 0;
321
322   vnet_classify_main_t *cm = &vnet_classify_main;
323   u32 table_id = ntohl (mp->table_id);
324   vnet_classify_table_t *t;
325
326    /* *INDENT-OFF* */
327    pool_foreach (t, cm->tables,
328    ({
329      if (table_id == t - cm->tables)
330        {
331          rmp = vl_msg_api_alloc_as_if_client
332            (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
333          rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
334          rmp->context = mp->context;
335          rmp->table_id = ntohl(table_id);
336          rmp->nbuckets = ntohl(t->nbuckets);
337          rmp->match_n_vectors = ntohl(t->match_n_vectors);
338          rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
339          rmp->active_sessions = ntohl(t->active_elements);
340          rmp->next_table_index = ntohl(t->next_table_index);
341          rmp->miss_next_index = ntohl(t->miss_next_index);
342          rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
343          clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
344          rmp->retval = 0;
345          break;
346        }
347    }));
348    /* *INDENT-ON* */
349
350   if (rmp == 0)
351     {
352       rmp = vl_msg_api_alloc (sizeof (*rmp));
353       rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
354       rmp->context = mp->context;
355       rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
356     }
357
358   vl_msg_api_send_shmem (q, (u8 *) & rmp);
359 }
360
361 static void
362 send_classify_session_details (svm_queue_t * q,
363                                u32 table_id,
364                                u32 match_length,
365                                vnet_classify_entry_t * e, u32 context)
366 {
367   vl_api_classify_session_details_t *rmp;
368
369   rmp = vl_msg_api_alloc (sizeof (*rmp));
370   memset (rmp, 0, sizeof (*rmp));
371   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
372   rmp->context = context;
373   rmp->table_id = ntohl (table_id);
374   rmp->hit_next_index = ntohl (e->next_index);
375   rmp->advance = ntohl (e->advance);
376   rmp->opaque_index = ntohl (e->opaque_index);
377   rmp->match_length = ntohl (match_length);
378   clib_memcpy (rmp->match, e->key, match_length);
379
380   vl_msg_api_send_shmem (q, (u8 *) & rmp);
381 }
382
383 static void
384 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
385 {
386   vnet_classify_main_t *cm = &vnet_classify_main;
387   svm_queue_t *q;
388
389   u32 table_id = ntohl (mp->table_id);
390   vnet_classify_table_t *t;
391
392   q = vl_api_client_index_to_input_queue (mp->client_index);
393   if (!q)
394     return;
395
396   /* *INDENT-OFF* */
397   pool_foreach (t, cm->tables,
398   ({
399     if (table_id == t - cm->tables)
400       {
401         vnet_classify_bucket_t * b;
402         vnet_classify_entry_t * v, * save_v;
403         int i, j, k;
404
405         for (i = 0; i < t->nbuckets; i++)
406           {
407             b = &t->buckets [i];
408             if (b->offset == 0)
409               continue;
410
411             save_v = vnet_classify_get_entry (t, b->offset);
412             for (j = 0; j < (1<<b->log2_pages); j++)
413               {
414                 for (k = 0; k < t->entries_per_page; k++)
415                   {
416                     v = vnet_classify_entry_at_index
417                       (t, save_v, j*t->entries_per_page + k);
418                     if (vnet_classify_entry_is_free (v))
419                       continue;
420
421                     send_classify_session_details
422                       (q, table_id, t->match_n_vectors * sizeof (u32x4),
423                        v, mp->context);
424                   }
425               }
426           }
427         break;
428       }
429   }));
430   /* *INDENT-ON* */
431 }
432
433 static void
434   vl_api_flow_classify_set_interface_t_handler
435   (vl_api_flow_classify_set_interface_t * mp)
436 {
437   vlib_main_t *vm = vlib_get_main ();
438   vl_api_flow_classify_set_interface_reply_t *rmp;
439   int rv;
440   u32 sw_if_index, ip4_table_index, ip6_table_index;
441
442   ip4_table_index = ntohl (mp->ip4_table_index);
443   ip6_table_index = ntohl (mp->ip6_table_index);
444   sw_if_index = ntohl (mp->sw_if_index);
445
446   VALIDATE_SW_IF_INDEX (mp);
447
448   rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
449                                      ip6_table_index, mp->is_add);
450
451   BAD_SW_IF_INDEX_LABEL;
452
453   REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
454 }
455
456 static void
457 send_flow_classify_details (u32 sw_if_index,
458                             u32 table_index, svm_queue_t * q, u32 context)
459 {
460   vl_api_flow_classify_details_t *mp;
461
462   mp = vl_msg_api_alloc (sizeof (*mp));
463   memset (mp, 0, sizeof (*mp));
464   mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
465   mp->context = context;
466   mp->sw_if_index = htonl (sw_if_index);
467   mp->table_index = htonl (table_index);
468
469   vl_msg_api_send_shmem (q, (u8 *) & mp);
470 }
471
472 static void
473 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
474 {
475   svm_queue_t *q;
476   flow_classify_main_t *pcm = &flow_classify_main;
477   u32 *vec_tbl;
478   int i;
479
480   q = vl_api_client_index_to_input_queue (mp->client_index);
481   if (q == 0)
482     return;
483
484   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
485
486   if (vec_len (vec_tbl))
487     {
488       for (i = 0; i < vec_len (vec_tbl); i++)
489         {
490           if (vec_elt (vec_tbl, i) == ~0)
491             continue;
492
493           send_flow_classify_details (i, vec_elt (vec_tbl, i), q,
494                                       mp->context);
495         }
496     }
497 }
498
499 static void vl_api_classify_set_interface_ip_table_t_handler
500   (vl_api_classify_set_interface_ip_table_t * mp)
501 {
502   vlib_main_t *vm = vlib_get_main ();
503   vl_api_classify_set_interface_ip_table_reply_t *rmp;
504   int rv;
505
506   VALIDATE_SW_IF_INDEX (mp);
507
508   u32 table_index = ntohl (mp->table_index);
509   u32 sw_if_index = ntohl (mp->sw_if_index);
510
511   if (mp->is_ipv6)
512     rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
513   else
514     rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
515
516   BAD_SW_IF_INDEX_LABEL;
517
518   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
519 }
520
521 static void vl_api_classify_set_interface_l2_tables_t_handler
522   (vl_api_classify_set_interface_l2_tables_t * mp)
523 {
524   vl_api_classify_set_interface_l2_tables_reply_t *rmp;
525   int rv;
526   u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
527   int enable;
528
529   ip4_table_index = ntohl (mp->ip4_table_index);
530   ip6_table_index = ntohl (mp->ip6_table_index);
531   other_table_index = ntohl (mp->other_table_index);
532   sw_if_index = ntohl (mp->sw_if_index);
533
534   VALIDATE_SW_IF_INDEX (mp);
535
536   if (mp->is_input)
537     rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
538                                             ip6_table_index,
539                                             other_table_index);
540   else
541     rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
542                                              ip6_table_index,
543                                              other_table_index);
544
545   if (rv == 0)
546     {
547       if (ip4_table_index != ~0 || ip6_table_index != ~0
548           || other_table_index != ~0)
549         enable = 1;
550       else
551         enable = 0;
552
553       if (mp->is_input)
554         vnet_l2_input_classify_enable_disable (sw_if_index, enable);
555       else
556         vnet_l2_output_classify_enable_disable (sw_if_index, enable);
557     }
558
559   BAD_SW_IF_INDEX_LABEL;
560
561   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
562 }
563
564 static void vl_api_input_acl_set_interface_t_handler
565   (vl_api_input_acl_set_interface_t * mp)
566 {
567   vlib_main_t *vm = vlib_get_main ();
568   vl_api_input_acl_set_interface_reply_t *rmp;
569   int rv;
570
571   VALIDATE_SW_IF_INDEX (mp);
572
573   u32 ip4_table_index = ntohl (mp->ip4_table_index);
574   u32 ip6_table_index = ntohl (mp->ip6_table_index);
575   u32 l2_table_index = ntohl (mp->l2_table_index);
576   u32 sw_if_index = ntohl (mp->sw_if_index);
577
578   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
579                                  ip6_table_index, l2_table_index, mp->is_add);
580
581   BAD_SW_IF_INDEX_LABEL;
582
583   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
584 }
585
586 /*
587  * classify_api_hookup
588  * Add vpe's API message handlers to the table.
589  * vlib has alread mapped shared memory and
590  * added the client registration handlers.
591  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
592  */
593 #define vl_msg_name_crc_list
594 #include <vnet/vnet_all_api_h.h>
595 #undef vl_msg_name_crc_list
596
597 static void
598 setup_message_id_table (api_main_t * am)
599 {
600 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
601   foreach_vl_msg_name_crc_classify;
602 #undef _
603 }
604
605 static clib_error_t *
606 classify_api_hookup (vlib_main_t * vm)
607 {
608   api_main_t *am = &api_main;
609
610 #define _(N,n)                                                  \
611     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
612                            vl_api_##n##_t_handler,              \
613                            vl_noop_handler,                     \
614                            vl_api_##n##_t_endian,               \
615                            vl_api_##n##_t_print,                \
616                            sizeof(vl_api_##n##_t), 1);
617   foreach_vpe_api_msg;
618 #undef _
619
620   /*
621    * Set up the (msg_name, crc, message-id) table
622    */
623   setup_message_id_table (am);
624
625   return 0;
626 }
627
628 VLIB_API_INIT_FUNCTION (classify_api_hookup);
629
630 /*
631  * fd.io coding-style-patch-verification: ON
632  *
633  * Local Variables:
634  * eval: (c-set-style "gnu")
635  * End:
636  */