c11 safe string handling support
[vpp.git] / src / vnet / dpo / l3_proxy_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  * @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  */
26 l3_proxy_dpo_t *l3_proxy_dpo_pool;
27
28 static l3_proxy_dpo_t *
29 l3_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);
34     clib_memset(l3p, 0, sizeof(*l3p));
35
36     return (l3p);
37 }
38
39 static l3_proxy_dpo_t *
40 l3_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  */
55 void
56 l3_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
69 static void
70 l3_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
78 static void
79 l3_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
92 static u32
93 l3_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
102 static u8*
103 format_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     {
119         return (format(s, "dpo-l3_proxy: %U",
120                        format_vnet_sw_interface_name, vnm,
121                        vnet_get_sw_interface(vnm, l3p->l3p_sw_if_index)));
122     }
123     else
124     {
125         return (format(s, "dpo-l3-proxy"));
126     }
127 }
128
129 static void
130 l3_proxy_dpo_mem_show (void)
131 {
132     fib_show_memory_usage("L3 Proxy",
133                           pool_elts(l3_proxy_dpo_pool),
134                           pool_len(l3_proxy_dpo_pool),
135                           sizeof(l3_proxy_dpo_t));
136 }
137
138 const static dpo_vft_t l3_proxy_vft = {
139     .dv_lock = l3_proxy_dpo_lock,
140     .dv_unlock = l3_proxy_dpo_unlock,
141     .dv_format = format_l3_proxy_dpo,
142     .dv_get_urpf = l3_proxy_dpo_get_urpf,
143     .dv_mem_show = l3_proxy_dpo_mem_show,
144 };
145
146 /**
147  * @brief The per-protocol VLIB graph nodes that are assigned to a l3_proxy
148  *        object.
149  *
150  * this means that these graph nodes are ones from which a l3_proxy is the
151  * parent object in the DPO-graph.
152  */
153 const static char* const l3_proxy_ip4_nodes[] =
154 {
155     "ip4-local",
156     NULL,
157 };
158 const static char* const l3_proxy_ip6_nodes[] =
159 {
160     "ip6-local",
161     NULL,
162 };
163
164 const static char* const * const l3_proxy_nodes[DPO_PROTO_NUM] =
165 {
166     [DPO_PROTO_IP4]  = l3_proxy_ip4_nodes,
167     [DPO_PROTO_IP6]  = l3_proxy_ip6_nodes,
168     [DPO_PROTO_MPLS] = NULL,
169 };
170
171 void
172 l3_proxy_dpo_module_init (void)
173 {
174     dpo_register(DPO_L3_PROXY, &l3_proxy_vft, l3_proxy_nodes);
175 }