93f3f0ae27556052085afc0dd8587a38024c8100
[vpp.git] / vnet / vnet / dpo / classify_dpo.c
1 /*
2  * Copyright (c) 2016 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 <vnet/ip/ip.h>
17 #include <vnet/dpo/classify_dpo.h>
18 #include <vnet/mpls/mpls.h>
19
20 /*
21  * pool of all MPLS Label DPOs
22  */
23 classify_dpo_t *classify_dpo_pool;
24
25 static classify_dpo_t *
26 classify_dpo_alloc (void)
27 {
28     classify_dpo_t *cd;
29
30     pool_get_aligned(classify_dpo_pool, cd, CLIB_CACHE_LINE_BYTES);
31     memset(cd, 0, sizeof(*cd));
32
33     return (cd);
34 }
35
36 static index_t
37 classify_dpo_get_index (classify_dpo_t *cd)
38 {
39     return (cd - classify_dpo_pool);
40 }
41
42 index_t
43 classify_dpo_create (fib_protocol_t proto,
44                      u32 classify_table_index)
45 {
46     classify_dpo_t *cd;
47
48     cd = classify_dpo_alloc();
49     cd->cd_proto = proto;
50     cd->cd_table_index = classify_table_index;
51
52     return (classify_dpo_get_index(cd));
53 }
54
55 u8*
56 format_classify_dpo (u8 *s, va_list *args)
57 {
58     index_t index = va_arg (*args, index_t);
59     CLIB_UNUSED(u32 indent) = va_arg (*args, u32);
60     classify_dpo_t *cd;
61
62     cd = classify_dpo_get(index);
63
64     return (format(s, "classify:[%d]:table:%d",
65                    index, cd->cd_table_index));
66 }
67
68 static void
69 classify_dpo_lock (dpo_id_t *dpo)
70 {
71     classify_dpo_t *cd;
72
73     cd = classify_dpo_get(dpo->dpoi_index);
74
75     cd->cd_locks++;
76 }
77
78 static void
79 classify_dpo_unlock (dpo_id_t *dpo)
80 {
81     classify_dpo_t *cd;
82
83     cd = classify_dpo_get(dpo->dpoi_index);
84
85     cd->cd_locks--;
86
87     if (0 == cd->cd_locks)
88     {
89         pool_put(classify_dpo_pool, cd);
90     }
91 }
92
93 static void
94 classify_dpo_mem_show (void)
95 {
96     fib_show_memory_usage("Classify",
97                           pool_elts(classify_dpo_pool),
98                           pool_len(classify_dpo_pool),
99                           sizeof(classify_dpo_t));
100 }
101
102 const static dpo_vft_t cd_vft = {
103     .dv_lock = classify_dpo_lock,
104     .dv_unlock = classify_dpo_unlock,
105     .dv_format = format_classify_dpo,
106     .dv_mem_show = classify_dpo_mem_show,
107 };
108
109 const static char* const classify_ip4_nodes[] =
110 {
111     "ip4-classify",
112     NULL,
113 };
114 const static char* const classify_ip6_nodes[] =
115 {
116     "ip6-classify",
117     NULL,
118 };
119 const static char* const * const classify_nodes[DPO_PROTO_NUM] =
120 {
121     [DPO_PROTO_IP4]  = classify_ip4_nodes,
122     [DPO_PROTO_IP6]  = classify_ip6_nodes,
123     [DPO_PROTO_MPLS] = NULL,
124 };
125
126 void
127 classify_dpo_module_init (void)
128 {
129     dpo_register(DPO_CLASSIFY, &cd_vft, classify_nodes);
130 }