blob: bc536a10266b3431d991b7b693b34358ee31750b [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
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 */
23classify_dpo_t *classify_dpo_pool;
24
25static classify_dpo_t *
26classify_dpo_alloc (void)
27{
28 classify_dpo_t *cd;
Dave Barach26d890e2020-06-05 09:42:50 -040029 vlib_main_t *vm;
30 u8 did_barrier_sync;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010031
Dave Barach26d890e2020-06-05 09:42:50 -040032 dpo_pool_barrier_sync (vm, classify_dpo_pool, did_barrier_sync);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010033 pool_get_aligned(classify_dpo_pool, cd, CLIB_CACHE_LINE_BYTES);
Dave Barach26d890e2020-06-05 09:42:50 -040034 dpo_pool_barrier_release (vm, did_barrier_sync);
35
Dave Barachb7b92992018-10-17 10:38:51 -040036 clib_memset(cd, 0, sizeof(*cd));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010037
38 return (cd);
39}
40
41static index_t
42classify_dpo_get_index (classify_dpo_t *cd)
43{
44 return (cd - classify_dpo_pool);
45}
46
47index_t
Neale Ranns8c1bebe2016-10-28 06:31:54 -070048classify_dpo_create (dpo_proto_t proto,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010049 u32 classify_table_index)
50{
51 classify_dpo_t *cd;
52
53 cd = classify_dpo_alloc();
54 cd->cd_proto = proto;
55 cd->cd_table_index = classify_table_index;
56
57 return (classify_dpo_get_index(cd));
58}
59
60u8*
61format_classify_dpo (u8 *s, va_list *args)
62{
63 index_t index = va_arg (*args, index_t);
64 CLIB_UNUSED(u32 indent) = va_arg (*args, u32);
65 classify_dpo_t *cd;
66
67 cd = classify_dpo_get(index);
68
Neale Ranns8c1bebe2016-10-28 06:31:54 -070069 return (format(s, "%U-classify:[%d]:table:%d",
70 format_dpo_proto, cd->cd_proto,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010071 index, cd->cd_table_index));
72}
73
74static void
75classify_dpo_lock (dpo_id_t *dpo)
76{
77 classify_dpo_t *cd;
78
79 cd = classify_dpo_get(dpo->dpoi_index);
80
81 cd->cd_locks++;
82}
83
84static void
85classify_dpo_unlock (dpo_id_t *dpo)
86{
87 classify_dpo_t *cd;
88
89 cd = classify_dpo_get(dpo->dpoi_index);
90
91 cd->cd_locks--;
92
93 if (0 == cd->cd_locks)
94 {
95 pool_put(classify_dpo_pool, cd);
96 }
97}
98
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010099static void
100classify_dpo_mem_show (void)
101{
102 fib_show_memory_usage("Classify",
103 pool_elts(classify_dpo_pool),
104 pool_len(classify_dpo_pool),
105 sizeof(classify_dpo_t));
106}
107
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100108const static dpo_vft_t cd_vft = {
109 .dv_lock = classify_dpo_lock,
110 .dv_unlock = classify_dpo_unlock,
111 .dv_format = format_classify_dpo,
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100112 .dv_mem_show = classify_dpo_mem_show,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100113};
114
115const static char* const classify_ip4_nodes[] =
116{
117 "ip4-classify",
118 NULL,
119};
120const static char* const classify_ip6_nodes[] =
121{
122 "ip6-classify",
123 NULL,
124};
125const static char* const * const classify_nodes[DPO_PROTO_NUM] =
126{
127 [DPO_PROTO_IP4] = classify_ip4_nodes,
128 [DPO_PROTO_IP6] = classify_ip6_nodes,
129 [DPO_PROTO_MPLS] = NULL,
130};
131
132void
133classify_dpo_module_init (void)
134{
135 dpo_register(DPO_CLASSIFY, &cd_vft, classify_nodes);
136}