blob: f89554d775fe419f31b5006add4ff8e37afd939c [file] [log] [blame]
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -07001/*
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 * @brief
17 * The data-path object representing l3_proxying the packet, i.e. it's for-us
18 */
19#include <vlib/vlib.h>
20#include <vnet/ip/ip.h>
21#include <vnet/dpo/l3_proxy_dpo.h>
22
23/**
24 * @brief pool of all l3_proxy DPOs
25 */
26l3_proxy_dpo_t *l3_proxy_dpo_pool;
27
28static l3_proxy_dpo_t *
29l3_proxy_dpo_alloc (void)
30{
31 l3_proxy_dpo_t *l3p;
32
33 pool_get_aligned(l3_proxy_dpo_pool, l3p, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040034 clib_memset(l3p, 0, sizeof(*l3p));
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -070035
36 return (l3p);
37}
38
39static l3_proxy_dpo_t *
40l3_proxy_dpo_get_from_dpo (const dpo_id_t *dpo)
41{
42 ASSERT(DPO_L3_PROXY == dpo->dpoi_type);
43
44 return (l3_proxy_dpo_get(dpo->dpoi_index));
45}
46
47
48/*
49 * l3_proxy_dpo_add_or_lock
50 *
51 * The next_hop address here is used for source address selection in the DP.
52 * The local adj is added to an interface's l3_proxy prefix, the next-hop
53 * passed here is the local prefix on the same interface.
54 */
55void
56l3_proxy_dpo_add_or_lock (dpo_proto_t proto,
57 u32 sw_if_index,
58 dpo_id_t *dpo)
59{
60 l3_proxy_dpo_t *l3p;
61
62 l3p = l3_proxy_dpo_alloc();
63
64 l3p->l3p_sw_if_index = sw_if_index;
65
66 dpo_set(dpo, DPO_L3_PROXY, proto, (l3p - l3_proxy_dpo_pool));
67}
68
69static void
70l3_proxy_dpo_lock (dpo_id_t *dpo)
71{
72 l3_proxy_dpo_t *l3p;
73
74 l3p = l3_proxy_dpo_get_from_dpo(dpo);
75 l3p->l3p_locks++;
76}
77
78static void
79l3_proxy_dpo_unlock (dpo_id_t *dpo)
80{
81 l3_proxy_dpo_t *l3p;
82
83 l3p = l3_proxy_dpo_get_from_dpo(dpo);
84 l3p->l3p_locks--;
85
86 if (0 == l3p->l3p_locks)
87 {
88 pool_put(l3_proxy_dpo_pool, l3p);
89 }
90}
91
92static u32
93l3_proxy_dpo_get_urpf (const dpo_id_t *dpo)
94{
95 l3_proxy_dpo_t *l3p;
96
97 l3p = l3_proxy_dpo_get_from_dpo(dpo);
98
99 return (l3p->l3p_sw_if_index);
100}
101
102static u8*
103format_l3_proxy_dpo (u8 *s, va_list *ap)
104{
105 CLIB_UNUSED(index_t index) = va_arg(*ap, index_t);
106 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
107 vnet_main_t * vnm = vnet_get_main();
108 l3_proxy_dpo_t *l3p;
109
110 if (pool_is_free_index(l3_proxy_dpo_pool, index))
111 {
112 return (format(s, "dpo-l3_proxy DELETED"));
113 }
114
115 l3p = l3_proxy_dpo_get(index);
116
117 if (~0 != l3p->l3p_sw_if_index)
118 {
Vladislav Grishenkofb9d1ac2022-08-04 20:36:45 +0500119 return (format (s, "dpo-l3_proxy: %U", format_vnet_sw_if_index_name, vnm,
120 l3p->l3p_sw_if_index));
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -0700121 }
122 else
123 {
124 return (format(s, "dpo-l3-proxy"));
125 }
126}
127
128static void
129l3_proxy_dpo_mem_show (void)
130{
131 fib_show_memory_usage("L3 Proxy",
132 pool_elts(l3_proxy_dpo_pool),
133 pool_len(l3_proxy_dpo_pool),
134 sizeof(l3_proxy_dpo_t));
135}
136
137const static dpo_vft_t l3_proxy_vft = {
138 .dv_lock = l3_proxy_dpo_lock,
139 .dv_unlock = l3_proxy_dpo_unlock,
140 .dv_format = format_l3_proxy_dpo,
141 .dv_get_urpf = l3_proxy_dpo_get_urpf,
142 .dv_mem_show = l3_proxy_dpo_mem_show,
143};
144
145/**
146 * @brief The per-protocol VLIB graph nodes that are assigned to a l3_proxy
147 * object.
148 *
149 * this means that these graph nodes are ones from which a l3_proxy is the
150 * parent object in the DPO-graph.
151 */
152const static char* const l3_proxy_ip4_nodes[] =
153{
154 "ip4-local",
155 NULL,
156};
157const static char* const l3_proxy_ip6_nodes[] =
158{
159 "ip6-local",
160 NULL,
161};
162
163const static char* const * const l3_proxy_nodes[DPO_PROTO_NUM] =
164{
165 [DPO_PROTO_IP4] = l3_proxy_ip4_nodes,
166 [DPO_PROTO_IP6] = l3_proxy_ip6_nodes,
167 [DPO_PROTO_MPLS] = NULL,
168};
169
170void
171l3_proxy_dpo_module_init (void)
172{
173 dpo_register(DPO_L3_PROXY, &l3_proxy_vft, l3_proxy_nodes);
174}