]>
Commit | Line | Data |
---|---|---|
244c1cdc DS |
1 | /* Kernel routing table updates using netlink over GNU/Linux system. |
2 | * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
244c1cdc DS |
19 | */ |
20 | ||
21 | #include <zebra.h> | |
43e52561 | 22 | |
d62a17ae | 23 | #include <sys/un.h> /* for sockaddr_un */ |
244c1cdc | 24 | #include <net/if.h> |
43e52561 QY |
25 | |
26 | #include "bfd.h" | |
27 | #include "buffer.h" | |
28 | #include "command.h" | |
29 | #include "if.h" | |
30 | #include "network.h" | |
31 | #include "ptm_lib.h" | |
32 | #include "rib.h" | |
33 | #include "stream.h" | |
34 | #include "version.h" | |
35 | #include "vrf.h" | |
c8ed14dd | 36 | #include "vty.h" |
43e52561 | 37 | |
244c1cdc | 38 | #include "zebra/debug.h" |
43e52561 QY |
39 | #include "zebra/interface.h" |
40 | #include "zebra/zebra_errors.h" | |
244c1cdc | 41 | #include "zebra/zebra_ptm.h" |
c43ed2e4 | 42 | #include "zebra/zebra_ptm_redistribute.h" |
43e52561 | 43 | #include "zebra/zserv.h" |
7c551956 | 44 | #include "zebra_vrf.h" |
244c1cdc | 45 | |
d3af6147 RZ |
46 | /* |
47 | * Choose the BFD implementation that we'll use. | |
48 | * | |
49 | * There are two implementations: | |
50 | * - PTM BFD: which uses an external daemon; | |
51 | * - bfdd: FRR's own BFD daemon; | |
52 | */ | |
53 | #if HAVE_BFDD == 0 | |
54 | ||
244c1cdc DS |
55 | #define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */ |
56 | #define ZEBRA_PTM_RECONNECT_TIME_MAX 300 | |
c01acf98 | 57 | |
c01acf98 | 58 | #define PTM_MSG_LEN 4 |
0a56c844 | 59 | #define PTM_HEADER_LEN 37 |
c43ed2e4 DS |
60 | |
61 | const char ZEBRA_PTM_GET_STATUS_CMD[] = "get-status"; | |
62 | const char ZEBRA_PTM_BFD_START_CMD[] = "start-bfd-sess"; | |
63 | const char ZEBRA_PTM_BFD_STOP_CMD[] = "stop-bfd-sess"; | |
055c4dfc | 64 | const char ZEBRA_PTM_BFD_CLIENT_REG_CMD[] = "reg-bfd-client"; |
567b877d | 65 | const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD[] = "dereg-bfd-client"; |
c43ed2e4 | 66 | |
c8ed14dd | 67 | const char ZEBRA_PTM_CMD_STR[] = "cmd"; |
b255a4b1 | 68 | const char ZEBRA_PTM_CMD_STATUS_STR[] = "cmd_status"; |
c43ed2e4 DS |
69 | const char ZEBRA_PTM_PORT_STR[] = "port"; |
70 | const char ZEBRA_PTM_CBL_STR[] = "cbl status"; | |
71 | const char ZEBRA_PTM_PASS_STR[] = "pass"; | |
72 | const char ZEBRA_PTM_FAIL_STR[] = "fail"; | |
73 | const char ZEBRA_PTM_BFDSTATUS_STR[] = "state"; | |
74 | const char ZEBRA_PTM_BFDSTATUS_UP_STR[] = "Up"; | |
75 | const char ZEBRA_PTM_BFDSTATUS_DOWN_STR[] = "Down"; | |
76 | const char ZEBRA_PTM_BFDDEST_STR[] = "peer"; | |
77 | const char ZEBRA_PTM_BFDSRC_STR[] = "local"; | |
d553294e | 78 | const char ZEBRA_PTM_BFDVRF_STR[] = "vrf"; |
c43ed2e4 DS |
79 | const char ZEBRA_PTM_INVALID_PORT_NAME[] = "N/A"; |
80 | const char ZEBRA_PTM_INVALID_SRC_IP[] = "N/A"; | |
d553294e | 81 | const char ZEBRA_PTM_INVALID_VRF[] = "N/A"; |
c43ed2e4 DS |
82 | |
83 | const char ZEBRA_PTM_BFD_DST_IP_FIELD[] = "dstIPaddr"; | |
84 | const char ZEBRA_PTM_BFD_SRC_IP_FIELD[] = "srcIPaddr"; | |
85 | const char ZEBRA_PTM_BFD_MIN_RX_FIELD[] = "requiredMinRx"; | |
86 | const char ZEBRA_PTM_BFD_MIN_TX_FIELD[] = "upMinTx"; | |
87 | const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD[] = "detectMult"; | |
88 | const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD[] = "multiHop"; | |
89 | const char ZEBRA_PTM_BFD_CLIENT_FIELD[] = "client"; | |
90 | const char ZEBRA_PTM_BFD_SEQID_FIELD[] = "seqid"; | |
91 | const char ZEBRA_PTM_BFD_IFNAME_FIELD[] = "ifName"; | |
92 | const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD[] = "maxHopCnt"; | |
68fe91d6 | 93 | const char ZEBRA_PTM_BFD_SEND_EVENT[] = "sendEvent"; |
d553294e | 94 | const char ZEBRA_PTM_BFD_VRF_NAME_FIELD[] = "vrfName"; |
c01acf98 | 95 | |
c43ed2e4 | 96 | static ptm_lib_handle_t *ptm_hdl; |
244c1cdc | 97 | |
c8ed14dd DS |
98 | struct zebra_ptm_cb ptm_cb; |
99 | ||
244c1cdc | 100 | static int zebra_ptm_socket_init(void); |
244c1cdc | 101 | int zebra_ptm_sock_read(struct thread *); |
d62a17ae | 102 | static void zebra_ptm_install_commands(void); |
b255a4b1 | 103 | static int zebra_ptm_handle_msg_cb(void *arg, void *in_ctxt); |
d62a17ae | 104 | void zebra_bfd_peer_replay_req(void); |
950bd436 | 105 | void zebra_ptm_send_status_req(void); |
106 | void zebra_ptm_reset_status(int ptm_disable); | |
8b1766b1 | 107 | static int zebra_ptm_bfd_client_deregister(struct zserv *client); |
244c1cdc DS |
108 | |
109 | const char ZEBRA_PTM_SOCK_NAME[] = "\0/var/run/ptmd.socket"; | |
110 | ||
d62a17ae | 111 | void zebra_ptm_init(void) |
244c1cdc | 112 | { |
d62a17ae | 113 | char buf[64]; |
c43ed2e4 | 114 | |
d62a17ae | 115 | memset(&ptm_cb, 0, sizeof(struct zebra_ptm_cb)); |
c8ed14dd | 116 | |
d62a17ae | 117 | ptm_cb.out_data = calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF); |
118 | if (!ptm_cb.out_data) { | |
119 | zlog_warn("%s: Allocation of send data failed", __func__); | |
120 | return; | |
121 | } | |
c8ed14dd | 122 | |
d62a17ae | 123 | ptm_cb.in_data = calloc(1, ZEBRA_PTM_MAX_SOCKBUF); |
124 | if (!ptm_cb.in_data) { | |
125 | zlog_warn("%s: Allocation of recv data failed", __func__); | |
126 | free(ptm_cb.out_data); | |
127 | return; | |
128 | } | |
c8ed14dd | 129 | |
d62a17ae | 130 | ptm_cb.pid = getpid(); |
131 | zebra_ptm_install_commands(); | |
c43ed2e4 | 132 | |
d62a17ae | 133 | sprintf(buf, "%s", FRR_PTM_NAME); |
134 | ptm_hdl = ptm_lib_register(buf, NULL, zebra_ptm_handle_msg_cb, | |
135 | zebra_ptm_handle_msg_cb); | |
136 | ptm_cb.wb = buffer_new(0); | |
c8ed14dd | 137 | |
d62a17ae | 138 | ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL; |
c8ed14dd | 139 | |
d62a17ae | 140 | ptm_cb.ptm_sock = -1; |
453844ab | 141 | |
21ccc0cf | 142 | hook_register(zserv_client_close, zebra_ptm_bfd_client_deregister); |
c8ed14dd DS |
143 | } |
144 | ||
d62a17ae | 145 | void zebra_ptm_finish(void) |
c8ed14dd | 146 | { |
d62a17ae | 147 | buffer_flush_all(ptm_cb.wb, ptm_cb.ptm_sock); |
c8ed14dd | 148 | |
d62a17ae | 149 | free(ptm_hdl); |
58ac32e2 | 150 | |
d62a17ae | 151 | if (ptm_cb.out_data) |
152 | free(ptm_cb.out_data); | |
c8ed14dd | 153 | |
d62a17ae | 154 | if (ptm_cb.in_data) |
155 | free(ptm_cb.in_data); | |
c8ed14dd | 156 | |
d62a17ae | 157 | /* Release threads. */ |
158 | if (ptm_cb.t_read) | |
159 | thread_cancel(ptm_cb.t_read); | |
160 | if (ptm_cb.t_write) | |
161 | thread_cancel(ptm_cb.t_write); | |
162 | if (ptm_cb.t_timer) | |
163 | thread_cancel(ptm_cb.t_timer); | |
2376c3f2 | 164 | |
d62a17ae | 165 | if (ptm_cb.wb) |
166 | buffer_free(ptm_cb.wb); | |
2376c3f2 | 167 | |
1e9f448f | 168 | if (ptm_cb.ptm_sock >= 0) |
d62a17ae | 169 | close(ptm_cb.ptm_sock); |
c8ed14dd DS |
170 | } |
171 | ||
d62a17ae | 172 | static int zebra_ptm_flush_messages(struct thread *thread) |
c8ed14dd | 173 | { |
d62a17ae | 174 | ptm_cb.t_write = NULL; |
175 | ||
176 | if (ptm_cb.ptm_sock == -1) | |
177 | return -1; | |
178 | ||
179 | errno = 0; | |
180 | ||
181 | switch (buffer_flush_available(ptm_cb.wb, ptm_cb.ptm_sock)) { | |
182 | case BUFFER_ERROR: | |
183 | zlog_warn("%s ptm socket error: %s", __func__, | |
184 | safe_strerror(errno)); | |
185 | close(ptm_cb.ptm_sock); | |
186 | ptm_cb.ptm_sock = -1; | |
187 | zebra_ptm_reset_status(0); | |
188 | ptm_cb.t_timer = NULL; | |
189 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
190 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
191 | return (-1); | |
192 | case BUFFER_PENDING: | |
193 | ptm_cb.t_write = NULL; | |
194 | thread_add_write(zebrad.master, zebra_ptm_flush_messages, NULL, | |
195 | ptm_cb.ptm_sock, &ptm_cb.t_write); | |
196 | break; | |
197 | case BUFFER_EMPTY: | |
198 | break; | |
199 | } | |
200 | ||
201 | return (0); | |
c8ed14dd DS |
202 | } |
203 | ||
d62a17ae | 204 | static int zebra_ptm_send_message(char *data, int size) |
c8ed14dd | 205 | { |
d62a17ae | 206 | errno = 0; |
207 | switch (buffer_write(ptm_cb.wb, ptm_cb.ptm_sock, data, size)) { | |
208 | case BUFFER_ERROR: | |
209 | zlog_warn("%s ptm socket error: %s", __func__, | |
210 | safe_strerror(errno)); | |
211 | close(ptm_cb.ptm_sock); | |
212 | ptm_cb.ptm_sock = -1; | |
213 | zebra_ptm_reset_status(0); | |
214 | ptm_cb.t_timer = NULL; | |
215 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
216 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
217 | return -1; | |
218 | case BUFFER_EMPTY: | |
219 | THREAD_OFF(ptm_cb.t_write); | |
220 | break; | |
221 | case BUFFER_PENDING: | |
222 | thread_add_write(zebrad.master, zebra_ptm_flush_messages, NULL, | |
223 | ptm_cb.ptm_sock, &ptm_cb.t_write); | |
224 | break; | |
225 | } | |
226 | ||
227 | return 0; | |
244c1cdc DS |
228 | } |
229 | ||
d62a17ae | 230 | int zebra_ptm_connect(struct thread *t) |
244c1cdc | 231 | { |
d62a17ae | 232 | int init = 0; |
233 | ||
234 | if (ptm_cb.ptm_sock == -1) { | |
235 | zebra_ptm_socket_init(); | |
236 | init = 1; | |
237 | } | |
238 | ||
239 | if (ptm_cb.ptm_sock != -1) { | |
240 | if (init) { | |
241 | ptm_cb.t_read = NULL; | |
242 | thread_add_read(zebrad.master, zebra_ptm_sock_read, | |
243 | NULL, ptm_cb.ptm_sock, &ptm_cb.t_read); | |
244 | zebra_bfd_peer_replay_req(); | |
245 | } | |
246 | zebra_ptm_send_status_req(); | |
247 | ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL; | |
248 | } else if (ptm_cb.reconnect_time < ZEBRA_PTM_RECONNECT_TIME_MAX) { | |
249 | ptm_cb.reconnect_time *= 2; | |
250 | if (ptm_cb.reconnect_time > ZEBRA_PTM_RECONNECT_TIME_MAX) | |
251 | ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_MAX; | |
252 | ||
253 | ptm_cb.t_timer = NULL; | |
254 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
255 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
256 | } else if (ptm_cb.reconnect_time >= ZEBRA_PTM_RECONNECT_TIME_MAX) { | |
257 | ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL; | |
258 | } | |
259 | ||
260 | return (errno); | |
244c1cdc DS |
261 | } |
262 | ||
244c1cdc DS |
263 | DEFUN (zebra_ptm_enable, |
264 | zebra_ptm_enable_cmd, | |
265 | "ptm-enable", | |
266 | "Enable neighbor check with specified topology\n") | |
267 | { | |
d62a17ae | 268 | struct vrf *vrf; |
d62a17ae | 269 | struct interface *ifp; |
270 | struct zebra_if *if_data; | |
271 | ||
272 | ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_ON; | |
273 | ||
a2addae8 | 274 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) |
451fda4f | 275 | FOR_ALL_INTERFACES (vrf, ifp) |
a2addae8 RW |
276 | if (!ifp->ptm_enable) { |
277 | if_data = (struct zebra_if *)ifp->info; | |
278 | if (if_data | |
279 | && (if_data->ptm_enable | |
9d303b37 | 280 | == ZEBRA_IF_PTM_ENABLE_UNSPEC)) { |
a2addae8 RW |
281 | ifp->ptm_enable = |
282 | ZEBRA_IF_PTM_ENABLE_ON; | |
283 | } | |
284 | /* Assign a default unknown status */ | |
285 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; | |
d62a17ae | 286 | } |
d62a17ae | 287 | |
288 | zebra_ptm_connect(NULL); | |
289 | ||
290 | return CMD_SUCCESS; | |
244c1cdc DS |
291 | } |
292 | ||
293 | DEFUN (no_zebra_ptm_enable, | |
294 | no_zebra_ptm_enable_cmd, | |
295 | "no ptm-enable", | |
296 | NO_STR | |
297 | "Enable neighbor check with specified topology\n") | |
298 | { | |
d62a17ae | 299 | ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF; |
300 | zebra_ptm_reset_status(1); | |
301 | return CMD_SUCCESS; | |
244c1cdc DS |
302 | } |
303 | ||
986aa00f | 304 | DEFUN (zebra_ptm_enable_if, |
305 | zebra_ptm_enable_if_cmd, | |
306 | "ptm-enable", | |
307 | "Enable neighbor check with specified topology\n") | |
308 | { | |
d62a17ae | 309 | VTY_DECLVAR_CONTEXT(interface, ifp); |
310 | struct zebra_if *if_data; | |
311 | int old_ptm_enable; | |
312 | int send_linkdown = 0; | |
313 | ||
a2023fab | 314 | if_data = ifp->info; |
315 | if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC; | |
316 | ||
d62a17ae | 317 | if (ifp->ifindex == IFINDEX_INTERNAL) { |
318 | return CMD_SUCCESS; | |
319 | } | |
320 | ||
321 | old_ptm_enable = ifp->ptm_enable; | |
322 | ifp->ptm_enable = ptm_cb.ptm_enable; | |
323 | ||
324 | if (if_is_no_ptm_operative(ifp)) | |
325 | send_linkdown = 1; | |
326 | ||
327 | if (!old_ptm_enable && ptm_cb.ptm_enable) { | |
328 | if (!if_is_operative(ifp) && send_linkdown) { | |
329 | if (IS_ZEBRA_DEBUG_EVENT) | |
330 | zlog_debug("%s: Bringing down interface %s\n", | |
331 | __func__, ifp->name); | |
332 | if_down(ifp); | |
333 | } | |
334 | } | |
335 | ||
d62a17ae | 336 | return CMD_SUCCESS; |
986aa00f | 337 | } |
338 | ||
339 | DEFUN (no_zebra_ptm_enable_if, | |
340 | no_zebra_ptm_enable_if_cmd, | |
341 | "no ptm-enable", | |
342 | NO_STR | |
343 | "Enable neighbor check with specified topology\n") | |
344 | { | |
d62a17ae | 345 | VTY_DECLVAR_CONTEXT(interface, ifp); |
346 | int send_linkup = 0; | |
347 | struct zebra_if *if_data; | |
348 | ||
349 | if ((ifp->ifindex != IFINDEX_INTERNAL) && (ifp->ptm_enable)) { | |
350 | if (!if_is_operative(ifp)) | |
351 | send_linkup = 1; | |
352 | ||
353 | ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF; | |
354 | if (if_is_no_ptm_operative(ifp) && send_linkup) { | |
355 | if (IS_ZEBRA_DEBUG_EVENT) | |
356 | zlog_debug("%s: Bringing up interface %s\n", | |
357 | __func__, ifp->name); | |
358 | if_up(ifp); | |
359 | } | |
360 | } | |
361 | ||
362 | if_data = ifp->info; | |
363 | if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF; | |
364 | ||
365 | return CMD_SUCCESS; | |
986aa00f | 366 | } |
367 | ||
368 | ||
d62a17ae | 369 | void zebra_ptm_write(struct vty *vty) |
244c1cdc | 370 | { |
d62a17ae | 371 | if (ptm_cb.ptm_enable) |
372 | vty_out(vty, "ptm-enable\n"); | |
244c1cdc | 373 | |
d62a17ae | 374 | return; |
244c1cdc DS |
375 | } |
376 | ||
d62a17ae | 377 | static int zebra_ptm_socket_init(void) |
244c1cdc | 378 | { |
d62a17ae | 379 | int ret; |
380 | int sock; | |
381 | struct sockaddr_un addr; | |
382 | ||
383 | ptm_cb.ptm_sock = -1; | |
384 | ||
385 | sock = socket(PF_UNIX, SOCK_STREAM, 0); | |
386 | if (sock < 0) | |
387 | return -1; | |
388 | if (set_nonblocking(sock) < 0) { | |
389 | if (IS_ZEBRA_DEBUG_EVENT) | |
390 | zlog_debug("%s: Unable to set socket non blocking[%s]", | |
391 | __PRETTY_FUNCTION__, safe_strerror(errno)); | |
392 | close(sock); | |
393 | return -1; | |
394 | } | |
395 | ||
396 | /* Make server socket. */ | |
397 | memset(&addr, 0, sizeof(struct sockaddr_un)); | |
398 | addr.sun_family = AF_UNIX; | |
399 | memcpy(&addr.sun_path, ZEBRA_PTM_SOCK_NAME, | |
400 | sizeof(ZEBRA_PTM_SOCK_NAME)); | |
401 | ||
402 | ret = connect(sock, (struct sockaddr *)&addr, | |
403 | sizeof(addr.sun_family) + sizeof(ZEBRA_PTM_SOCK_NAME) | |
404 | - 1); | |
405 | if (ret < 0) { | |
406 | if (IS_ZEBRA_DEBUG_EVENT) | |
407 | zlog_debug("%s: Unable to connect to socket %s [%s]", | |
408 | __func__, ZEBRA_PTM_SOCK_NAME, | |
409 | safe_strerror(errno)); | |
410 | close(sock); | |
411 | return -1; | |
412 | } | |
413 | ptm_cb.ptm_sock = sock; | |
414 | return sock; | |
244c1cdc DS |
415 | } |
416 | ||
d62a17ae | 417 | static void zebra_ptm_install_commands(void) |
244c1cdc | 418 | { |
d62a17ae | 419 | install_element(CONFIG_NODE, &zebra_ptm_enable_cmd); |
420 | install_element(CONFIG_NODE, &no_zebra_ptm_enable_cmd); | |
421 | install_element(INTERFACE_NODE, &zebra_ptm_enable_if_cmd); | |
422 | install_element(INTERFACE_NODE, &no_zebra_ptm_enable_if_cmd); | |
244c1cdc DS |
423 | } |
424 | ||
c43ed2e4 | 425 | /* BFD session goes down, send message to the protocols. */ |
d62a17ae | 426 | static void if_bfd_session_update(struct interface *ifp, struct prefix *dp, |
427 | struct prefix *sp, int status, | |
428 | vrf_id_t vrf_id) | |
c01acf98 | 429 | { |
d62a17ae | 430 | if (IS_ZEBRA_DEBUG_EVENT) { |
431 | char buf[2][INET6_ADDRSTRLEN]; | |
432 | ||
433 | if (ifp) { | |
434 | zlog_debug( | |
435 | "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s" | |
436 | " %s event", | |
437 | inet_ntop(dp->family, &dp->u.prefix, buf[0], | |
438 | INET6_ADDRSTRLEN), | |
439 | dp->prefixlen, ifp->name, | |
440 | bfd_get_status_str(status)); | |
441 | } else { | |
442 | zlog_debug( | |
443 | "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d " | |
a9ff90c4 | 444 | "with src %s/%d and vrf %u %s event", |
d62a17ae | 445 | inet_ntop(dp->family, &dp->u.prefix, buf[0], |
446 | INET6_ADDRSTRLEN), | |
447 | dp->prefixlen, | |
448 | inet_ntop(sp->family, &sp->u.prefix, buf[1], | |
449 | INET6_ADDRSTRLEN), | |
450 | sp->prefixlen, vrf_id, | |
451 | bfd_get_status_str(status)); | |
452 | } | |
453 | } | |
454 | ||
455 | zebra_interface_bfd_update(ifp, dp, sp, status, vrf_id); | |
c01acf98 DS |
456 | } |
457 | ||
d62a17ae | 458 | static int zebra_ptm_handle_bfd_msg(void *arg, void *in_ctxt, |
459 | struct interface *ifp) | |
244c1cdc | 460 | { |
d62a17ae | 461 | char bfdst_str[32]; |
462 | char dest_str[64]; | |
463 | char src_str[64]; | |
464 | char vrf_str[64]; | |
465 | struct prefix dest_prefix; | |
466 | struct prefix src_prefix; | |
467 | vrf_id_t vrf_id; | |
468 | ||
469 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSTATUS_STR, bfdst_str); | |
470 | ||
471 | if (bfdst_str[0] == '\0') { | |
472 | return -1; | |
473 | } | |
474 | ||
475 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDDEST_STR, dest_str); | |
476 | ||
477 | if (dest_str[0] == '\0') { | |
478 | zlog_debug("%s: Key %s not found in PTM msg", __func__, | |
479 | ZEBRA_PTM_BFDDEST_STR); | |
480 | return -1; | |
481 | } | |
482 | ||
483 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSRC_STR, src_str); | |
484 | ||
485 | if (src_str[0] == '\0') { | |
486 | zlog_debug("%s: Key %s not found in PTM msg", __func__, | |
487 | ZEBRA_PTM_BFDSRC_STR); | |
488 | return -1; | |
489 | } | |
490 | ||
491 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDVRF_STR, vrf_str); | |
492 | ||
493 | if (vrf_str[0] == '\0') { | |
494 | zlog_debug("%s: Key %s not found in PTM msg", __func__, | |
495 | ZEBRA_PTM_BFDVRF_STR); | |
496 | return -1; | |
497 | } | |
498 | ||
499 | if (IS_ZEBRA_DEBUG_EVENT) | |
500 | zlog_debug( | |
501 | "%s: Recv Port [%s] bfd status [%s] vrf [%s]" | |
502 | " peer [%s] local [%s]", | |
503 | __func__, ifp ? ifp->name : "N/A", bfdst_str, vrf_str, | |
504 | dest_str, src_str); | |
505 | ||
506 | if (str2prefix(dest_str, &dest_prefix) == 0) { | |
af4c2728 | 507 | flog_err(ZEBRA_ERR_PREFIX_PARSE_ERROR, |
43e52561 | 508 | "%s: Peer addr %s not found", __func__, dest_str); |
d62a17ae | 509 | return -1; |
510 | } | |
511 | ||
512 | memset(&src_prefix, 0, sizeof(struct prefix)); | |
513 | if (strcmp(ZEBRA_PTM_INVALID_SRC_IP, src_str)) { | |
514 | if (str2prefix(src_str, &src_prefix) == 0) { | |
af4c2728 | 515 | flog_err(ZEBRA_ERR_PREFIX_PARSE_ERROR, |
43e52561 QY |
516 | "%s: Local addr %s not found", __func__, |
517 | src_str); | |
d62a17ae | 518 | return -1; |
519 | } | |
520 | } | |
521 | ||
522 | if (!strcmp(ZEBRA_PTM_INVALID_VRF, vrf_str) && ifp) { | |
523 | vrf_id = ifp->vrf_id; | |
524 | } else { | |
525 | vrf_id = vrf_name_to_id(vrf_str); | |
526 | } | |
527 | ||
528 | if (!strcmp(bfdst_str, ZEBRA_PTM_BFDSTATUS_DOWN_STR)) { | |
529 | if_bfd_session_update(ifp, &dest_prefix, &src_prefix, | |
530 | BFD_STATUS_DOWN, vrf_id); | |
531 | } else { | |
532 | if_bfd_session_update(ifp, &dest_prefix, &src_prefix, | |
533 | BFD_STATUS_UP, vrf_id); | |
534 | } | |
535 | ||
536 | return 0; | |
c01acf98 DS |
537 | } |
538 | ||
d62a17ae | 539 | static int zebra_ptm_handle_cbl_msg(void *arg, void *in_ctxt, |
540 | struct interface *ifp, char *cbl_str) | |
c01acf98 | 541 | { |
d62a17ae | 542 | int send_linkup = 0; |
543 | ||
544 | if (IS_ZEBRA_DEBUG_EVENT) | |
545 | zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__, | |
546 | ifp->name, cbl_str); | |
547 | ||
548 | if (!strcmp(cbl_str, ZEBRA_PTM_PASS_STR) | |
549 | && (ifp->ptm_status != ZEBRA_PTM_STATUS_UP)) { | |
550 | ||
551 | if (ifp->ptm_status == ZEBRA_PTM_STATUS_DOWN) | |
552 | send_linkup = 1; | |
553 | ifp->ptm_status = ZEBRA_PTM_STATUS_UP; | |
554 | if (ifp->ptm_enable && if_is_no_ptm_operative(ifp) | |
555 | && send_linkup) | |
556 | if_up(ifp); | |
557 | } else if (!strcmp(cbl_str, ZEBRA_PTM_FAIL_STR) | |
558 | && (ifp->ptm_status != ZEBRA_PTM_STATUS_DOWN)) { | |
559 | ifp->ptm_status = ZEBRA_PTM_STATUS_DOWN; | |
560 | if (ifp->ptm_enable && if_is_no_ptm_operative(ifp)) | |
561 | if_down(ifp); | |
562 | } | |
563 | ||
564 | return 0; | |
b255a4b1 | 565 | } |
950bd436 | 566 | |
b255a4b1 | 567 | /* |
568 | * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle | |
569 | * all the command responses and notifications received from PTM. | |
570 | * | |
571 | * Command responses: Upon establishing connection with PTM, Zebra requests | |
572 | * status of all interfaces using 'get-status' command if global ptm-enable | |
573 | * knob is enabled. As a response to the get-status command PTM sends status | |
574 | * of all the interfaces as command responses. All other type of command | |
575 | * responses with cmd_status key word are dropped. The sole purpose of | |
576 | * registering this function as callback for the command responses is to | |
577 | * handle the responses to get-status command. | |
578 | * | |
579 | * Notifications: Cable status and BFD session status changes are sent as | |
580 | * notifications by PTM. So, this function is also the callback function for | |
581 | * processing all the notifications from the PTM. | |
582 | * | |
583 | */ | |
d62a17ae | 584 | static int zebra_ptm_handle_msg_cb(void *arg, void *in_ctxt) |
b255a4b1 | 585 | { |
d62a17ae | 586 | struct interface *ifp = NULL; |
587 | char port_str[128]; | |
588 | char cbl_str[32]; | |
589 | char cmd_status_str[32]; | |
590 | ||
591 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CMD_STATUS_STR, | |
592 | cmd_status_str); | |
593 | ||
594 | /* Drop command response messages */ | |
595 | if (cmd_status_str[0] != '\0') { | |
596 | return 0; | |
597 | } | |
598 | ||
599 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_PORT_STR, port_str); | |
600 | ||
601 | if (port_str[0] == '\0') { | |
602 | zlog_debug("%s: Key %s not found in PTM msg", __func__, | |
603 | ZEBRA_PTM_PORT_STR); | |
604 | return -1; | |
605 | } | |
606 | ||
607 | if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME, port_str)) { | |
608 | ifp = if_lookup_by_name_all_vrf(port_str); | |
609 | ||
610 | if (!ifp) { | |
43e52561 QY |
611 | zlog_warn("%s: %s not found in interface list", |
612 | __func__, port_str); | |
d62a17ae | 613 | return -1; |
614 | } | |
615 | } | |
616 | ||
617 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CBL_STR, cbl_str); | |
618 | ||
619 | if (cbl_str[0] == '\0') { | |
620 | return zebra_ptm_handle_bfd_msg(arg, in_ctxt, ifp); | |
621 | } else { | |
622 | if (ifp) { | |
623 | return zebra_ptm_handle_cbl_msg(arg, in_ctxt, ifp, | |
624 | cbl_str); | |
625 | } else { | |
626 | return -1; | |
627 | } | |
628 | } | |
c01acf98 DS |
629 | } |
630 | ||
d62a17ae | 631 | int zebra_ptm_sock_read(struct thread *thread) |
c01acf98 | 632 | { |
2e1cc436 | 633 | int sock; |
d62a17ae | 634 | int rc; |
635 | ||
636 | errno = 0; | |
637 | sock = THREAD_FD(thread); | |
638 | ||
639 | if (sock == -1) | |
640 | return -1; | |
641 | ||
642 | /* PTM communicates in CSV format */ | |
2e1cc436 | 643 | do { |
d62a17ae | 644 | rc = ptm_lib_process_msg(ptm_hdl, sock, ptm_cb.in_data, |
645 | ZEBRA_PTM_MAX_SOCKBUF, NULL); | |
2e1cc436 | 646 | } while (rc > 0); |
d62a17ae | 647 | |
2e1cc436 A |
648 | if (((rc == 0) && !errno) |
649 | || (errno && (errno != EWOULDBLOCK) && (errno != EAGAIN))) { | |
650 | zlog_warn("%s routing socket error: %s(%d) bytes %d", | |
651 | __func__, safe_strerror(errno), errno, rc); | |
652 | ||
653 | close(ptm_cb.ptm_sock); | |
654 | ptm_cb.ptm_sock = -1; | |
655 | zebra_ptm_reset_status(0); | |
656 | ptm_cb.t_timer = NULL; | |
657 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
658 | ptm_cb.reconnect_time, | |
659 | &ptm_cb.t_timer); | |
660 | return (-1); | |
d62a17ae | 661 | } |
662 | ||
663 | ptm_cb.t_read = NULL; | |
664 | thread_add_read(zebrad.master, zebra_ptm_sock_read, NULL, | |
665 | ptm_cb.ptm_sock, &ptm_cb.t_read); | |
666 | ||
667 | return 0; | |
c43ed2e4 DS |
668 | } |
669 | ||
670 | /* BFD peer/dst register/update */ | |
89f4e507 | 671 | void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS) |
c43ed2e4 | 672 | { |
d62a17ae | 673 | struct stream *s; |
674 | struct prefix src_p; | |
675 | struct prefix dst_p; | |
d7c0a89a QY |
676 | uint8_t multi_hop; |
677 | uint8_t multi_hop_cnt; | |
678 | uint8_t detect_mul; | |
d62a17ae | 679 | unsigned int min_rx_timer; |
680 | unsigned int min_tx_timer; | |
681 | char if_name[INTERFACE_NAMSIZ]; | |
d7c0a89a | 682 | uint8_t len; |
d62a17ae | 683 | void *out_ctxt; |
684 | char buf[INET6_ADDRSTRLEN]; | |
685 | char tmp_buf[64]; | |
686 | int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
687 | unsigned int pid; | |
688 | ||
89f4e507 | 689 | if (hdr->command == ZEBRA_BFD_DEST_UPDATE) |
d62a17ae | 690 | client->bfd_peer_upd8_cnt++; |
691 | else | |
692 | client->bfd_peer_add_cnt++; | |
693 | ||
694 | if (IS_ZEBRA_DEBUG_EVENT) | |
695 | zlog_debug("bfd_dst_register msg from client %s: length=%d", | |
89f4e507 | 696 | zebra_route_string(client->proto), hdr->length); |
d62a17ae | 697 | |
698 | if (ptm_cb.ptm_sock == -1) { | |
699 | ptm_cb.t_timer = NULL; | |
700 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
701 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
8068a649 | 702 | return; |
d62a17ae | 703 | } |
704 | ||
705 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt); | |
706 | sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_START_CMD); | |
707 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf); | |
708 | sprintf(tmp_buf, "%s", zebra_route_string(client->proto)); | |
709 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD, | |
710 | tmp_buf); | |
711 | ||
1002497a | 712 | s = msg; |
d62a17ae | 713 | |
ec93aa12 | 714 | STREAM_GETL(s, pid); |
d62a17ae | 715 | sprintf(tmp_buf, "%d", pid); |
716 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD, | |
717 | tmp_buf); | |
718 | ||
ec93aa12 | 719 | STREAM_GETW(s, dst_p.family); |
d62a17ae | 720 | |
721 | if (dst_p.family == AF_INET) | |
722 | dst_p.prefixlen = IPV4_MAX_BYTELEN; | |
723 | else | |
724 | dst_p.prefixlen = IPV6_MAX_BYTELEN; | |
725 | ||
ec93aa12 | 726 | STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen); |
d62a17ae | 727 | if (dst_p.family == AF_INET) { |
728 | inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf)); | |
729 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
730 | ZEBRA_PTM_BFD_DST_IP_FIELD, buf); | |
731 | } else { | |
732 | inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf)); | |
733 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
734 | ZEBRA_PTM_BFD_DST_IP_FIELD, buf); | |
735 | } | |
736 | ||
ec93aa12 | 737 | STREAM_GETL(s, min_rx_timer); |
d62a17ae | 738 | sprintf(tmp_buf, "%d", min_rx_timer); |
739 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_RX_FIELD, | |
740 | tmp_buf); | |
ec93aa12 | 741 | STREAM_GETL(s, min_tx_timer); |
d62a17ae | 742 | sprintf(tmp_buf, "%d", min_tx_timer); |
743 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_TX_FIELD, | |
744 | tmp_buf); | |
ec93aa12 | 745 | STREAM_GETC(s, detect_mul); |
d62a17ae | 746 | sprintf(tmp_buf, "%d", detect_mul); |
747 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DETECT_MULT_FIELD, | |
748 | tmp_buf); | |
749 | ||
ec93aa12 | 750 | STREAM_GETC(s, multi_hop); |
d62a17ae | 751 | if (multi_hop) { |
752 | sprintf(tmp_buf, "%d", 1); | |
753 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
754 | ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf); | |
ec93aa12 | 755 | STREAM_GETW(s, src_p.family); |
d62a17ae | 756 | |
757 | if (src_p.family == AF_INET) | |
758 | src_p.prefixlen = IPV4_MAX_BYTELEN; | |
759 | else | |
760 | src_p.prefixlen = IPV6_MAX_BYTELEN; | |
761 | ||
ec93aa12 | 762 | STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen); |
d62a17ae | 763 | if (src_p.family == AF_INET) { |
764 | inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf)); | |
765 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
766 | ZEBRA_PTM_BFD_SRC_IP_FIELD, buf); | |
767 | } else { | |
768 | inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf)); | |
769 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
770 | ZEBRA_PTM_BFD_SRC_IP_FIELD, buf); | |
771 | } | |
772 | ||
ec93aa12 | 773 | STREAM_GETC(s, multi_hop_cnt); |
d62a17ae | 774 | sprintf(tmp_buf, "%d", multi_hop_cnt); |
775 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
776 | ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD, tmp_buf); | |
777 | ||
778 | if (zvrf_id(zvrf) != VRF_DEFAULT) | |
779 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
780 | ZEBRA_PTM_BFD_VRF_NAME_FIELD, | |
781 | zvrf_name(zvrf)); | |
782 | } else { | |
783 | if (dst_p.family == AF_INET6) { | |
ec93aa12 | 784 | STREAM_GETW(s, src_p.family); |
d62a17ae | 785 | |
786 | if (src_p.family == AF_INET) | |
787 | src_p.prefixlen = IPV4_MAX_BYTELEN; | |
788 | else | |
789 | src_p.prefixlen = IPV6_MAX_BYTELEN; | |
790 | ||
ec93aa12 | 791 | STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen); |
d62a17ae | 792 | if (src_p.family == AF_INET) { |
793 | inet_ntop(AF_INET, &src_p.u.prefix4, buf, | |
794 | sizeof(buf)); | |
795 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
796 | ZEBRA_PTM_BFD_SRC_IP_FIELD, | |
797 | buf); | |
798 | } else { | |
799 | inet_ntop(AF_INET6, &src_p.u.prefix6, buf, | |
800 | sizeof(buf)); | |
801 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
802 | ZEBRA_PTM_BFD_SRC_IP_FIELD, | |
803 | buf); | |
804 | } | |
805 | } | |
ec93aa12 DS |
806 | STREAM_GETC(s, len); |
807 | STREAM_GET(if_name, s, len); | |
d62a17ae | 808 | if_name[len] = '\0'; |
809 | ||
810 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
811 | ZEBRA_PTM_BFD_IFNAME_FIELD, if_name); | |
812 | } | |
813 | ||
814 | sprintf(tmp_buf, "%d", 1); | |
815 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEND_EVENT, | |
816 | tmp_buf); | |
817 | ||
818 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len); | |
819 | ||
820 | if (IS_ZEBRA_DEBUG_SEND) | |
821 | zlog_debug("%s: Sent message (%d) %s", __func__, data_len, | |
822 | ptm_cb.out_data); | |
823 | zebra_ptm_send_message(ptm_cb.out_data, data_len); | |
ec93aa12 | 824 | |
8068a649 | 825 | return; |
9cc46248 | 826 | |
ec93aa12 | 827 | stream_failure: |
a928d464 | 828 | ptm_lib_cleanup_msg(ptm_hdl, out_ctxt); |
c43ed2e4 DS |
829 | } |
830 | ||
831 | /* BFD peer/dst deregister */ | |
89f4e507 | 832 | void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS) |
c43ed2e4 | 833 | { |
d62a17ae | 834 | struct stream *s; |
835 | struct prefix src_p; | |
836 | struct prefix dst_p; | |
d7c0a89a | 837 | uint8_t multi_hop; |
d62a17ae | 838 | char if_name[INTERFACE_NAMSIZ]; |
d7c0a89a | 839 | uint8_t len; |
d62a17ae | 840 | char buf[INET6_ADDRSTRLEN]; |
841 | char tmp_buf[64]; | |
842 | int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
843 | void *out_ctxt; | |
844 | unsigned int pid; | |
845 | ||
846 | client->bfd_peer_del_cnt++; | |
847 | ||
848 | if (IS_ZEBRA_DEBUG_EVENT) | |
849 | zlog_debug("bfd_dst_deregister msg from client %s: length=%d", | |
89f4e507 | 850 | zebra_route_string(client->proto), hdr->length); |
d62a17ae | 851 | |
852 | if (ptm_cb.ptm_sock == -1) { | |
853 | ptm_cb.t_timer = NULL; | |
854 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
855 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
8068a649 | 856 | return; |
d62a17ae | 857 | } |
858 | ||
859 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt); | |
860 | ||
861 | sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_STOP_CMD); | |
862 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf); | |
863 | ||
864 | sprintf(tmp_buf, "%s", zebra_route_string(client->proto)); | |
865 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD, | |
866 | tmp_buf); | |
867 | ||
1002497a | 868 | s = msg; |
d62a17ae | 869 | |
ec93aa12 | 870 | STREAM_GETL(s, pid); |
d62a17ae | 871 | sprintf(tmp_buf, "%d", pid); |
872 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD, | |
873 | tmp_buf); | |
874 | ||
ec93aa12 | 875 | STREAM_GETW(s, dst_p.family); |
d62a17ae | 876 | |
877 | if (dst_p.family == AF_INET) | |
878 | dst_p.prefixlen = IPV4_MAX_BYTELEN; | |
879 | else | |
880 | dst_p.prefixlen = IPV6_MAX_BYTELEN; | |
881 | ||
ec93aa12 | 882 | STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen); |
d62a17ae | 883 | if (dst_p.family == AF_INET) |
884 | inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf)); | |
885 | else | |
886 | inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf)); | |
887 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DST_IP_FIELD, buf); | |
888 | ||
889 | ||
ec93aa12 | 890 | STREAM_GETC(s, multi_hop); |
d62a17ae | 891 | if (multi_hop) { |
892 | sprintf(tmp_buf, "%d", 1); | |
893 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
894 | ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf); | |
895 | ||
ec93aa12 | 896 | STREAM_GETW(s, src_p.family); |
d62a17ae | 897 | |
898 | if (src_p.family == AF_INET) | |
899 | src_p.prefixlen = IPV4_MAX_BYTELEN; | |
900 | else | |
901 | src_p.prefixlen = IPV6_MAX_BYTELEN; | |
902 | ||
ec93aa12 | 903 | STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen); |
d62a17ae | 904 | if (src_p.family == AF_INET) |
905 | inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf)); | |
906 | else | |
907 | inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf)); | |
908 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
909 | ZEBRA_PTM_BFD_SRC_IP_FIELD, buf); | |
910 | ||
911 | if (zvrf_id(zvrf) != VRF_DEFAULT) | |
912 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
913 | ZEBRA_PTM_BFD_VRF_NAME_FIELD, | |
914 | zvrf_name(zvrf)); | |
915 | } else { | |
916 | if (dst_p.family == AF_INET6) { | |
ec93aa12 | 917 | STREAM_GETW(s, src_p.family); |
d62a17ae | 918 | |
919 | if (src_p.family == AF_INET) | |
920 | src_p.prefixlen = IPV4_MAX_BYTELEN; | |
921 | else | |
922 | src_p.prefixlen = IPV6_MAX_BYTELEN; | |
923 | ||
ec93aa12 | 924 | STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen); |
d62a17ae | 925 | if (src_p.family == AF_INET) { |
926 | inet_ntop(AF_INET, &src_p.u.prefix4, buf, | |
927 | sizeof(buf)); | |
928 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
929 | ZEBRA_PTM_BFD_SRC_IP_FIELD, | |
930 | buf); | |
931 | } else { | |
932 | inet_ntop(AF_INET6, &src_p.u.prefix6, buf, | |
933 | sizeof(buf)); | |
934 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
935 | ZEBRA_PTM_BFD_SRC_IP_FIELD, | |
936 | buf); | |
937 | } | |
938 | } | |
939 | ||
ec93aa12 DS |
940 | STREAM_GETC(s, len); |
941 | STREAM_GET(if_name, s, len); | |
d62a17ae | 942 | if_name[len] = '\0'; |
943 | ||
944 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
945 | ZEBRA_PTM_BFD_IFNAME_FIELD, if_name); | |
946 | } | |
947 | ||
948 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len); | |
949 | if (IS_ZEBRA_DEBUG_SEND) | |
950 | zlog_debug("%s: Sent message (%d) %s", __func__, data_len, | |
951 | ptm_cb.out_data); | |
952 | ||
953 | zebra_ptm_send_message(ptm_cb.out_data, data_len); | |
ec93aa12 | 954 | |
8068a649 | 955 | return; |
9cc46248 | 956 | |
ec93aa12 | 957 | stream_failure: |
a928d464 | 958 | ptm_lib_cleanup_msg(ptm_hdl, out_ctxt); |
c01acf98 | 959 | } |
c8ed14dd | 960 | |
055c4dfc | 961 | /* BFD client register */ |
89f4e507 | 962 | void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS) |
055c4dfc | 963 | { |
d62a17ae | 964 | struct stream *s; |
965 | unsigned int pid; | |
9cc46248 | 966 | void *out_ctxt = NULL; |
d62a17ae | 967 | char tmp_buf[64]; |
968 | int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
055c4dfc | 969 | |
d62a17ae | 970 | client->bfd_client_reg_cnt++; |
055c4dfc | 971 | |
d62a17ae | 972 | if (IS_ZEBRA_DEBUG_EVENT) |
973 | zlog_debug("bfd_client_register msg from client %s: length=%d", | |
89f4e507 | 974 | zebra_route_string(client->proto), hdr->length); |
055c4dfc | 975 | |
1002497a | 976 | s = msg; |
ec93aa12 | 977 | STREAM_GETL(s, pid); |
bf6e101c | 978 | |
d62a17ae | 979 | if (ptm_cb.ptm_sock == -1) { |
980 | ptm_cb.t_timer = NULL; | |
981 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
982 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
8068a649 | 983 | return; |
d62a17ae | 984 | } |
055c4dfc | 985 | |
d62a17ae | 986 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt); |
055c4dfc | 987 | |
d62a17ae | 988 | sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD); |
989 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf); | |
055c4dfc | 990 | |
d62a17ae | 991 | sprintf(tmp_buf, "%s", zebra_route_string(client->proto)); |
992 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD, | |
993 | tmp_buf); | |
055c4dfc | 994 | |
d62a17ae | 995 | sprintf(tmp_buf, "%d", pid); |
996 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD, | |
997 | tmp_buf); | |
055c4dfc | 998 | |
d62a17ae | 999 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len); |
055c4dfc | 1000 | |
d62a17ae | 1001 | if (IS_ZEBRA_DEBUG_SEND) |
1002 | zlog_debug("%s: Sent message (%d) %s", __func__, data_len, | |
1003 | ptm_cb.out_data); | |
1004 | zebra_ptm_send_message(ptm_cb.out_data, data_len); | |
2376c3f2 | 1005 | |
d62a17ae | 1006 | SET_FLAG(ptm_cb.client_flags[client->proto], |
1007 | ZEBRA_PTM_BFD_CLIENT_FLAG_REG); | |
9cc46248 | 1008 | |
8068a649 | 1009 | return; |
9cc46248 | 1010 | |
ec93aa12 | 1011 | stream_failure: |
6447dbb3 DS |
1012 | /* |
1013 | * IF we ever add more STREAM_GETXXX functions after the out_ctxt | |
1014 | * is allocated then we need to add this code back in | |
1015 | * | |
1016 | * if (out_ctxt) | |
1017 | * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt); | |
1018 | */ | |
8068a649 | 1019 | return; |
055c4dfc | 1020 | } |
1021 | ||
567b877d | 1022 | /* BFD client deregister */ |
453844ab | 1023 | int zebra_ptm_bfd_client_deregister(struct zserv *client) |
567b877d | 1024 | { |
453844ab | 1025 | uint8_t proto = client->proto; |
d62a17ae | 1026 | void *out_ctxt; |
1027 | char tmp_buf[64]; | |
1028 | int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
567b877d | 1029 | |
d62a17ae | 1030 | if (proto != ZEBRA_ROUTE_OSPF && proto != ZEBRA_ROUTE_BGP |
1031 | && proto != ZEBRA_ROUTE_OSPF6 && proto != ZEBRA_ROUTE_PIM) | |
453844ab | 1032 | return 0; |
567b877d | 1033 | |
d62a17ae | 1034 | if (IS_ZEBRA_DEBUG_EVENT) |
43e52561 QY |
1035 | zlog_warn("bfd_client_deregister msg for client %s", |
1036 | zebra_route_string(proto)); | |
567b877d | 1037 | |
d62a17ae | 1038 | if (ptm_cb.ptm_sock == -1) { |
1039 | ptm_cb.t_timer = NULL; | |
1040 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
1041 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
453844ab | 1042 | return 0; |
d62a17ae | 1043 | } |
567b877d | 1044 | |
d62a17ae | 1045 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt); |
567b877d | 1046 | |
d62a17ae | 1047 | sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD); |
1048 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf); | |
567b877d | 1049 | |
d62a17ae | 1050 | sprintf(tmp_buf, "%s", zebra_route_string(proto)); |
1051 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD, | |
1052 | tmp_buf); | |
567b877d | 1053 | |
d62a17ae | 1054 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len); |
567b877d | 1055 | |
d62a17ae | 1056 | if (IS_ZEBRA_DEBUG_SEND) |
1057 | zlog_debug("%s: Sent message (%d) %s", __func__, data_len, | |
1058 | ptm_cb.out_data); | |
2376c3f2 | 1059 | |
d62a17ae | 1060 | zebra_ptm_send_message(ptm_cb.out_data, data_len); |
1061 | UNSET_FLAG(ptm_cb.client_flags[proto], ZEBRA_PTM_BFD_CLIENT_FLAG_REG); | |
453844ab QY |
1062 | |
1063 | return 0; | |
567b877d | 1064 | } |
1065 | ||
d62a17ae | 1066 | int zebra_ptm_get_enable_state(void) |
c8ed14dd | 1067 | { |
d62a17ae | 1068 | return ptm_cb.ptm_enable; |
c8ed14dd | 1069 | } |
950bd436 | 1070 | |
1071 | /* | |
1072 | * zebra_ptm_get_status_str - Convert status to a display string. | |
1073 | */ | |
d62a17ae | 1074 | static const char *zebra_ptm_get_status_str(int status) |
950bd436 | 1075 | { |
d62a17ae | 1076 | switch (status) { |
1077 | case ZEBRA_PTM_STATUS_DOWN: | |
1078 | return "fail"; | |
1079 | case ZEBRA_PTM_STATUS_UP: | |
1080 | return "pass"; | |
1081 | case ZEBRA_PTM_STATUS_UNKNOWN: | |
1082 | default: | |
1083 | return "n/a"; | |
1084 | } | |
950bd436 | 1085 | } |
1086 | ||
d62a17ae | 1087 | void zebra_ptm_show_status(struct vty *vty, struct interface *ifp) |
950bd436 | 1088 | { |
d62a17ae | 1089 | vty_out(vty, " PTM status: "); |
1090 | if (ifp->ptm_enable) { | |
1091 | vty_out(vty, "%s\n", zebra_ptm_get_status_str(ifp->ptm_status)); | |
1092 | } else { | |
1093 | vty_out(vty, "disabled\n"); | |
1094 | } | |
950bd436 | 1095 | } |
1096 | ||
d62a17ae | 1097 | void zebra_ptm_send_status_req(void) |
950bd436 | 1098 | { |
d62a17ae | 1099 | void *out_ctxt; |
1100 | int len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
1101 | ||
1102 | if (ptm_cb.ptm_enable) { | |
1103 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, | |
1104 | &out_ctxt); | |
1105 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, | |
1106 | ZEBRA_PTM_GET_STATUS_CMD); | |
1107 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &len); | |
1108 | ||
1109 | zebra_ptm_send_message(ptm_cb.out_data, len); | |
1110 | } | |
950bd436 | 1111 | } |
1112 | ||
d62a17ae | 1113 | void zebra_ptm_reset_status(int ptm_disable) |
950bd436 | 1114 | { |
d62a17ae | 1115 | struct vrf *vrf; |
d62a17ae | 1116 | struct interface *ifp; |
1117 | int send_linkup; | |
1118 | ||
a2addae8 | 1119 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) |
451fda4f | 1120 | FOR_ALL_INTERFACES (vrf, ifp) { |
a2addae8 RW |
1121 | send_linkup = 0; |
1122 | if (ifp->ptm_enable) { | |
1123 | if (!if_is_operative(ifp)) | |
1124 | send_linkup = 1; | |
1125 | ||
1126 | if (ptm_disable) | |
1127 | ifp->ptm_enable = | |
1128 | ZEBRA_IF_PTM_ENABLE_OFF; | |
1129 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; | |
1130 | ||
1131 | if (if_is_operative(ifp) && send_linkup) { | |
1132 | if (IS_ZEBRA_DEBUG_EVENT) | |
1133 | zlog_debug( | |
1134 | "%s: Bringing up interface %s", | |
1135 | __func__, ifp->name); | |
1136 | if_up(ifp); | |
1137 | } | |
d62a17ae | 1138 | } |
1139 | } | |
950bd436 | 1140 | } |
986aa00f | 1141 | |
d62a17ae | 1142 | void zebra_ptm_if_init(struct zebra_if *zebra_ifp) |
986aa00f | 1143 | { |
d62a17ae | 1144 | zebra_ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC; |
986aa00f | 1145 | } |
1146 | ||
d62a17ae | 1147 | void zebra_ptm_if_set_ptm_state(struct interface *ifp, |
1148 | struct zebra_if *zebra_ifp) | |
986aa00f | 1149 | { |
d62a17ae | 1150 | if (zebra_ifp && zebra_ifp->ptm_enable != ZEBRA_IF_PTM_ENABLE_UNSPEC) |
1151 | ifp->ptm_enable = zebra_ifp->ptm_enable; | |
986aa00f | 1152 | } |
1153 | ||
d62a17ae | 1154 | void zebra_ptm_if_write(struct vty *vty, struct zebra_if *zebra_ifp) |
986aa00f | 1155 | { |
d62a17ae | 1156 | if (zebra_ifp->ptm_enable == ZEBRA_IF_PTM_ENABLE_OFF) |
1157 | vty_out(vty, " no ptm-enable\n"); | |
986aa00f | 1158 | } |
d3af6147 RZ |
1159 | |
1160 | #else /* HAVE_BFDD */ | |
1161 | ||
1162 | #include "zebra/zebra_memory.h" | |
1163 | ||
1164 | /* | |
1165 | * Data structures. | |
1166 | */ | |
1167 | struct ptm_process { | |
1168 | struct zserv *pp_zs; | |
1169 | pid_t pp_pid; | |
1170 | ||
1171 | TAILQ_ENTRY(ptm_process) pp_entry; | |
1172 | }; | |
1173 | TAILQ_HEAD(ppqueue, ptm_process) ppqueue; | |
1174 | ||
1175 | DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_PTM_BFD_PROCESS, | |
1176 | "PTM BFD process registration table."); | |
1177 | ||
1178 | /* | |
1179 | * Prototypes. | |
1180 | */ | |
1181 | static struct ptm_process *pp_new(pid_t pid, struct zserv *zs); | |
1182 | static struct ptm_process *pp_lookup_byzs(struct zserv *zs); | |
1183 | static void pp_free(struct ptm_process *pp); | |
788378fe | 1184 | static void pp_free_all(void); |
d3af6147 RZ |
1185 | |
1186 | static void zebra_ptm_send_bfdd(struct stream *msg); | |
1187 | static void zebra_ptm_send_clients(struct stream *msg); | |
1188 | static int _zebra_ptm_bfd_client_deregister(struct zserv *zs); | |
1189 | static void _zebra_ptm_reroute(struct zserv *zs, struct stream *msg, | |
1190 | uint32_t command); | |
1191 | ||
1192 | ||
1193 | /* | |
1194 | * Process PID registration. | |
1195 | */ | |
1196 | static struct ptm_process *pp_new(pid_t pid, struct zserv *zs) | |
1197 | { | |
1198 | struct ptm_process *pp; | |
1199 | ||
1200 | #ifdef PTM_DEBUG | |
1201 | /* Sanity check: more than one client can't have the same PID. */ | |
1202 | TAILQ_FOREACH(pp, &ppqueue, pp_entry) { | |
1203 | if (pp->pp_pid == pid && pp->pp_zs != zs) | |
1204 | zlog_err("%s:%d pid and client pointer doesn't match", | |
1205 | __FILE__, __LINE__); | |
1206 | } | |
1207 | #endif /* PTM_DEBUG */ | |
1208 | ||
1209 | /* Lookup for duplicates. */ | |
1210 | pp = pp_lookup_byzs(zs); | |
1211 | if (pp != NULL) | |
1212 | return pp; | |
1213 | ||
1214 | /* Allocate and register new process. */ | |
1215 | pp = XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS, sizeof(*pp)); | |
1216 | if (pp == NULL) | |
1217 | return NULL; | |
1218 | ||
1219 | pp->pp_pid = pid; | |
1220 | pp->pp_zs = zs; | |
1221 | TAILQ_INSERT_HEAD(&ppqueue, pp, pp_entry); | |
1222 | ||
1223 | return pp; | |
1224 | } | |
1225 | ||
1226 | static struct ptm_process *pp_lookup_byzs(struct zserv *zs) | |
1227 | { | |
1228 | struct ptm_process *pp; | |
1229 | ||
1230 | TAILQ_FOREACH(pp, &ppqueue, pp_entry) { | |
1231 | if (pp->pp_zs != zs) | |
1232 | continue; | |
1233 | ||
1234 | break; | |
1235 | } | |
1236 | ||
1237 | return pp; | |
1238 | } | |
1239 | ||
1240 | static void pp_free(struct ptm_process *pp) | |
1241 | { | |
1242 | if (pp == NULL) | |
1243 | return; | |
1244 | ||
1245 | TAILQ_REMOVE(&ppqueue, pp, pp_entry); | |
1246 | XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS, pp); | |
1247 | } | |
1248 | ||
788378fe RZ |
1249 | static void pp_free_all(void) |
1250 | { | |
1251 | struct ptm_process *pp; | |
1252 | ||
1253 | while (!TAILQ_EMPTY(&ppqueue)) { | |
1254 | pp = TAILQ_FIRST(&ppqueue); | |
1255 | pp_free(pp); | |
1256 | } | |
1257 | } | |
1258 | ||
d3af6147 RZ |
1259 | |
1260 | /* | |
1261 | * Use the FRR's internal daemon implementation. | |
1262 | */ | |
1263 | static void zebra_ptm_send_bfdd(struct stream *msg) | |
1264 | { | |
1265 | struct listnode *node; | |
1266 | struct zserv *client; | |
1267 | struct stream *msgc; | |
1268 | ||
1269 | /* Create copy for replication. */ | |
1270 | msgc = stream_dup(msg); | |
1271 | if (msgc == NULL) { | |
1272 | zlog_warn("%s: not enough memory", __func__); | |
1273 | return; | |
1274 | } | |
1275 | ||
1276 | /* Send message to all running BFDd daemons. */ | |
1277 | for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client)) { | |
1278 | if (client->proto != ZEBRA_ROUTE_BFD) | |
1279 | continue; | |
1280 | ||
1281 | zserv_send_message(client, msg); | |
1282 | ||
1283 | /* Allocate more messages. */ | |
1284 | msg = stream_dup(msgc); | |
1285 | if (msg == NULL) { | |
1286 | zlog_warn("%s: not enough memory", __func__); | |
1287 | return; | |
1288 | } | |
1289 | } | |
1290 | ||
1291 | stream_free(msgc); | |
7efe273a | 1292 | stream_free(msg); |
d3af6147 RZ |
1293 | } |
1294 | ||
1295 | static void zebra_ptm_send_clients(struct stream *msg) | |
1296 | { | |
1297 | struct listnode *node; | |
1298 | struct zserv *client; | |
1299 | struct stream *msgc; | |
1300 | ||
1301 | /* Create copy for replication. */ | |
1302 | msgc = stream_dup(msg); | |
1303 | if (msgc == NULL) { | |
1304 | zlog_warn("%s: not enough memory", __func__); | |
1305 | return; | |
1306 | } | |
1307 | ||
1308 | /* Send message to all running client daemons. */ | |
1309 | for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client)) { | |
1310 | switch (client->proto) { | |
1311 | case ZEBRA_ROUTE_BGP: | |
1312 | case ZEBRA_ROUTE_OSPF: | |
1313 | case ZEBRA_ROUTE_OSPF6: | |
1314 | case ZEBRA_ROUTE_PIM: | |
1315 | break; | |
1316 | ||
1317 | default: | |
1318 | /* NOTHING: skip this daemon. */ | |
1319 | continue; | |
1320 | } | |
1321 | ||
1322 | zserv_send_message(client, msg); | |
1323 | ||
1324 | /* Allocate more messages. */ | |
1325 | msg = stream_dup(msgc); | |
1326 | if (msg == NULL) { | |
1327 | zlog_warn("%s: not enough memory", __func__); | |
1328 | return; | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | stream_free(msgc); | |
7efe273a | 1333 | stream_free(msg); |
d3af6147 RZ |
1334 | } |
1335 | ||
1336 | static int _zebra_ptm_bfd_client_deregister(struct zserv *zs) | |
1337 | { | |
1338 | struct stream *msg; | |
1339 | struct ptm_process *pp; | |
1340 | ||
1341 | /* Filter daemons that must receive this treatment. */ | |
1342 | switch (zs->proto) { | |
1343 | case ZEBRA_ROUTE_BGP: | |
1344 | case ZEBRA_ROUTE_OSPF: | |
1345 | case ZEBRA_ROUTE_OSPF6: | |
1346 | case ZEBRA_ROUTE_PIM: | |
1347 | break; | |
1348 | ||
1349 | case ZEBRA_ROUTE_BFD: | |
1350 | /* Don't try to send BFDd messages to itself. */ | |
1351 | return 0; | |
1352 | ||
1353 | default: | |
1354 | /* Unsupported daemon. */ | |
1355 | return 0; | |
1356 | } | |
1357 | ||
1358 | /* Find daemon pid by zebra connection pointer. */ | |
1359 | pp = pp_lookup_byzs(zs); | |
1360 | if (pp == NULL) { | |
1361 | zlog_err("%s:%d failed to find process pid registration", | |
1362 | __FILE__, __LINE__); | |
1363 | return -1; | |
1364 | } | |
1365 | ||
1366 | /* Generate, send message and free() daemon related data. */ | |
1367 | msg = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1368 | if (msg == NULL) { | |
1369 | zlog_warn("%s: not enough memory", __func__); | |
1370 | return 0; | |
1371 | } | |
1372 | ||
1373 | /* | |
1374 | * The message type will be BFD_DEST_REPLY so we can use only | |
1375 | * one callback at the `bfdd` side, however the real command | |
1376 | * number will be included right after the zebra header. | |
1377 | */ | |
1378 | zclient_create_header(msg, ZEBRA_BFD_DEST_REPLAY, 0); | |
1379 | stream_putl(msg, ZEBRA_BFD_CLIENT_DEREGISTER); | |
1380 | ||
1381 | /* Put process PID. */ | |
1382 | stream_putl(msg, pp->pp_pid); | |
1383 | ||
1384 | /* Update the data pointers. */ | |
1385 | stream_putw_at(msg, 0, stream_get_endp(msg)); | |
1386 | ||
1387 | zebra_ptm_send_bfdd(msg); | |
1388 | ||
1389 | pp_free(pp); | |
1390 | ||
1391 | return 0; | |
1392 | } | |
1393 | ||
1394 | void zebra_ptm_init(void) | |
1395 | { | |
1396 | /* Initialize the ptm process information list. */ | |
1397 | TAILQ_INIT(&ppqueue); | |
1398 | ||
1399 | /* | |
1400 | * Send deregistration messages to BFD daemon when some other | |
1401 | * daemon closes. This will help avoid sending daemons | |
1402 | * unnecessary notification messages. | |
1403 | */ | |
1404 | hook_register(zserv_client_close, _zebra_ptm_bfd_client_deregister); | |
1405 | } | |
1406 | ||
788378fe RZ |
1407 | void zebra_ptm_finish(void) |
1408 | { | |
1409 | /* Remove the client disconnect hook and free all memory. */ | |
1410 | hook_unregister(zserv_client_close, _zebra_ptm_bfd_client_deregister); | |
1411 | pp_free_all(); | |
1412 | } | |
1413 | ||
d3af6147 RZ |
1414 | |
1415 | /* | |
1416 | * Message handling. | |
1417 | */ | |
1418 | static void _zebra_ptm_reroute(struct zserv *zs, struct stream *msg, | |
1419 | uint32_t command) | |
1420 | { | |
1421 | struct stream *msgc; | |
1422 | size_t zmsglen, zhdrlen; | |
1423 | pid_t ppid; | |
1424 | ||
1425 | /* | |
1426 | * Don't modify message in the zebra API. In order to do that we | |
1427 | * need to allocate a new message stream and copy the message | |
1428 | * provided by zebra. | |
1429 | */ | |
1430 | msgc = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1431 | if (msgc == NULL) { | |
1432 | zlog_warn("%s: not enough memory", __func__); | |
1433 | return; | |
1434 | } | |
1435 | ||
1436 | /* Calculate our header size plus the message contents. */ | |
1437 | zhdrlen = ZEBRA_HEADER_SIZE + sizeof(uint32_t); | |
1438 | zmsglen = msg->endp - msg->getp; | |
1439 | memcpy(msgc->data + zhdrlen, msg->data + msg->getp, zmsglen); | |
1440 | ||
1441 | /* | |
1442 | * The message type will be BFD_DEST_REPLY so we can use only | |
1443 | * one callback at the `bfdd` side, however the real command | |
1444 | * number will be included right after the zebra header. | |
1445 | */ | |
1446 | zclient_create_header(msgc, ZEBRA_BFD_DEST_REPLAY, 0); | |
1447 | stream_putl(msgc, command); | |
1448 | ||
1449 | /* Update the data pointers. */ | |
1450 | msgc->getp = 0; | |
1451 | msgc->endp = zhdrlen + zmsglen; | |
1452 | stream_putw_at(msgc, 0, stream_get_endp(msgc)); | |
1453 | ||
1454 | zebra_ptm_send_bfdd(msgc); | |
1455 | ||
1456 | /* Registrate process PID for shutdown hook. */ | |
1457 | STREAM_GETL(msg, ppid); | |
1458 | pp_new(ppid, zs); | |
1459 | ||
1460 | return; | |
1461 | ||
1462 | stream_failure: | |
1463 | zlog_err("%s:%d failed to registrate client pid", __FILE__, __LINE__); | |
1464 | } | |
1465 | ||
1466 | void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS) | |
1467 | { | |
1468 | if (IS_ZEBRA_DEBUG_EVENT) | |
1469 | zlog_debug("bfd_dst_register msg from client %s: length=%d", | |
1470 | zebra_route_string(client->proto), hdr->length); | |
1471 | ||
1472 | _zebra_ptm_reroute(client, msg, ZEBRA_BFD_DEST_REGISTER); | |
1473 | } | |
1474 | ||
1475 | void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS) | |
1476 | { | |
1477 | if (IS_ZEBRA_DEBUG_EVENT) | |
1478 | zlog_debug("bfd_dst_deregister msg from client %s: length=%d", | |
1479 | zebra_route_string(client->proto), hdr->length); | |
1480 | ||
1481 | _zebra_ptm_reroute(client, msg, ZEBRA_BFD_DEST_DEREGISTER); | |
1482 | } | |
1483 | ||
1484 | void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS) | |
1485 | { | |
1486 | if (IS_ZEBRA_DEBUG_EVENT) | |
1487 | zlog_debug("bfd_client_register msg from client %s: length=%d", | |
1488 | zebra_route_string(client->proto), hdr->length); | |
1489 | ||
1490 | _zebra_ptm_reroute(client, msg, ZEBRA_BFD_CLIENT_REGISTER); | |
1491 | } | |
1492 | ||
1493 | void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS) | |
1494 | { | |
1495 | struct stream *msgc; | |
1496 | size_t zmsglen, zhdrlen; | |
971532e2 | 1497 | uint32_t cmd; |
d3af6147 RZ |
1498 | |
1499 | /* | |
1500 | * NOTE: | |
1501 | * Replay messages with HAVE_BFDD are meant to be replayed to | |
1502 | * the client daemons. These messages are composed and | |
1503 | * originated from the `bfdd` daemon. | |
1504 | */ | |
1505 | if (IS_ZEBRA_DEBUG_EVENT) | |
1506 | zlog_debug("bfd_dst_update msg from client %s: length=%d", | |
1507 | zebra_route_string(client->proto), hdr->length); | |
1508 | ||
971532e2 RZ |
1509 | /* |
1510 | * Client messages must be re-routed, otherwise do the `bfdd` | |
1511 | * special treatment. | |
1512 | */ | |
1513 | if (client->proto != ZEBRA_ROUTE_BFD) { | |
1514 | _zebra_ptm_reroute(client, msg, ZEBRA_BFD_DEST_REPLAY); | |
1515 | return; | |
1516 | } | |
1517 | ||
1518 | /* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */ | |
1519 | if (stream_getl2(msg, &cmd) == false) { | |
1520 | zlog_err("%s: expected at least 4 bytes (command)", __func__); | |
1521 | return; | |
1522 | } | |
1523 | ||
d3af6147 RZ |
1524 | /* |
1525 | * Don't modify message in the zebra API. In order to do that we | |
1526 | * need to allocate a new message stream and copy the message | |
1527 | * provided by zebra. | |
1528 | */ | |
1529 | msgc = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1530 | if (msgc == NULL) { | |
1531 | zlog_warn("%s: not enough memory", __func__); | |
1532 | return; | |
1533 | } | |
1534 | ||
1535 | /* Calculate our header size plus the message contents. */ | |
971532e2 RZ |
1536 | if (cmd != ZEBRA_BFD_DEST_REPLAY) { |
1537 | zhdrlen = ZEBRA_HEADER_SIZE; | |
1538 | zmsglen = msg->endp - msg->getp; | |
1539 | memcpy(msgc->data + zhdrlen, msg->data + msg->getp, zmsglen); | |
d3af6147 | 1540 | |
971532e2 RZ |
1541 | zclient_create_header(msgc, cmd, zvrf_id(zvrf)); |
1542 | ||
1543 | msgc->getp = 0; | |
1544 | msgc->endp = zhdrlen + zmsglen; | |
1545 | } else | |
1546 | zclient_create_header(msgc, cmd, zvrf_id(zvrf)); | |
d3af6147 RZ |
1547 | |
1548 | /* Update the data pointers. */ | |
d3af6147 RZ |
1549 | stream_putw_at(msgc, 0, stream_get_endp(msgc)); |
1550 | ||
1551 | zebra_ptm_send_clients(msgc); | |
1552 | } | |
1553 | ||
1554 | /* | |
1555 | * Unused functions. | |
1556 | */ | |
d3af6147 RZ |
1557 | void zebra_ptm_if_init(struct zebra_if *zifp __attribute__((__unused__))) |
1558 | { | |
1559 | /* NOTHING */ | |
1560 | } | |
1561 | ||
1562 | int zebra_ptm_get_enable_state(void) | |
1563 | { | |
5efdb801 | 1564 | return 0; |
d3af6147 RZ |
1565 | } |
1566 | ||
1567 | void zebra_ptm_show_status(struct vty *vty __attribute__((__unused__)), | |
1568 | struct interface *ifp __attribute__((__unused__))) | |
1569 | { | |
1570 | /* NOTHING */ | |
1571 | } | |
1572 | ||
1573 | void zebra_ptm_write(struct vty *vty __attribute__((__unused__))) | |
1574 | { | |
1575 | /* NOTHING */ | |
1576 | } | |
1577 | ||
1578 | void zebra_ptm_if_write(struct vty *vty __attribute__((__unused__)), | |
1579 | struct zebra_if *zifp __attribute__((__unused__))) | |
1580 | { | |
1581 | /* NOTHING */ | |
1582 | } | |
1583 | void zebra_ptm_if_set_ptm_state(struct interface *i __attribute__((__unused__)), | |
1584 | struct zebra_if *zi __attribute__((__unused__))) | |
1585 | { | |
1586 | /* NOTHING */ | |
1587 | } | |
1588 | ||
1589 | #endif /* HAVE_BFDD */ |