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