2 * proxy_node.c: common dhcp v4 and v6 proxy node processing
4 * Copyright (c) 2013 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/dhcp/dhcp_proxy.h>
19 #include <vnet/fib/fib_table.h>
20 #include <vnet/mfib/mfib_table.h>
23 * @brief Shard 4/6 instance of DHCP main
25 dhcp_proxy_main_t dhcp_proxy_main;
28 dhcp_proxy_rx_table_lock (fib_protocol_t proto,
31 if (FIB_PROTOCOL_IP4 == proto)
32 fib_table_lock(fib_index, proto, FIB_SOURCE_DHCP);
34 mfib_table_lock(fib_index, proto, MFIB_SOURCE_DHCP);
38 dhcp_proxy_rx_table_unlock (fib_protocol_t proto,
41 if (FIB_PROTOCOL_IP4 == proto)
42 fib_table_unlock(fib_index, proto, FIB_SOURCE_DHCP);
44 mfib_table_unlock(fib_index, proto, MFIB_SOURCE_DHCP);
48 dhcp_proxy_rx_table_get_table_id (fib_protocol_t proto,
51 if (FIB_PROTOCOL_IP4 == proto)
55 fib = fib_table_get(fib_index, proto);
57 return (fib->ft_table_id);
63 mfib = mfib_table_get(fib_index, proto);
65 return (mfib->mft_table_id);
70 dhcp_proxy_walk (fib_protocol_t proto,
71 dhcp_proxy_walk_fn_t fn,
74 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
75 dhcp_proxy_t * server;
78 vec_foreach_index (i, dpm->dhcp_server_index_by_rx_fib_index[proto])
80 server_index = dpm->dhcp_server_index_by_rx_fib_index[proto][i];
81 if (~0 == server_index)
84 server = pool_elt_at_index (dpm->dhcp_servers[proto], server_index);
92 dhcp_vss_walk (fib_protocol_t proto,
93 dhcp_vss_walk_fn_t fn,
96 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
102 vec_foreach_index (i, dpm->vss_index_by_rx_fib_index[proto])
104 vss_index = dpm->vss_index_by_rx_fib_index[proto][i];
108 vss = pool_elt_at_index (dpm->vss[proto], vss_index);
110 if (FIB_PROTOCOL_IP4 == proto)
112 fib = fib_table_get(i, proto);
114 if (!fn(vss, fib->ft_table_id, ctx))
119 mfib = mfib_table_get(i, proto);
121 if (!fn(vss, mfib->mft_table_id, ctx))
128 dhcp_proxy_server_find (dhcp_proxy_t *proxy,
129 fib_protocol_t proto,
130 ip46_address_t *addr,
133 dhcp_server_t *server;
136 vec_foreach_index(ii, proxy->dhcp_servers)
138 server = &proxy->dhcp_servers[ii];
139 fib_index = fib_table_find(proto, server_table_id);
141 if (ip46_address_is_equal(&server->dhcp_server,
143 (server->server_fib_index == fib_index))
152 dhcp_proxy_server_del (fib_protocol_t proto,
154 ip46_address_t *addr,
157 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
158 dhcp_proxy_t *proxy = 0;
160 proxy = dhcp_get_proxy(dpm, rx_fib_index, proto);
164 dhcp_server_t *server;
167 index = dhcp_proxy_server_find(proxy, proto, addr, server_table_id);
171 server = &proxy->dhcp_servers[index];
172 fib_table_unlock (server->server_fib_index, proto, FIB_SOURCE_DHCP);
174 vec_del1(proxy->dhcp_servers, index);
176 if (0 == vec_len(proxy->dhcp_servers))
178 /* no servers left, delete the proxy config */
179 dpm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] = ~0;
180 vec_free(proxy->dhcp_servers);
181 pool_put (dpm->dhcp_servers[proto], proxy);
187 /* the proxy still exists */
192 dhcp_proxy_server_add (fib_protocol_t proto,
193 ip46_address_t *addr,
194 ip46_address_t *src_address,
198 dhcp_proxy_main_t * dpm = &dhcp_proxy_main;
199 dhcp_proxy_t * proxy = 0;
202 proxy = dhcp_get_proxy(dpm, rx_fib_index, proto);
206 vec_validate_init_empty(dpm->dhcp_server_index_by_rx_fib_index[proto],
210 pool_get (dpm->dhcp_servers[proto], proxy);
211 memset (proxy, 0, sizeof (*proxy));
214 dpm->dhcp_server_index_by_rx_fib_index[proto][rx_fib_index] =
215 proxy - dpm->dhcp_servers[proto];
217 proxy->dhcp_src_address = *src_address;
218 proxy->rx_fib_index = rx_fib_index;
222 if (~0 != dhcp_proxy_server_find(proxy, proto, addr, server_table_id))
228 dhcp_server_t server = {
229 .dhcp_server = *addr,
230 .server_fib_index = fib_table_find_or_create_and_lock(proto,
235 vec_add1(proxy->dhcp_servers, server);
240 typedef struct dhcp4_proxy_dump_walk_ctx_t_
242 fib_protocol_t proto;
245 } dhcp_proxy_dump_walk_cxt_t;
248 dhcp_proxy_dump_walk (dhcp_proxy_t *proxy,
251 dhcp_proxy_dump_walk_cxt_t *ctx = arg;
253 dhcp_send_details(ctx->proto,
262 dhcp_proxy_dump (fib_protocol_t proto,
266 dhcp_proxy_dump_walk_cxt_t ctx = {
271 dhcp_proxy_walk(proto, dhcp_proxy_dump_walk, &ctx);
275 dhcp_vss_show_walk (dhcp_vss_t *vss,
279 vlib_main_t * vm = ctx;
281 vlib_cli_output (vm, "%=6d%=6d%=12d",
289 int dhcp_proxy_set_vss (fib_protocol_t proto,
295 dhcp_proxy_main_t *dm = &dhcp_proxy_main;
296 dhcp_vss_t *v = NULL;
300 if (proto == FIB_PROTOCOL_IP4)
301 rx_fib_index = fib_table_find_or_create_and_lock(proto, tbl_id,
304 rx_fib_index = mfib_table_find_or_create_and_lock(proto, tbl_id,
306 v = dhcp_get_vss_info(dm, rx_fib_index, proto);
312 /* release the lock held on the table when the VSS
313 * info was created */
314 dhcp_proxy_rx_table_unlock (proto, rx_fib_index);
316 pool_put (dm->vss[proto], v);
317 dm->vss_index_by_rx_fib_index[proto][rx_fib_index] = ~0;
321 /* this is a modify */
329 rc = VNET_API_ERROR_NO_SUCH_ENTRY;
332 /* create a new entry */
333 vec_validate_init_empty(dm->vss_index_by_rx_fib_index[proto],
336 /* hold a lock on the table whilst the VSS info exist */
337 pool_get (dm->vss[proto], v);
341 dm->vss_index_by_rx_fib_index[proto][rx_fib_index] =
343 dhcp_proxy_rx_table_lock (proto, rx_fib_index);
347 /* Release the lock taken during the create_or_lock at the start */
348 dhcp_proxy_rx_table_unlock (proto, rx_fib_index);