c11 safe string handling support
[vpp.git] / src / vnet / dpo / receive_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 receiveing the packet, i.e. it's for-us
18  */
19 #include <vlib/vlib.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/dpo/receive_dpo.h>
22
23 /**
24  * @brief pool of all receive DPOs
25  */
26 receive_dpo_t *receive_dpo_pool;
27
28 static receive_dpo_t *
29 receive_dpo_alloc (void)
30 {
31     receive_dpo_t *rd;
32
33     pool_get_aligned(receive_dpo_pool, rd, CLIB_CACHE_LINE_BYTES);
34     clib_memset(rd, 0, sizeof(*rd));
35
36     return (rd);
37 }
38
39 static receive_dpo_t *
40 receive_dpo_get_from_dpo (const dpo_id_t *dpo)
41 {
42     ASSERT(DPO_RECEIVE == dpo->dpoi_type);
43
44     return (receive_dpo_get(dpo->dpoi_index));
45 }
46
47
48 /*
49  * receive_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 receive prefix, the next-hop
53  * passed here is the local prefix on the same interface.
54  */
55 void
56 receive_dpo_add_or_lock (dpo_proto_t proto,
57                          u32 sw_if_index,
58                          const ip46_address_t *nh_addr,
59                          dpo_id_t *dpo)
60 {
61     receive_dpo_t *rd;
62
63     rd = receive_dpo_alloc();
64
65     rd->rd_sw_if_index = sw_if_index;
66     if (NULL != nh_addr)
67     {
68         rd->rd_addr = *nh_addr;
69     }
70
71     dpo_set(dpo, DPO_RECEIVE, proto, (rd - receive_dpo_pool));
72 }
73
74 static void
75 receive_dpo_lock (dpo_id_t *dpo)
76 {
77     receive_dpo_t *rd;
78
79     rd = receive_dpo_get_from_dpo(dpo);
80     rd->rd_locks++;
81 }
82
83 static void
84 receive_dpo_unlock (dpo_id_t *dpo)
85 {
86     receive_dpo_t *rd;
87
88     rd = receive_dpo_get_from_dpo(dpo);
89     rd->rd_locks--;
90
91     if (0 == rd->rd_locks)
92     {
93         pool_put(receive_dpo_pool, rd);
94     }
95 }
96
97 static u8*
98 format_receive_dpo (u8 *s, va_list *ap)
99 {
100     CLIB_UNUSED(index_t index) = va_arg(*ap, index_t);
101     CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
102     vnet_main_t * vnm = vnet_get_main();
103     receive_dpo_t *rd;
104
105     if (pool_is_free_index(receive_dpo_pool, index))
106     {
107         return (format(s, "dpo-receive DELETED"));
108     }
109
110     rd = receive_dpo_get(index);
111
112     if (~0 != rd->rd_sw_if_index)
113     {
114         return (format(s, "dpo-receive: %U on %U",
115                        format_ip46_address, &rd->rd_addr, IP46_TYPE_ANY,
116                        format_vnet_sw_interface_name, vnm,
117                        vnet_get_sw_interface(vnm, rd->rd_sw_if_index)));
118     }
119     else
120     {
121         return (format(s, "dpo-receive"));
122     }
123 }
124
125 static void
126 receive_dpo_mem_show (void)
127 {
128     fib_show_memory_usage("Receive",
129                           pool_elts(receive_dpo_pool),
130                           pool_len(receive_dpo_pool),
131                           sizeof(receive_dpo_t));
132 }
133
134 const static dpo_vft_t receive_vft = {
135     .dv_lock = receive_dpo_lock,
136     .dv_unlock = receive_dpo_unlock,
137     .dv_format = format_receive_dpo,
138     .dv_mem_show = receive_dpo_mem_show,
139 };
140
141 /**
142  * @brief The per-protocol VLIB graph nodes that are assigned to a receive
143  *        object.
144  *
145  * this means that these graph nodes are ones from which a receive is the
146  * parent object in the DPO-graph.
147  */
148 const static char* const receive_ip4_nodes[] =
149 {
150     "ip4-local",
151     NULL,
152 };
153 const static char* const receive_ip6_nodes[] =
154 {
155     "ip6-local",
156     NULL,
157 };
158
159 const static char* const * const receive_nodes[DPO_PROTO_NUM] =
160 {
161     [DPO_PROTO_IP4]  = receive_ip4_nodes,
162     [DPO_PROTO_IP6]  = receive_ip6_nodes,
163     [DPO_PROTO_MPLS] = NULL,
164 };
165
166 void
167 receive_dpo_module_init (void)
168 {
169     dpo_register(DPO_RECEIVE, &receive_vft, receive_nodes);
170 }