FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
dhcp_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * dhcp_api.c - dhcp api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22 
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/dhcp/dhcp_proxy.h>
26 #include <vnet/dhcp/client.h>
27 #include <vnet/fib/fib_table.h>
28 
29 #include <vnet/vnet_msg_enum.h>
30 
31 #define vl_typedefs /* define message structures */
32 #include <vnet/vnet_all_api_h.h>
33 #undef vl_typedefs
34 
35 #define vl_endianfun /* define message structures */
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_endianfun
38 
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #define vl_printfun
42 #include <vnet/vnet_all_api_h.h>
43 #undef vl_printfun
44 
46 
47 #define foreach_vpe_api_msg \
48 _(DHCP_PROXY_CONFIG,dhcp_proxy_config) \
49 _(DHCP_PROXY_DUMP,dhcp_proxy_dump) \
50 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss) \
51 _(DHCP_CLIENT_CONFIG, dhcp_client_config)
52 
53 
54 static void
56 {
58  int rv;
59 
60  rv = dhcp_proxy_set_vss ((mp->is_ipv6 ?
63  ntohl (mp->tbl_id),
64  ntohl (mp->oui),
65  ntohl (mp->fib_id), (int) mp->is_add == 0);
66 
67  REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
68 }
69 
70 
73 {
75  ip46_address_t src, server;
76  int rv = -1;
77 
78  if (mp->is_ipv6)
79  {
80  clib_memcpy (&src.ip6, mp->dhcp_src_address, sizeof (src.ip6));
81  clib_memcpy (&server.ip6, mp->dhcp_server, sizeof (server.ip6));
82 
83  rv = dhcp6_proxy_set_server (&server,
84  &src,
85  (u32) ntohl (mp->rx_vrf_id),
86  (u32) ntohl (mp->server_vrf_id),
87  (int) (mp->is_add == 0));
88  }
89  else
90  {
91  ip46_address_reset (&src);
92  ip46_address_reset (&server);
93 
94  clib_memcpy (&src.ip4, mp->dhcp_src_address, sizeof (src.ip4));
95  clib_memcpy (&server.ip4, mp->dhcp_server, sizeof (server.ip4));
96 
97  rv = dhcp4_proxy_set_server (&server,
98  &src,
99  (u32) ntohl (mp->rx_vrf_id),
100  (u32) ntohl (mp->server_vrf_id),
101  (int) (mp->is_add == 0));
102  }
103 
104 
105  REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
106 }
107 
108 static void
110 {
112 
114  if (q == 0)
115  return;
116 
117  dhcp_proxy_dump ((mp->is_ip6 == 1 ?
119 }
120 
121 void
123  void *opaque, u32 context, dhcp_proxy_t * proxy)
124 {
126  unix_shared_memory_queue_t *q = opaque;
127  vl_api_dhcp_server_t *v_server;
128  dhcp_server_t *server;
129  fib_table_t *s_fib;
130  dhcp_vss_t *vss;
131  u32 count;
132  size_t n;
133 
134  count = vec_len (proxy->dhcp_servers);
135  n = sizeof (*mp) + (count * sizeof (vl_api_dhcp_server_t));
136  mp = vl_msg_api_alloc (n);
137  if (!mp)
138  return;
139  memset (mp, 0, n);
140  mp->_vl_msg_id = ntohs (VL_API_DHCP_PROXY_DETAILS);
141  mp->context = context;
142  mp->count = count;
143 
144  mp->is_ipv6 = (proto == FIB_PROTOCOL_IP6);
145  mp->rx_vrf_id =
146  htonl (dhcp_proxy_rx_table_get_table_id (proto, proxy->rx_fib_index));
147 
148  vss = dhcp_get_vss_info (&dhcp_proxy_main, proxy->rx_fib_index, proto);
149 
150  if (NULL != vss)
151  {
152  mp->vss_oui = htonl (vss->oui);
153  mp->vss_fib_id = htonl (vss->fib_id);
154  }
155 
156  vec_foreach_index (count, proxy->dhcp_servers)
157  {
158  server = &proxy->dhcp_servers[count];
159  v_server = &mp->servers[count];
160 
161  s_fib = fib_table_get (server->server_fib_index, proto);
162 
163  v_server->server_vrf_id = htonl (s_fib->ft_table_id);
164 
165  if (mp->is_ipv6)
166  {
167  memcpy (v_server->dhcp_server, &server->dhcp_server.ip6, 16);
168  }
169  else
170  {
171  /* put the address in the first bytes */
172  memcpy (v_server->dhcp_server, &server->dhcp_server.ip4, 4);
173  }
174  }
175 
176  if (mp->is_ipv6)
177  {
178  memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip6, 16);
179  }
180  else
181  {
182  /* put the address in the first bytes */
183  memcpy (mp->dhcp_src_address, &proxy->dhcp_src_address.ip4, 4);
184  }
185  vl_msg_api_send_shmem (q, (u8 *) & mp);
186 }
187 
188 void
189 dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
190  u8 is_ipv6, u8 * host_address, u8 * router_address,
191  u8 * host_mac)
192 {
195 
196  q = vl_api_client_index_to_input_queue (client_index);
197  if (!q)
198  return;
199 
200  mp = vl_msg_api_alloc (sizeof (*mp));
201  mp->client_index = client_index;
202  mp->pid = pid;
203  mp->is_ipv6 = is_ipv6;
204  clib_memcpy (&mp->hostname, hostname, vec_len (hostname));
205  mp->hostname[vec_len (hostname) + 1] = '\n';
206  clib_memcpy (&mp->host_address[0], host_address, 16);
207  clib_memcpy (&mp->router_address[0], router_address, 16);
208 
209  if (NULL != host_mac)
210  clib_memcpy (&mp->host_mac[0], host_mac, 6);
211 
212  mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
213 
214  vl_msg_api_send_shmem (q, (u8 *) & mp);
215 }
216 
219 {
222  int rv = 0;
223 
225 
226  rv = dhcp_client_config (vm, ntohl (mp->sw_if_index),
227  mp->hostname, mp->is_add, mp->client_index,
229  NULL, mp->pid);
230 
232 
233  REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
234 }
235 
236 /*
237  * dhcp_api_hookup
238  * Add vpe's API message handlers to the table.
239  * vlib has alread mapped shared memory and
240  * added the client registration handlers.
241  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
242  */
243 #define vl_msg_name_crc_list
244 #include <vnet/vnet_all_api_h.h>
245 #undef vl_msg_name_crc_list
246 
247 static void
249 {
250 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
251  foreach_vl_msg_name_crc_dhcp;
252 #undef _
253 }
254 
255 static clib_error_t *
257 {
258  api_main_t *am = &api_main;
259 
260 #define _(N,n) \
261  vl_msg_api_set_handlers(VL_API_##N, #n, \
262  vl_api_##n##_t_handler, \
263  vl_noop_handler, \
264  vl_api_##n##_t_endian, \
265  vl_api_##n##_t_print, \
266  sizeof(vl_api_##n##_t), 1);
268 #undef _
269 
270  /*
271  * Set up the (msg_name, crc, message-id) table
272  */
274 
275  return 0;
276 }
277 
279 
280 /*
281  * fd.io coding-style-patch-verification: ON
282  *
283  * Local Variables:
284  * eval: (c-set-style "gnu")
285  * End:
286  */
DHCP Proxy set / unset vss request.
Definition: dhcp.api:58
#define vec_foreach_index(var, v)
Iterate over vector indices.
static dhcp_vss_t * dhcp_get_vss_info(dhcp_proxy_main_t *dm, u32 rx_fib_index, fib_protocol_t proto)
Get the VSS data for the FIB index.
Definition: dhcp_proxy.h:237
DHCP Client config response.
Definition: dhcp.api:104
static clib_error_t * dhcp_api_hookup(vlib_main_t *vm)
Definition: dhcp_api.c:256
void vl_msg_api_send_shmem(unix_shared_memory_queue_t *q, u8 *elem)
dhcp_server_t * dhcp_servers
The set of DHCP servers to which messages are relayed.
Definition: dhcp_proxy.h:90
#define NULL
Definition: clib.h:55
dhcp_proxy_main_t dhcp_proxy_main
Shard 4/6 instance of DHCP main.
Definition: dhcp_proxy.c:25
int dhcp6_proxy_set_server(ip46_address_t *addr, ip46_address_t *src_addr, u32 rx_table_id, u32 server_table_id, int is_del)
ip46_address_t dhcp_src_address
The source address to use in relayed messaes.
Definition: dhcp_proxy.h:105
The Virtual Sub-net Selection information for a given RX FIB.
Definition: dhcp_proxy.h:49
unix_shared_memory_queue_t * vl_api_client_index_to_input_queue(u32 index)
void dhcp_send_details(fib_protocol_t proto, void *opaque, u32 context, dhcp_proxy_t *proxy)
Send the details of a proxy session to the API client during a dump.
Definition: dhcp_api.c:122
u32 rx_fib_index
The FIB index (not the external Table-ID) in which the client is resides.
Definition: dhcp_proxy.h:111
u32 oui
?? RFC doesn&#39;t say
Definition: dhcp_proxy.h:53
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
void dhcp_proxy_dump(fib_protocol_t proto, void *opaque, u32 context)
Dump the proxy configs to the API.
Definition: dhcp_proxy.c:261
api_main_t api_main
Definition: api_shared.c:35
vl_api_dhcp_server_t servers[count]
Definition: dhcp.api:158
static void vl_api_dhcp_proxy_config_t_handler(vl_api_dhcp_proxy_config_t *mp)
Definition: dhcp_api.c:72
#define foreach_vpe_api_msg
Definition: dhcp_api.c:47
u32 server_fib_index
The FIB index (not the external Table-ID) in which the server is reachable.
Definition: dhcp_proxy.h:75
#define REPLY_MACRO(t)
static void vl_api_dhcp_proxy_dump_t_handler(vl_api_dhcp_proxy_dump_t *mp)
Definition: dhcp_api.c:109
u32 fib_id
VPN-ID.
Definition: dhcp_proxy.h:57
#define BAD_SW_IF_INDEX_LABEL
void * vl_msg_api_alloc(int nbytes)
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:56
vlib_main_t * vm
Definition: buffer.c:276
#define clib_memcpy(a, b, c)
Definition: string.h:69
A representation of a single DHCP Server within a given VRF config.
Definition: dhcp_proxy.h:63
int dhcp4_proxy_set_server(ip46_address_t *addr, ip46_address_t *src_addr, u32 rx_table_id, u32 server_table_id, int is_del)
DHCP proxy set / unset vss response.
Definition: dhcp.api:73
ip46_address_t dhcp_server
The address of the DHCP server to which to relay the client&#39;s messages.
Definition: dhcp_proxy.h:69
unsigned int u32
Definition: types.h:88
VLIB_API_INIT_FUNCTION(dhcp_api_hookup)
int dhcp_client_config(vlib_main_t *vm, u32 sw_if_index, u8 *hostname, u32 is_add, u32 client_index, void *event_callback, u32 pid)
Definition: client.c:836
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u8 dhcp_server[16]
Definition: dhcp.api:143
int dhcp_proxy_set_vss(fib_protocol_t proto, u32 tbl_id, u32 oui, u32 fib_id, int is_del)
Configure/set a new VSS info.
Definition: dhcp_proxy.c:288
Tell client about a DHCP completion event.
Definition: dhcp.api:149
#define ip46_address_reset(ip46)
Definition: ip6_packet.h:79
A DHCP proxy represenation fpr per-client VRF config.
Definition: dhcp_proxy.h:81
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
static void setup_message_id_table(api_main_t *am)
Definition: dhcp_api.c:248
static void vl_api_dhcp_client_config_t_handler(vl_api_dhcp_client_config_t *mp)
Definition: dhcp_api.c:218
fib_table_t * fib_table_get(fib_node_index_t index, fib_protocol_t proto)
Get a pointer to a FIB table.
Definition: fib_table.c:27
static void vl_api_dhcp_proxy_set_vss_t_handler(vl_api_dhcp_proxy_set_vss_t *mp)
Definition: dhcp_api.c:55
Dump DHCP proxy table.
Definition: dhcp.api:133
u32 dhcp_proxy_rx_table_get_table_id(fib_protocol_t proto, u32 fib_index)
Definition: dhcp_proxy.c:48
void dhcp_compl_event_callback(u32 client_index, u32 pid, u8 *hostname, u8 is_ipv6, u8 *host_address, u8 *router_address, u8 *host_mac)
Definition: dhcp_api.c:189
Tell client about a DHCP completion event.
Definition: dhcp.api:118
DHCP Client config add / del request.
Definition: dhcp.api:89
DHCP Proxy config add / del request.
Definition: dhcp.api:27
#define VALIDATE_SW_IF_INDEX(mp)
A protocol Independent FIB table.
Definition: fib_table.h:29
struct _unix_shared_memory_queue unix_shared_memory_queue_t