blob: 949dbfa6587de254923693f4b81588a9b2d5e98d [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 * @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 */
26receive_dpo_t *receive_dpo_pool;
27
Neale Ranns9e829a82018-12-17 05:50:32 -080028int
29dpo_is_receive (const dpo_id_t *dpo)
30{
31 return (dpo->dpoi_type == DPO_RECEIVE);
32}
33
Neale Ranns0bfe5d82016-08-25 15:29:12 +010034static receive_dpo_t *
35receive_dpo_alloc (void)
36{
37 receive_dpo_t *rd;
38
39 pool_get_aligned(receive_dpo_pool, rd, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040040 clib_memset(rd, 0, sizeof(*rd));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010041
42 return (rd);
43}
44
45static receive_dpo_t *
46receive_dpo_get_from_dpo (const dpo_id_t *dpo)
47{
48 ASSERT(DPO_RECEIVE == dpo->dpoi_type);
49
50 return (receive_dpo_get(dpo->dpoi_index));
51}
52
53
54/*
55 * receive_dpo_add_or_lock
56 *
57 * The next_hop address here is used for source address selection in the DP.
58 * The local adj is added to an interface's receive prefix, the next-hop
59 * passed here is the local prefix on the same interface.
60 */
61void
62receive_dpo_add_or_lock (dpo_proto_t proto,
63 u32 sw_if_index,
64 const ip46_address_t *nh_addr,
65 dpo_id_t *dpo)
66{
67 receive_dpo_t *rd;
68
69 rd = receive_dpo_alloc();
70
71 rd->rd_sw_if_index = sw_if_index;
72 if (NULL != nh_addr)
73 {
74 rd->rd_addr = *nh_addr;
75 }
76
77 dpo_set(dpo, DPO_RECEIVE, proto, (rd - receive_dpo_pool));
78}
79
80static void
81receive_dpo_lock (dpo_id_t *dpo)
82{
83 receive_dpo_t *rd;
84
85 rd = receive_dpo_get_from_dpo(dpo);
86 rd->rd_locks++;
87}
88
89static void
90receive_dpo_unlock (dpo_id_t *dpo)
91{
92 receive_dpo_t *rd;
93
94 rd = receive_dpo_get_from_dpo(dpo);
95 rd->rd_locks--;
96
97 if (0 == rd->rd_locks)
98 {
99 pool_put(receive_dpo_pool, rd);
100 }
101}
102
103static u8*
104format_receive_dpo (u8 *s, va_list *ap)
105{
Billy McFallcfcf1e22016-10-14 09:51:49 -0400106 CLIB_UNUSED(index_t index) = va_arg(*ap, index_t);
107 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100108 vnet_main_t * vnm = vnet_get_main();
109 receive_dpo_t *rd;
110
Neale Ranns20a175a2017-02-14 07:28:41 -0800111 if (pool_is_free_index(receive_dpo_pool, index))
112 {
113 return (format(s, "dpo-receive DELETED"));
114 }
115
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100116 rd = receive_dpo_get(index);
117
118 if (~0 != rd->rd_sw_if_index)
119 {
120 return (format(s, "dpo-receive: %U on %U",
121 format_ip46_address, &rd->rd_addr, IP46_TYPE_ANY,
122 format_vnet_sw_interface_name, vnm,
123 vnet_get_sw_interface(vnm, rd->rd_sw_if_index)));
124 }
125 else
126 {
127 return (format(s, "dpo-receive"));
128 }
129}
130
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100131static void
132receive_dpo_mem_show (void)
133{
134 fib_show_memory_usage("Receive",
135 pool_elts(receive_dpo_pool),
136 pool_len(receive_dpo_pool),
137 sizeof(receive_dpo_t));
138}
139
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100140const static dpo_vft_t receive_vft = {
141 .dv_lock = receive_dpo_lock,
142 .dv_unlock = receive_dpo_unlock,
143 .dv_format = format_receive_dpo,
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100144 .dv_mem_show = receive_dpo_mem_show,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100145};
146
147/**
148 * @brief The per-protocol VLIB graph nodes that are assigned to a receive
149 * object.
150 *
151 * this means that these graph nodes are ones from which a receive is the
152 * parent object in the DPO-graph.
153 */
154const static char* const receive_ip4_nodes[] =
155{
156 "ip4-local",
157 NULL,
158};
159const static char* const receive_ip6_nodes[] =
160{
161 "ip6-local",
162 NULL,
163};
164
165const static char* const * const receive_nodes[DPO_PROTO_NUM] =
166{
167 [DPO_PROTO_IP4] = receive_ip4_nodes,
168 [DPO_PROTO_IP6] = receive_ip6_nodes,
169 [DPO_PROTO_MPLS] = NULL,
170};
171
172void
173receive_dpo_module_init (void)
174{
175 dpo_register(DPO_RECEIVE, &receive_vft, receive_nodes);
176}