]>
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); |
cc9f21da | 103 | void zebra_ptm_sock_read(struct thread *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 | |
6006b807 | 117 | memset(&ptm_cb, 0, sizeof(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 | 159 | /* Cancel events. */ |
146bcb9b DS |
160 | THREAD_OFF(ptm_cb.t_read); |
161 | THREAD_OFF(ptm_cb.t_write); | |
162 | THREAD_OFF(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 | ||
cc9f21da | 171 | static void zebra_ptm_flush_messages(struct thread *thread) |
c8ed14dd | 172 | { |
d62a17ae | 173 | ptm_cb.t_write = NULL; |
174 | ||
175 | if (ptm_cb.ptm_sock == -1) | |
cc9f21da | 176 | return; |
d62a17ae | 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); |
cc9f21da | 190 | return; |
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 | } | |
c8ed14dd DS |
199 | } |
200 | ||
d62a17ae | 201 | static int zebra_ptm_send_message(char *data, int size) |
c8ed14dd | 202 | { |
d62a17ae | 203 | errno = 0; |
204 | switch (buffer_write(ptm_cb.wb, ptm_cb.ptm_sock, data, size)) { | |
205 | case BUFFER_ERROR: | |
1c50c1c0 QY |
206 | flog_err_sys(EC_LIB_SOCKET, "%s ptm socket error: %s", __func__, |
207 | safe_strerror(errno)); | |
d62a17ae | 208 | close(ptm_cb.ptm_sock); |
209 | ptm_cb.ptm_sock = -1; | |
210 | zebra_ptm_reset_status(0); | |
211 | ptm_cb.t_timer = NULL; | |
3801e764 | 212 | thread_add_timer(zrouter.master, zebra_ptm_connect, NULL, |
d62a17ae | 213 | ptm_cb.reconnect_time, &ptm_cb.t_timer); |
214 | return -1; | |
215 | case BUFFER_EMPTY: | |
146bcb9b | 216 | THREAD_OFF(ptm_cb.t_write); |
d62a17ae | 217 | break; |
218 | case BUFFER_PENDING: | |
3801e764 | 219 | thread_add_write(zrouter.master, zebra_ptm_flush_messages, NULL, |
d62a17ae | 220 | ptm_cb.ptm_sock, &ptm_cb.t_write); |
221 | break; | |
222 | } | |
223 | ||
224 | return 0; | |
244c1cdc DS |
225 | } |
226 | ||
cc9f21da | 227 | void zebra_ptm_connect(struct thread *t) |
244c1cdc | 228 | { |
d62a17ae | 229 | int init = 0; |
230 | ||
231 | if (ptm_cb.ptm_sock == -1) { | |
232 | zebra_ptm_socket_init(); | |
233 | init = 1; | |
234 | } | |
235 | ||
236 | if (ptm_cb.ptm_sock != -1) { | |
237 | if (init) { | |
238 | ptm_cb.t_read = NULL; | |
3801e764 | 239 | thread_add_read(zrouter.master, zebra_ptm_sock_read, |
d62a17ae | 240 | NULL, ptm_cb.ptm_sock, &ptm_cb.t_read); |
241 | zebra_bfd_peer_replay_req(); | |
242 | } | |
243 | zebra_ptm_send_status_req(); | |
244 | ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL; | |
245 | } else if (ptm_cb.reconnect_time < ZEBRA_PTM_RECONNECT_TIME_MAX) { | |
246 | ptm_cb.reconnect_time *= 2; | |
247 | if (ptm_cb.reconnect_time > ZEBRA_PTM_RECONNECT_TIME_MAX) | |
248 | ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_MAX; | |
249 | ||
250 | ptm_cb.t_timer = NULL; | |
3801e764 | 251 | thread_add_timer(zrouter.master, zebra_ptm_connect, NULL, |
d62a17ae | 252 | ptm_cb.reconnect_time, &ptm_cb.t_timer); |
253 | } else if (ptm_cb.reconnect_time >= ZEBRA_PTM_RECONNECT_TIME_MAX) { | |
254 | ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL; | |
255 | } | |
244c1cdc DS |
256 | } |
257 | ||
244c1cdc DS |
258 | DEFUN (zebra_ptm_enable, |
259 | zebra_ptm_enable_cmd, | |
260 | "ptm-enable", | |
261 | "Enable neighbor check with specified topology\n") | |
262 | { | |
d62a17ae | 263 | struct vrf *vrf; |
d62a17ae | 264 | struct interface *ifp; |
265 | struct zebra_if *if_data; | |
266 | ||
267 | ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_ON; | |
268 | ||
a2addae8 | 269 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) |
451fda4f | 270 | FOR_ALL_INTERFACES (vrf, ifp) |
a2addae8 RW |
271 | if (!ifp->ptm_enable) { |
272 | if_data = (struct zebra_if *)ifp->info; | |
273 | if (if_data | |
274 | && (if_data->ptm_enable | |
9d303b37 | 275 | == ZEBRA_IF_PTM_ENABLE_UNSPEC)) { |
a2addae8 RW |
276 | ifp->ptm_enable = |
277 | ZEBRA_IF_PTM_ENABLE_ON; | |
278 | } | |
279 | /* Assign a default unknown status */ | |
280 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; | |
d62a17ae | 281 | } |
d62a17ae | 282 | |
283 | zebra_ptm_connect(NULL); | |
284 | ||
285 | return CMD_SUCCESS; | |
244c1cdc DS |
286 | } |
287 | ||
288 | DEFUN (no_zebra_ptm_enable, | |
289 | no_zebra_ptm_enable_cmd, | |
290 | "no ptm-enable", | |
291 | NO_STR | |
292 | "Enable neighbor check with specified topology\n") | |
293 | { | |
d62a17ae | 294 | ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF; |
295 | zebra_ptm_reset_status(1); | |
296 | return CMD_SUCCESS; | |
244c1cdc DS |
297 | } |
298 | ||
986aa00f | 299 | DEFUN (zebra_ptm_enable_if, |
300 | zebra_ptm_enable_if_cmd, | |
301 | "ptm-enable", | |
302 | "Enable neighbor check with specified topology\n") | |
303 | { | |
d62a17ae | 304 | VTY_DECLVAR_CONTEXT(interface, ifp); |
305 | struct zebra_if *if_data; | |
306 | int old_ptm_enable; | |
307 | int send_linkdown = 0; | |
308 | ||
a2023fab | 309 | if_data = ifp->info; |
310 | if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC; | |
311 | ||
d62a17ae | 312 | if (ifp->ifindex == IFINDEX_INTERNAL) { |
313 | return CMD_SUCCESS; | |
314 | } | |
315 | ||
316 | old_ptm_enable = ifp->ptm_enable; | |
317 | ifp->ptm_enable = ptm_cb.ptm_enable; | |
318 | ||
319 | if (if_is_no_ptm_operative(ifp)) | |
320 | send_linkdown = 1; | |
321 | ||
322 | if (!old_ptm_enable && ptm_cb.ptm_enable) { | |
323 | if (!if_is_operative(ifp) && send_linkdown) { | |
324 | if (IS_ZEBRA_DEBUG_EVENT) | |
9165c5f5 | 325 | zlog_debug("%s: Bringing down interface %s", |
d62a17ae | 326 | __func__, ifp->name); |
327 | if_down(ifp); | |
328 | } | |
329 | } | |
330 | ||
d62a17ae | 331 | return CMD_SUCCESS; |
986aa00f | 332 | } |
333 | ||
334 | DEFUN (no_zebra_ptm_enable_if, | |
335 | no_zebra_ptm_enable_if_cmd, | |
336 | "no ptm-enable", | |
337 | NO_STR | |
338 | "Enable neighbor check with specified topology\n") | |
339 | { | |
d62a17ae | 340 | VTY_DECLVAR_CONTEXT(interface, ifp); |
341 | int send_linkup = 0; | |
342 | struct zebra_if *if_data; | |
343 | ||
344 | if ((ifp->ifindex != IFINDEX_INTERNAL) && (ifp->ptm_enable)) { | |
345 | if (!if_is_operative(ifp)) | |
346 | send_linkup = 1; | |
347 | ||
348 | ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF; | |
349 | if (if_is_no_ptm_operative(ifp) && send_linkup) { | |
350 | if (IS_ZEBRA_DEBUG_EVENT) | |
9165c5f5 | 351 | zlog_debug("%s: Bringing up interface %s", |
d62a17ae | 352 | __func__, ifp->name); |
8b48cdb9 | 353 | if_up(ifp, true); |
d62a17ae | 354 | } |
355 | } | |
356 | ||
357 | if_data = ifp->info; | |
358 | if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF; | |
359 | ||
360 | return CMD_SUCCESS; | |
986aa00f | 361 | } |
362 | ||
363 | ||
d62a17ae | 364 | void zebra_ptm_write(struct vty *vty) |
244c1cdc | 365 | { |
d62a17ae | 366 | if (ptm_cb.ptm_enable) |
367 | vty_out(vty, "ptm-enable\n"); | |
244c1cdc | 368 | |
d62a17ae | 369 | return; |
244c1cdc DS |
370 | } |
371 | ||
d62a17ae | 372 | static int zebra_ptm_socket_init(void) |
244c1cdc | 373 | { |
d62a17ae | 374 | int ret; |
375 | int sock; | |
376 | struct sockaddr_un addr; | |
377 | ||
378 | ptm_cb.ptm_sock = -1; | |
379 | ||
380 | sock = socket(PF_UNIX, SOCK_STREAM, 0); | |
381 | if (sock < 0) | |
382 | return -1; | |
383 | if (set_nonblocking(sock) < 0) { | |
384 | if (IS_ZEBRA_DEBUG_EVENT) | |
385 | zlog_debug("%s: Unable to set socket non blocking[%s]", | |
15569c58 | 386 | __func__, safe_strerror(errno)); |
d62a17ae | 387 | close(sock); |
388 | return -1; | |
389 | } | |
390 | ||
391 | /* Make server socket. */ | |
6006b807 | 392 | memset(&addr, 0, sizeof(addr)); |
d62a17ae | 393 | addr.sun_family = AF_UNIX; |
394 | memcpy(&addr.sun_path, ZEBRA_PTM_SOCK_NAME, | |
395 | sizeof(ZEBRA_PTM_SOCK_NAME)); | |
396 | ||
397 | ret = connect(sock, (struct sockaddr *)&addr, | |
398 | sizeof(addr.sun_family) + sizeof(ZEBRA_PTM_SOCK_NAME) | |
399 | - 1); | |
400 | if (ret < 0) { | |
401 | if (IS_ZEBRA_DEBUG_EVENT) | |
402 | zlog_debug("%s: Unable to connect to socket %s [%s]", | |
403 | __func__, ZEBRA_PTM_SOCK_NAME, | |
404 | safe_strerror(errno)); | |
405 | close(sock); | |
406 | return -1; | |
407 | } | |
408 | ptm_cb.ptm_sock = sock; | |
409 | return sock; | |
244c1cdc DS |
410 | } |
411 | ||
d62a17ae | 412 | static void zebra_ptm_install_commands(void) |
244c1cdc | 413 | { |
d62a17ae | 414 | install_element(CONFIG_NODE, &zebra_ptm_enable_cmd); |
415 | install_element(CONFIG_NODE, &no_zebra_ptm_enable_cmd); | |
416 | install_element(INTERFACE_NODE, &zebra_ptm_enable_if_cmd); | |
417 | install_element(INTERFACE_NODE, &no_zebra_ptm_enable_if_cmd); | |
244c1cdc DS |
418 | } |
419 | ||
c43ed2e4 | 420 | /* BFD session goes down, send message to the protocols. */ |
d62a17ae | 421 | static void if_bfd_session_update(struct interface *ifp, struct prefix *dp, |
422 | struct prefix *sp, int status, | |
423 | vrf_id_t vrf_id) | |
c01acf98 | 424 | { |
d62a17ae | 425 | if (IS_ZEBRA_DEBUG_EVENT) { |
426 | char buf[2][INET6_ADDRSTRLEN]; | |
427 | ||
428 | if (ifp) { | |
429 | zlog_debug( | |
3efd0893 | 430 | "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s %s event", |
d62a17ae | 431 | inet_ntop(dp->family, &dp->u.prefix, buf[0], |
432 | INET6_ADDRSTRLEN), | |
433 | dp->prefixlen, ifp->name, | |
434 | bfd_get_status_str(status)); | |
435 | } else { | |
c479e756 DS |
436 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
437 | ||
d62a17ae | 438 | zlog_debug( |
3efd0893 | 439 | "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d with src %s/%d and vrf %s(%u) %s event", |
d62a17ae | 440 | inet_ntop(dp->family, &dp->u.prefix, buf[0], |
441 | INET6_ADDRSTRLEN), | |
442 | dp->prefixlen, | |
443 | inet_ntop(sp->family, &sp->u.prefix, buf[1], | |
444 | INET6_ADDRSTRLEN), | |
c479e756 | 445 | sp->prefixlen, VRF_LOGNAME(vrf), vrf_id, |
d62a17ae | 446 | bfd_get_status_str(status)); |
447 | } | |
448 | } | |
449 | ||
450 | zebra_interface_bfd_update(ifp, dp, sp, status, vrf_id); | |
c01acf98 DS |
451 | } |
452 | ||
d62a17ae | 453 | static int zebra_ptm_handle_bfd_msg(void *arg, void *in_ctxt, |
454 | struct interface *ifp) | |
244c1cdc | 455 | { |
d62a17ae | 456 | char bfdst_str[32]; |
457 | char dest_str[64]; | |
458 | char src_str[64]; | |
459 | char vrf_str[64]; | |
460 | struct prefix dest_prefix; | |
461 | struct prefix src_prefix; | |
462 | vrf_id_t vrf_id; | |
463 | ||
464 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSTATUS_STR, bfdst_str); | |
465 | ||
466 | if (bfdst_str[0] == '\0') { | |
467 | return -1; | |
468 | } | |
469 | ||
470 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDDEST_STR, dest_str); | |
471 | ||
472 | if (dest_str[0] == '\0') { | |
473 | zlog_debug("%s: Key %s not found in PTM msg", __func__, | |
474 | ZEBRA_PTM_BFDDEST_STR); | |
475 | return -1; | |
476 | } | |
477 | ||
478 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSRC_STR, src_str); | |
479 | ||
480 | if (src_str[0] == '\0') { | |
481 | zlog_debug("%s: Key %s not found in PTM msg", __func__, | |
482 | ZEBRA_PTM_BFDSRC_STR); | |
483 | return -1; | |
484 | } | |
485 | ||
486 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDVRF_STR, vrf_str); | |
487 | ||
488 | if (vrf_str[0] == '\0') { | |
489 | zlog_debug("%s: Key %s not found in PTM msg", __func__, | |
490 | ZEBRA_PTM_BFDVRF_STR); | |
491 | return -1; | |
492 | } | |
493 | ||
494 | if (IS_ZEBRA_DEBUG_EVENT) | |
495 | zlog_debug( | |
3efd0893 | 496 | "%s: Recv Port [%s] bfd status [%s] vrf [%s] peer [%s] local [%s]", |
d62a17ae | 497 | __func__, ifp ? ifp->name : "N/A", bfdst_str, vrf_str, |
498 | dest_str, src_str); | |
499 | ||
500 | if (str2prefix(dest_str, &dest_prefix) == 0) { | |
e914ccbe | 501 | flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR, |
1c50c1c0 | 502 | "%s: Peer addr %s not found", __func__, dest_str); |
d62a17ae | 503 | return -1; |
504 | } | |
505 | ||
6006b807 | 506 | memset(&src_prefix, 0, sizeof(src_prefix)); |
d62a17ae | 507 | if (strcmp(ZEBRA_PTM_INVALID_SRC_IP, src_str)) { |
508 | if (str2prefix(src_str, &src_prefix) == 0) { | |
e914ccbe | 509 | flog_err(EC_ZEBRA_PREFIX_PARSE_ERROR, |
1c50c1c0 QY |
510 | "%s: Local addr %s not found", __func__, |
511 | src_str); | |
d62a17ae | 512 | return -1; |
513 | } | |
514 | } | |
515 | ||
516 | if (!strcmp(ZEBRA_PTM_INVALID_VRF, vrf_str) && ifp) { | |
096f7609 | 517 | vrf_id = ifp->vrf->vrf_id; |
d62a17ae | 518 | } else { |
a383d4d2 PZ |
519 | struct vrf *pVrf; |
520 | ||
521 | pVrf = vrf_lookup_by_name(vrf_str); | |
522 | if (pVrf) | |
523 | vrf_id = pVrf->vrf_id; | |
524 | else | |
525 | vrf_id = VRF_DEFAULT; | |
d62a17ae | 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) | |
8b48cdb9 | 556 | if_up(ifp, true); |
d62a17ae | 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)) { | |
f13fdc67 IR |
608 | struct vrf *vrf; |
609 | int count = 0; | |
610 | ||
611 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { | |
77712f66 | 612 | ifp = if_lookup_by_name_vrf(port_str, vrf); |
f13fdc67 IR |
613 | if (ifp) { |
614 | count++; | |
615 | if (!vrf_is_backend_netns()) | |
616 | break; | |
617 | } | |
618 | } | |
d62a17ae | 619 | |
620 | if (!ifp) { | |
e914ccbe | 621 | flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE, |
9df414fe | 622 | "%s: %s not found in interface list", |
43e52561 | 623 | __func__, port_str); |
d62a17ae | 624 | return -1; |
625 | } | |
f13fdc67 IR |
626 | if (count > 1) { |
627 | flog_warn(EC_ZEBRA_UNKNOWN_INTERFACE, | |
628 | "%s: multiple interface with name %s", | |
629 | __func__, port_str); | |
630 | return -1; | |
631 | } | |
d62a17ae | 632 | } |
633 | ||
634 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CBL_STR, cbl_str); | |
635 | ||
636 | if (cbl_str[0] == '\0') { | |
637 | return zebra_ptm_handle_bfd_msg(arg, in_ctxt, ifp); | |
638 | } else { | |
639 | if (ifp) { | |
640 | return zebra_ptm_handle_cbl_msg(arg, in_ctxt, ifp, | |
641 | cbl_str); | |
642 | } else { | |
643 | return -1; | |
644 | } | |
645 | } | |
c01acf98 DS |
646 | } |
647 | ||
cc9f21da | 648 | void zebra_ptm_sock_read(struct thread *thread) |
c01acf98 | 649 | { |
2e1cc436 | 650 | int sock; |
d62a17ae | 651 | int rc; |
652 | ||
653 | errno = 0; | |
654 | sock = THREAD_FD(thread); | |
655 | ||
656 | if (sock == -1) | |
cc9f21da | 657 | return; |
d62a17ae | 658 | |
659 | /* PTM communicates in CSV format */ | |
2e1cc436 | 660 | do { |
d62a17ae | 661 | rc = ptm_lib_process_msg(ptm_hdl, sock, ptm_cb.in_data, |
662 | ZEBRA_PTM_MAX_SOCKBUF, NULL); | |
2e1cc436 | 663 | } while (rc > 0); |
d62a17ae | 664 | |
2e1cc436 A |
665 | if (((rc == 0) && !errno) |
666 | || (errno && (errno != EWOULDBLOCK) && (errno != EAGAIN))) { | |
450971aa | 667 | flog_err_sys(EC_LIB_SOCKET, |
9df414fe QY |
668 | "%s routing socket error: %s(%d) bytes %d", |
669 | __func__, safe_strerror(errno), errno, rc); | |
2e1cc436 A |
670 | |
671 | close(ptm_cb.ptm_sock); | |
672 | ptm_cb.ptm_sock = -1; | |
673 | zebra_ptm_reset_status(0); | |
674 | ptm_cb.t_timer = NULL; | |
3801e764 | 675 | thread_add_timer(zrouter.master, zebra_ptm_connect, NULL, |
2e1cc436 A |
676 | ptm_cb.reconnect_time, |
677 | &ptm_cb.t_timer); | |
cc9f21da | 678 | return; |
d62a17ae | 679 | } |
680 | ||
681 | ptm_cb.t_read = NULL; | |
3801e764 | 682 | thread_add_read(zrouter.master, zebra_ptm_sock_read, NULL, |
d62a17ae | 683 | ptm_cb.ptm_sock, &ptm_cb.t_read); |
c43ed2e4 DS |
684 | } |
685 | ||
686 | /* BFD peer/dst register/update */ | |
89f4e507 | 687 | void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS) |
c43ed2e4 | 688 | { |
d62a17ae | 689 | struct stream *s; |
690 | struct prefix src_p; | |
691 | struct prefix dst_p; | |
d7c0a89a QY |
692 | uint8_t multi_hop; |
693 | uint8_t multi_hop_cnt; | |
694 | uint8_t detect_mul; | |
d62a17ae | 695 | unsigned int min_rx_timer; |
696 | unsigned int min_tx_timer; | |
697 | char if_name[INTERFACE_NAMSIZ]; | |
d7c0a89a | 698 | uint8_t len; |
d62a17ae | 699 | void *out_ctxt; |
700 | char buf[INET6_ADDRSTRLEN]; | |
701 | char tmp_buf[64]; | |
702 | int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
703 | unsigned int pid; | |
9beff0bd | 704 | uint8_t cbit_set; |
d62a17ae | 705 | |
89f4e507 | 706 | if (hdr->command == ZEBRA_BFD_DEST_UPDATE) |
d62a17ae | 707 | client->bfd_peer_upd8_cnt++; |
708 | else | |
709 | client->bfd_peer_add_cnt++; | |
710 | ||
711 | if (IS_ZEBRA_DEBUG_EVENT) | |
712 | zlog_debug("bfd_dst_register msg from client %s: length=%d", | |
89f4e507 | 713 | zebra_route_string(client->proto), hdr->length); |
d62a17ae | 714 | |
715 | if (ptm_cb.ptm_sock == -1) { | |
716 | ptm_cb.t_timer = NULL; | |
3801e764 | 717 | thread_add_timer(zrouter.master, zebra_ptm_connect, NULL, |
d62a17ae | 718 | ptm_cb.reconnect_time, &ptm_cb.t_timer); |
8068a649 | 719 | return; |
d62a17ae | 720 | } |
721 | ||
722 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt); | |
772270f3 | 723 | snprintf(tmp_buf, sizeof(tmp_buf), "%s", ZEBRA_PTM_BFD_START_CMD); |
d62a17ae | 724 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf); |
772270f3 QY |
725 | snprintf(tmp_buf, sizeof(tmp_buf), "%s", |
726 | zebra_route_string(client->proto)); | |
d62a17ae | 727 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD, |
728 | tmp_buf); | |
729 | ||
1002497a | 730 | s = msg; |
d62a17ae | 731 | |
ec93aa12 | 732 | STREAM_GETL(s, pid); |
772270f3 | 733 | snprintf(tmp_buf, sizeof(tmp_buf), "%d", pid); |
d62a17ae | 734 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD, |
735 | tmp_buf); | |
736 | ||
ec93aa12 | 737 | STREAM_GETW(s, dst_p.family); |
d62a17ae | 738 | |
739 | if (dst_p.family == AF_INET) | |
740 | dst_p.prefixlen = IPV4_MAX_BYTELEN; | |
741 | else | |
742 | dst_p.prefixlen = IPV6_MAX_BYTELEN; | |
743 | ||
ec93aa12 | 744 | STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen); |
d62a17ae | 745 | if (dst_p.family == AF_INET) { |
746 | inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf)); | |
747 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
748 | ZEBRA_PTM_BFD_DST_IP_FIELD, buf); | |
749 | } else { | |
750 | inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf)); | |
751 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
752 | ZEBRA_PTM_BFD_DST_IP_FIELD, buf); | |
753 | } | |
754 | ||
ec93aa12 | 755 | STREAM_GETL(s, min_rx_timer); |
772270f3 | 756 | snprintf(tmp_buf, sizeof(tmp_buf), "%d", min_rx_timer); |
d62a17ae | 757 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_RX_FIELD, |
758 | tmp_buf); | |
ec93aa12 | 759 | STREAM_GETL(s, min_tx_timer); |
772270f3 | 760 | snprintf(tmp_buf, sizeof(tmp_buf), "%d", min_tx_timer); |
d62a17ae | 761 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_TX_FIELD, |
762 | tmp_buf); | |
ec93aa12 | 763 | STREAM_GETC(s, detect_mul); |
772270f3 | 764 | snprintf(tmp_buf, sizeof(tmp_buf), "%d", detect_mul); |
d62a17ae | 765 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DETECT_MULT_FIELD, |
766 | tmp_buf); | |
767 | ||
ec93aa12 | 768 | STREAM_GETC(s, multi_hop); |
d62a17ae | 769 | if (multi_hop) { |
772270f3 | 770 | snprintf(tmp_buf, sizeof(tmp_buf), "%d", 1); |
d62a17ae | 771 | ptm_lib_append_msg(ptm_hdl, out_ctxt, |
772 | ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf); | |
ec93aa12 | 773 | STREAM_GETW(s, src_p.family); |
d62a17ae | 774 | |
775 | if (src_p.family == AF_INET) | |
776 | src_p.prefixlen = IPV4_MAX_BYTELEN; | |
777 | else | |
778 | src_p.prefixlen = IPV6_MAX_BYTELEN; | |
779 | ||
ec93aa12 | 780 | STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen); |
d62a17ae | 781 | if (src_p.family == AF_INET) { |
782 | inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf)); | |
783 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
784 | ZEBRA_PTM_BFD_SRC_IP_FIELD, buf); | |
785 | } else { | |
786 | inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf)); | |
787 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
788 | ZEBRA_PTM_BFD_SRC_IP_FIELD, buf); | |
789 | } | |
790 | ||
ec93aa12 | 791 | STREAM_GETC(s, multi_hop_cnt); |
772270f3 | 792 | snprintf(tmp_buf, sizeof(tmp_buf), "%d", multi_hop_cnt); |
d62a17ae | 793 | ptm_lib_append_msg(ptm_hdl, out_ctxt, |
794 | ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD, tmp_buf); | |
795 | ||
796 | if (zvrf_id(zvrf) != VRF_DEFAULT) | |
797 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
798 | ZEBRA_PTM_BFD_VRF_NAME_FIELD, | |
799 | zvrf_name(zvrf)); | |
800 | } else { | |
801 | if (dst_p.family == AF_INET6) { | |
ec93aa12 | 802 | STREAM_GETW(s, src_p.family); |
d62a17ae | 803 | |
804 | if (src_p.family == AF_INET) | |
805 | src_p.prefixlen = IPV4_MAX_BYTELEN; | |
806 | else | |
807 | src_p.prefixlen = IPV6_MAX_BYTELEN; | |
808 | ||
ec93aa12 | 809 | STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen); |
d62a17ae | 810 | if (src_p.family == AF_INET) { |
811 | inet_ntop(AF_INET, &src_p.u.prefix4, buf, | |
812 | sizeof(buf)); | |
813 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
814 | ZEBRA_PTM_BFD_SRC_IP_FIELD, | |
815 | buf); | |
816 | } else { | |
817 | inet_ntop(AF_INET6, &src_p.u.prefix6, buf, | |
818 | sizeof(buf)); | |
819 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
820 | ZEBRA_PTM_BFD_SRC_IP_FIELD, | |
821 | buf); | |
822 | } | |
823 | } | |
ec93aa12 DS |
824 | STREAM_GETC(s, len); |
825 | STREAM_GET(if_name, s, len); | |
d62a17ae | 826 | if_name[len] = '\0'; |
827 | ||
828 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
829 | ZEBRA_PTM_BFD_IFNAME_FIELD, if_name); | |
830 | } | |
9beff0bd | 831 | STREAM_GETC(s, cbit_set); |
772270f3 | 832 | snprintf(tmp_buf, sizeof(tmp_buf), "%d", cbit_set); |
9beff0bd PG |
833 | ptm_lib_append_msg(ptm_hdl, out_ctxt, |
834 | ZEBRA_PTM_BFD_CBIT_FIELD, tmp_buf); | |
d62a17ae | 835 | |
772270f3 | 836 | snprintf(tmp_buf, sizeof(tmp_buf), "%d", 1); |
d62a17ae | 837 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEND_EVENT, |
838 | tmp_buf); | |
839 | ||
840 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len); | |
841 | ||
842 | if (IS_ZEBRA_DEBUG_SEND) | |
843 | zlog_debug("%s: Sent message (%d) %s", __func__, data_len, | |
844 | ptm_cb.out_data); | |
845 | zebra_ptm_send_message(ptm_cb.out_data, data_len); | |
ec93aa12 | 846 | |
8068a649 | 847 | return; |
9cc46248 | 848 | |
ec93aa12 | 849 | stream_failure: |
a928d464 | 850 | ptm_lib_cleanup_msg(ptm_hdl, out_ctxt); |
c43ed2e4 DS |
851 | } |
852 | ||
853 | /* BFD peer/dst deregister */ | |
89f4e507 | 854 | void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS) |
c43ed2e4 | 855 | { |
d62a17ae | 856 | struct stream *s; |
857 | struct prefix src_p; | |
858 | struct prefix dst_p; | |
d7c0a89a | 859 | uint8_t multi_hop; |
d62a17ae | 860 | char if_name[INTERFACE_NAMSIZ]; |
d7c0a89a | 861 | uint8_t len; |
d62a17ae | 862 | char buf[INET6_ADDRSTRLEN]; |
863 | char tmp_buf[64]; | |
864 | int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
865 | void *out_ctxt; | |
866 | unsigned int pid; | |
867 | ||
868 | client->bfd_peer_del_cnt++; | |
869 | ||
870 | if (IS_ZEBRA_DEBUG_EVENT) | |
871 | zlog_debug("bfd_dst_deregister msg from client %s: length=%d", | |
89f4e507 | 872 | zebra_route_string(client->proto), hdr->length); |
d62a17ae | 873 | |
874 | if (ptm_cb.ptm_sock == -1) { | |
875 | ptm_cb.t_timer = NULL; | |
3801e764 | 876 | thread_add_timer(zrouter.master, zebra_ptm_connect, NULL, |
d62a17ae | 877 | ptm_cb.reconnect_time, &ptm_cb.t_timer); |
8068a649 | 878 | return; |
d62a17ae | 879 | } |
880 | ||
881 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt); | |
882 | ||
772270f3 | 883 | snprintf(tmp_buf, sizeof(tmp_buf), "%s", ZEBRA_PTM_BFD_STOP_CMD); |
d62a17ae | 884 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf); |
885 | ||
772270f3 QY |
886 | snprintf(tmp_buf, sizeof(tmp_buf), "%s", |
887 | zebra_route_string(client->proto)); | |
d62a17ae | 888 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD, |
889 | tmp_buf); | |
890 | ||
1002497a | 891 | s = msg; |
d62a17ae | 892 | |
ec93aa12 | 893 | STREAM_GETL(s, pid); |
772270f3 | 894 | snprintf(tmp_buf, sizeof(tmp_buf), "%d", pid); |
d62a17ae | 895 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD, |
896 | tmp_buf); | |
897 | ||
ec93aa12 | 898 | STREAM_GETW(s, dst_p.family); |
d62a17ae | 899 | |
900 | if (dst_p.family == AF_INET) | |
901 | dst_p.prefixlen = IPV4_MAX_BYTELEN; | |
902 | else | |
903 | dst_p.prefixlen = IPV6_MAX_BYTELEN; | |
904 | ||
ec93aa12 | 905 | STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen); |
d62a17ae | 906 | if (dst_p.family == AF_INET) |
907 | inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf)); | |
908 | else | |
909 | inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf)); | |
910 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DST_IP_FIELD, buf); | |
911 | ||
912 | ||
ec93aa12 | 913 | STREAM_GETC(s, multi_hop); |
d62a17ae | 914 | if (multi_hop) { |
772270f3 | 915 | snprintf(tmp_buf, sizeof(tmp_buf), "%d", 1); |
d62a17ae | 916 | ptm_lib_append_msg(ptm_hdl, out_ctxt, |
917 | ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf); | |
918 | ||
ec93aa12 | 919 | STREAM_GETW(s, src_p.family); |
d62a17ae | 920 | |
921 | if (src_p.family == AF_INET) | |
922 | src_p.prefixlen = IPV4_MAX_BYTELEN; | |
923 | else | |
924 | src_p.prefixlen = IPV6_MAX_BYTELEN; | |
925 | ||
ec93aa12 | 926 | STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen); |
d62a17ae | 927 | if (src_p.family == AF_INET) |
928 | inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf)); | |
929 | else | |
930 | inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf)); | |
931 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
932 | ZEBRA_PTM_BFD_SRC_IP_FIELD, buf); | |
933 | ||
934 | if (zvrf_id(zvrf) != VRF_DEFAULT) | |
935 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
936 | ZEBRA_PTM_BFD_VRF_NAME_FIELD, | |
937 | zvrf_name(zvrf)); | |
938 | } else { | |
939 | if (dst_p.family == AF_INET6) { | |
ec93aa12 | 940 | STREAM_GETW(s, src_p.family); |
d62a17ae | 941 | |
942 | if (src_p.family == AF_INET) | |
943 | src_p.prefixlen = IPV4_MAX_BYTELEN; | |
944 | else | |
945 | src_p.prefixlen = IPV6_MAX_BYTELEN; | |
946 | ||
ec93aa12 | 947 | STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen); |
d62a17ae | 948 | if (src_p.family == AF_INET) { |
949 | inet_ntop(AF_INET, &src_p.u.prefix4, buf, | |
950 | sizeof(buf)); | |
951 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
952 | ZEBRA_PTM_BFD_SRC_IP_FIELD, | |
953 | buf); | |
954 | } else { | |
955 | inet_ntop(AF_INET6, &src_p.u.prefix6, buf, | |
956 | sizeof(buf)); | |
957 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
958 | ZEBRA_PTM_BFD_SRC_IP_FIELD, | |
959 | buf); | |
960 | } | |
961 | } | |
962 | ||
ec93aa12 DS |
963 | STREAM_GETC(s, len); |
964 | STREAM_GET(if_name, s, len); | |
d62a17ae | 965 | if_name[len] = '\0'; |
966 | ||
967 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
968 | ZEBRA_PTM_BFD_IFNAME_FIELD, if_name); | |
969 | } | |
970 | ||
971 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len); | |
972 | if (IS_ZEBRA_DEBUG_SEND) | |
973 | zlog_debug("%s: Sent message (%d) %s", __func__, data_len, | |
974 | ptm_cb.out_data); | |
975 | ||
976 | zebra_ptm_send_message(ptm_cb.out_data, data_len); | |
ec93aa12 | 977 | |
8068a649 | 978 | return; |
9cc46248 | 979 | |
ec93aa12 | 980 | stream_failure: |
a928d464 | 981 | ptm_lib_cleanup_msg(ptm_hdl, out_ctxt); |
c01acf98 | 982 | } |
c8ed14dd | 983 | |
055c4dfc | 984 | /* BFD client register */ |
89f4e507 | 985 | void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS) |
055c4dfc | 986 | { |
d62a17ae | 987 | struct stream *s; |
988 | unsigned int pid; | |
9cc46248 | 989 | void *out_ctxt = NULL; |
d62a17ae | 990 | char tmp_buf[64]; |
991 | int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
055c4dfc | 992 | |
d62a17ae | 993 | client->bfd_client_reg_cnt++; |
055c4dfc | 994 | |
d62a17ae | 995 | if (IS_ZEBRA_DEBUG_EVENT) |
996 | zlog_debug("bfd_client_register msg from client %s: length=%d", | |
89f4e507 | 997 | zebra_route_string(client->proto), hdr->length); |
055c4dfc | 998 | |
1002497a | 999 | s = msg; |
ec93aa12 | 1000 | STREAM_GETL(s, pid); |
bf6e101c | 1001 | |
d62a17ae | 1002 | if (ptm_cb.ptm_sock == -1) { |
1003 | ptm_cb.t_timer = NULL; | |
3801e764 | 1004 | thread_add_timer(zrouter.master, zebra_ptm_connect, NULL, |
d62a17ae | 1005 | ptm_cb.reconnect_time, &ptm_cb.t_timer); |
8068a649 | 1006 | return; |
d62a17ae | 1007 | } |
055c4dfc | 1008 | |
d62a17ae | 1009 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt); |
055c4dfc | 1010 | |
772270f3 | 1011 | snprintf(tmp_buf, sizeof(tmp_buf), "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD); |
d62a17ae | 1012 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf); |
055c4dfc | 1013 | |
772270f3 QY |
1014 | snprintf(tmp_buf, sizeof(tmp_buf), "%s", |
1015 | zebra_route_string(client->proto)); | |
d62a17ae | 1016 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD, |
1017 | tmp_buf); | |
055c4dfc | 1018 | |
772270f3 | 1019 | snprintf(tmp_buf, sizeof(tmp_buf), "%d", pid); |
d62a17ae | 1020 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD, |
1021 | tmp_buf); | |
055c4dfc | 1022 | |
d62a17ae | 1023 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len); |
055c4dfc | 1024 | |
d62a17ae | 1025 | if (IS_ZEBRA_DEBUG_SEND) |
1026 | zlog_debug("%s: Sent message (%d) %s", __func__, data_len, | |
1027 | ptm_cb.out_data); | |
1028 | zebra_ptm_send_message(ptm_cb.out_data, data_len); | |
2376c3f2 | 1029 | |
d62a17ae | 1030 | SET_FLAG(ptm_cb.client_flags[client->proto], |
1031 | ZEBRA_PTM_BFD_CLIENT_FLAG_REG); | |
9cc46248 | 1032 | |
8068a649 | 1033 | return; |
9cc46248 | 1034 | |
ec93aa12 | 1035 | stream_failure: |
6447dbb3 DS |
1036 | /* |
1037 | * IF we ever add more STREAM_GETXXX functions after the out_ctxt | |
1038 | * is allocated then we need to add this code back in | |
1039 | * | |
1040 | * if (out_ctxt) | |
1041 | * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt); | |
1042 | */ | |
8068a649 | 1043 | return; |
055c4dfc | 1044 | } |
1045 | ||
567b877d | 1046 | /* BFD client deregister */ |
453844ab | 1047 | int zebra_ptm_bfd_client_deregister(struct zserv *client) |
567b877d | 1048 | { |
453844ab | 1049 | uint8_t proto = client->proto; |
d62a17ae | 1050 | void *out_ctxt; |
1051 | char tmp_buf[64]; | |
1052 | int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
567b877d | 1053 | |
4943f243 | 1054 | if (!IS_BFD_ENABLED_PROTOCOL(proto)) |
453844ab | 1055 | return 0; |
567b877d | 1056 | |
d62a17ae | 1057 | if (IS_ZEBRA_DEBUG_EVENT) |
9df414fe QY |
1058 | zlog_debug("bfd_client_deregister msg for client %s", |
1059 | zebra_route_string(proto)); | |
567b877d | 1060 | |
d62a17ae | 1061 | if (ptm_cb.ptm_sock == -1) { |
1062 | ptm_cb.t_timer = NULL; | |
3801e764 | 1063 | thread_add_timer(zrouter.master, zebra_ptm_connect, NULL, |
d62a17ae | 1064 | ptm_cb.reconnect_time, &ptm_cb.t_timer); |
453844ab | 1065 | return 0; |
d62a17ae | 1066 | } |
567b877d | 1067 | |
d62a17ae | 1068 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt); |
567b877d | 1069 | |
772270f3 QY |
1070 | snprintf(tmp_buf, sizeof(tmp_buf), "%s", |
1071 | ZEBRA_PTM_BFD_CLIENT_DEREG_CMD); | |
d62a17ae | 1072 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf); |
567b877d | 1073 | |
772270f3 | 1074 | snprintf(tmp_buf, sizeof(tmp_buf), "%s", zebra_route_string(proto)); |
d62a17ae | 1075 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD, |
1076 | tmp_buf); | |
567b877d | 1077 | |
d62a17ae | 1078 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len); |
567b877d | 1079 | |
d62a17ae | 1080 | if (IS_ZEBRA_DEBUG_SEND) |
1081 | zlog_debug("%s: Sent message (%d) %s", __func__, data_len, | |
1082 | ptm_cb.out_data); | |
2376c3f2 | 1083 | |
d62a17ae | 1084 | zebra_ptm_send_message(ptm_cb.out_data, data_len); |
1085 | UNSET_FLAG(ptm_cb.client_flags[proto], ZEBRA_PTM_BFD_CLIENT_FLAG_REG); | |
453844ab QY |
1086 | |
1087 | return 0; | |
567b877d | 1088 | } |
1089 | ||
d62a17ae | 1090 | int zebra_ptm_get_enable_state(void) |
c8ed14dd | 1091 | { |
d62a17ae | 1092 | return ptm_cb.ptm_enable; |
c8ed14dd | 1093 | } |
950bd436 | 1094 | |
1095 | /* | |
1096 | * zebra_ptm_get_status_str - Convert status to a display string. | |
1097 | */ | |
d62a17ae | 1098 | static const char *zebra_ptm_get_status_str(int status) |
950bd436 | 1099 | { |
d62a17ae | 1100 | switch (status) { |
1101 | case ZEBRA_PTM_STATUS_DOWN: | |
1102 | return "fail"; | |
1103 | case ZEBRA_PTM_STATUS_UP: | |
1104 | return "pass"; | |
1105 | case ZEBRA_PTM_STATUS_UNKNOWN: | |
1106 | default: | |
1107 | return "n/a"; | |
1108 | } | |
950bd436 | 1109 | } |
1110 | ||
c15dc24f RW |
1111 | void zebra_ptm_show_status(struct vty *vty, json_object *json, |
1112 | struct interface *ifp) | |
950bd436 | 1113 | { |
c15dc24f RW |
1114 | const char *status; |
1115 | ||
1116 | if (ifp->ptm_enable) | |
1117 | status = zebra_ptm_get_status_str(ifp->ptm_status); | |
1118 | else | |
1119 | status = "disabled"; | |
1120 | ||
1121 | if (json) | |
1122 | json_object_string_add(json, "ptmStatus", status); | |
1123 | else | |
1124 | vty_out(vty, " PTM status: %s\n", status); | |
950bd436 | 1125 | } |
1126 | ||
d62a17ae | 1127 | void zebra_ptm_send_status_req(void) |
950bd436 | 1128 | { |
d62a17ae | 1129 | void *out_ctxt; |
1130 | int len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
1131 | ||
1132 | if (ptm_cb.ptm_enable) { | |
1133 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, | |
1134 | &out_ctxt); | |
1135 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, | |
1136 | ZEBRA_PTM_GET_STATUS_CMD); | |
1137 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &len); | |
1138 | ||
1139 | zebra_ptm_send_message(ptm_cb.out_data, len); | |
1140 | } | |
950bd436 | 1141 | } |
1142 | ||
d62a17ae | 1143 | void zebra_ptm_reset_status(int ptm_disable) |
950bd436 | 1144 | { |
d62a17ae | 1145 | struct vrf *vrf; |
d62a17ae | 1146 | struct interface *ifp; |
1147 | int send_linkup; | |
1148 | ||
a2addae8 | 1149 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) |
451fda4f | 1150 | FOR_ALL_INTERFACES (vrf, ifp) { |
a2addae8 RW |
1151 | send_linkup = 0; |
1152 | if (ifp->ptm_enable) { | |
1153 | if (!if_is_operative(ifp)) | |
1154 | send_linkup = 1; | |
1155 | ||
1156 | if (ptm_disable) | |
1157 | ifp->ptm_enable = | |
1158 | ZEBRA_IF_PTM_ENABLE_OFF; | |
1159 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; | |
1160 | ||
1161 | if (if_is_operative(ifp) && send_linkup) { | |
1162 | if (IS_ZEBRA_DEBUG_EVENT) | |
1163 | zlog_debug( | |
1164 | "%s: Bringing up interface %s", | |
1165 | __func__, ifp->name); | |
8b48cdb9 | 1166 | if_up(ifp, true); |
a2addae8 | 1167 | } |
d62a17ae | 1168 | } |
1169 | } | |
950bd436 | 1170 | } |
986aa00f | 1171 | |
d62a17ae | 1172 | void zebra_ptm_if_init(struct zebra_if *zebra_ifp) |
986aa00f | 1173 | { |
d62a17ae | 1174 | zebra_ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC; |
986aa00f | 1175 | } |
1176 | ||
d62a17ae | 1177 | void zebra_ptm_if_set_ptm_state(struct interface *ifp, |
1178 | struct zebra_if *zebra_ifp) | |
986aa00f | 1179 | { |
d62a17ae | 1180 | if (zebra_ifp && zebra_ifp->ptm_enable != ZEBRA_IF_PTM_ENABLE_UNSPEC) |
1181 | ifp->ptm_enable = zebra_ifp->ptm_enable; | |
986aa00f | 1182 | } |
1183 | ||
d62a17ae | 1184 | void zebra_ptm_if_write(struct vty *vty, struct zebra_if *zebra_ifp) |
986aa00f | 1185 | { |
d62a17ae | 1186 | if (zebra_ifp->ptm_enable == ZEBRA_IF_PTM_ENABLE_OFF) |
1187 | vty_out(vty, " no ptm-enable\n"); | |
986aa00f | 1188 | } |
d3af6147 RZ |
1189 | |
1190 | #else /* HAVE_BFDD */ | |
1191 | ||
d3af6147 RZ |
1192 | /* |
1193 | * Data structures. | |
1194 | */ | |
1195 | struct ptm_process { | |
1196 | struct zserv *pp_zs; | |
1197 | pid_t pp_pid; | |
1198 | ||
1199 | TAILQ_ENTRY(ptm_process) pp_entry; | |
1200 | }; | |
1201 | TAILQ_HEAD(ppqueue, ptm_process) ppqueue; | |
1202 | ||
1203 | DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_PTM_BFD_PROCESS, | |
1204 | "PTM BFD process registration table."); | |
1205 | ||
1206 | /* | |
1207 | * Prototypes. | |
1208 | */ | |
1209 | static struct ptm_process *pp_new(pid_t pid, struct zserv *zs); | |
1210 | static struct ptm_process *pp_lookup_byzs(struct zserv *zs); | |
1211 | static void pp_free(struct ptm_process *pp); | |
788378fe | 1212 | static void pp_free_all(void); |
d3af6147 RZ |
1213 | |
1214 | static void zebra_ptm_send_bfdd(struct stream *msg); | |
1215 | static void zebra_ptm_send_clients(struct stream *msg); | |
1216 | static int _zebra_ptm_bfd_client_deregister(struct zserv *zs); | |
45b000d0 PG |
1217 | static void _zebra_ptm_reroute(struct zserv *zs, struct zebra_vrf *zvrf, |
1218 | struct stream *msg, uint32_t command); | |
d3af6147 RZ |
1219 | |
1220 | ||
1221 | /* | |
1222 | * Process PID registration. | |
1223 | */ | |
1224 | static struct ptm_process *pp_new(pid_t pid, struct zserv *zs) | |
1225 | { | |
1226 | struct ptm_process *pp; | |
1227 | ||
1228 | #ifdef PTM_DEBUG | |
1229 | /* Sanity check: more than one client can't have the same PID. */ | |
1230 | TAILQ_FOREACH(pp, &ppqueue, pp_entry) { | |
1231 | if (pp->pp_pid == pid && pp->pp_zs != zs) | |
1232 | zlog_err("%s:%d pid and client pointer doesn't match", | |
1233 | __FILE__, __LINE__); | |
1234 | } | |
1235 | #endif /* PTM_DEBUG */ | |
1236 | ||
1237 | /* Lookup for duplicates. */ | |
1238 | pp = pp_lookup_byzs(zs); | |
1239 | if (pp != NULL) | |
1240 | return pp; | |
1241 | ||
1242 | /* Allocate and register new process. */ | |
1243 | pp = XCALLOC(MTYPE_ZEBRA_PTM_BFD_PROCESS, sizeof(*pp)); | |
d3af6147 RZ |
1244 | |
1245 | pp->pp_pid = pid; | |
1246 | pp->pp_zs = zs; | |
1247 | TAILQ_INSERT_HEAD(&ppqueue, pp, pp_entry); | |
1248 | ||
1249 | return pp; | |
1250 | } | |
1251 | ||
1252 | static struct ptm_process *pp_lookup_byzs(struct zserv *zs) | |
1253 | { | |
1254 | struct ptm_process *pp; | |
1255 | ||
1256 | TAILQ_FOREACH(pp, &ppqueue, pp_entry) { | |
1257 | if (pp->pp_zs != zs) | |
1258 | continue; | |
1259 | ||
1260 | break; | |
1261 | } | |
1262 | ||
1263 | return pp; | |
1264 | } | |
1265 | ||
1266 | static void pp_free(struct ptm_process *pp) | |
1267 | { | |
1268 | if (pp == NULL) | |
1269 | return; | |
1270 | ||
1271 | TAILQ_REMOVE(&ppqueue, pp, pp_entry); | |
1272 | XFREE(MTYPE_ZEBRA_PTM_BFD_PROCESS, pp); | |
1273 | } | |
1274 | ||
788378fe RZ |
1275 | static void pp_free_all(void) |
1276 | { | |
1277 | struct ptm_process *pp; | |
1278 | ||
1279 | while (!TAILQ_EMPTY(&ppqueue)) { | |
1280 | pp = TAILQ_FIRST(&ppqueue); | |
1281 | pp_free(pp); | |
1282 | } | |
1283 | } | |
1284 | ||
d3af6147 RZ |
1285 | |
1286 | /* | |
1287 | * Use the FRR's internal daemon implementation. | |
1288 | */ | |
1289 | static void zebra_ptm_send_bfdd(struct stream *msg) | |
1290 | { | |
1291 | struct listnode *node; | |
1292 | struct zserv *client; | |
1293 | struct stream *msgc; | |
1294 | ||
1295 | /* Create copy for replication. */ | |
1296 | msgc = stream_dup(msg); | |
d3af6147 RZ |
1297 | |
1298 | /* Send message to all running BFDd daemons. */ | |
161e9ab7 | 1299 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) { |
d3af6147 RZ |
1300 | if (client->proto != ZEBRA_ROUTE_BFD) |
1301 | continue; | |
1302 | ||
1303 | zserv_send_message(client, msg); | |
1304 | ||
1305 | /* Allocate more messages. */ | |
1306 | msg = stream_dup(msgc); | |
d3af6147 RZ |
1307 | } |
1308 | ||
1309 | stream_free(msgc); | |
011a7139 | 1310 | stream_free(msg); |
d3af6147 RZ |
1311 | } |
1312 | ||
1313 | static void zebra_ptm_send_clients(struct stream *msg) | |
1314 | { | |
1315 | struct listnode *node; | |
1316 | struct zserv *client; | |
1317 | struct stream *msgc; | |
1318 | ||
1319 | /* Create copy for replication. */ | |
1320 | msgc = stream_dup(msg); | |
d3af6147 RZ |
1321 | |
1322 | /* Send message to all running client daemons. */ | |
161e9ab7 | 1323 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) { |
4943f243 | 1324 | if (!IS_BFD_ENABLED_PROTOCOL(client->proto)) |
d3af6147 | 1325 | continue; |
d3af6147 RZ |
1326 | |
1327 | zserv_send_message(client, msg); | |
1328 | ||
1329 | /* Allocate more messages. */ | |
1330 | msg = stream_dup(msgc); | |
d3af6147 RZ |
1331 | } |
1332 | ||
1333 | stream_free(msgc); | |
011a7139 | 1334 | stream_free(msg); |
d3af6147 RZ |
1335 | } |
1336 | ||
1337 | static int _zebra_ptm_bfd_client_deregister(struct zserv *zs) | |
1338 | { | |
1339 | struct stream *msg; | |
1340 | struct ptm_process *pp; | |
1341 | ||
4943f243 | 1342 | if (!IS_BFD_ENABLED_PROTOCOL(zs->proto)) |
d3af6147 RZ |
1343 | return 0; |
1344 | ||
d3af6147 RZ |
1345 | /* Find daemon pid by zebra connection pointer. */ |
1346 | pp = pp_lookup_byzs(zs); | |
1347 | if (pp == NULL) { | |
1348 | zlog_err("%s:%d failed to find process pid registration", | |
1349 | __FILE__, __LINE__); | |
1350 | return -1; | |
1351 | } | |
1352 | ||
1353 | /* Generate, send message and free() daemon related data. */ | |
1354 | msg = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1355 | if (msg == NULL) { | |
9df414fe | 1356 | zlog_debug("%s: not enough memory", __func__); |
d3af6147 RZ |
1357 | return 0; |
1358 | } | |
1359 | ||
1360 | /* | |
5498a9f1 | 1361 | * The message type will be ZEBRA_BFD_DEST_REPLAY so we can use only |
d3af6147 RZ |
1362 | * one callback at the `bfdd` side, however the real command |
1363 | * number will be included right after the zebra header. | |
1364 | */ | |
1365 | zclient_create_header(msg, ZEBRA_BFD_DEST_REPLAY, 0); | |
1366 | stream_putl(msg, ZEBRA_BFD_CLIENT_DEREGISTER); | |
1367 | ||
1368 | /* Put process PID. */ | |
1369 | stream_putl(msg, pp->pp_pid); | |
1370 | ||
1371 | /* Update the data pointers. */ | |
1372 | stream_putw_at(msg, 0, stream_get_endp(msg)); | |
1373 | ||
1374 | zebra_ptm_send_bfdd(msg); | |
1375 | ||
1376 | pp_free(pp); | |
1377 | ||
1378 | return 0; | |
1379 | } | |
1380 | ||
1381 | void zebra_ptm_init(void) | |
1382 | { | |
1383 | /* Initialize the ptm process information list. */ | |
1384 | TAILQ_INIT(&ppqueue); | |
1385 | ||
1386 | /* | |
1387 | * Send deregistration messages to BFD daemon when some other | |
1388 | * daemon closes. This will help avoid sending daemons | |
1389 | * unnecessary notification messages. | |
1390 | */ | |
1391 | hook_register(zserv_client_close, _zebra_ptm_bfd_client_deregister); | |
1392 | } | |
1393 | ||
788378fe RZ |
1394 | void zebra_ptm_finish(void) |
1395 | { | |
1396 | /* Remove the client disconnect hook and free all memory. */ | |
1397 | hook_unregister(zserv_client_close, _zebra_ptm_bfd_client_deregister); | |
1398 | pp_free_all(); | |
1399 | } | |
1400 | ||
d3af6147 RZ |
1401 | |
1402 | /* | |
1403 | * Message handling. | |
1404 | */ | |
45b000d0 PG |
1405 | static void _zebra_ptm_reroute(struct zserv *zs, struct zebra_vrf *zvrf, |
1406 | struct stream *msg, uint32_t command) | |
d3af6147 RZ |
1407 | { |
1408 | struct stream *msgc; | |
ab12ca85 | 1409 | char buf[ZEBRA_MAX_PACKET_SIZ]; |
d3af6147 RZ |
1410 | pid_t ppid; |
1411 | ||
ab12ca85 | 1412 | /* Create BFD header */ |
d3af6147 | 1413 | msgc = stream_new(ZEBRA_MAX_PACKET_SIZ); |
45b000d0 | 1414 | zclient_create_header(msgc, ZEBRA_BFD_DEST_REPLAY, zvrf->vrf->vrf_id); |
d3af6147 RZ |
1415 | stream_putl(msgc, command); |
1416 | ||
ab12ca85 QY |
1417 | if (STREAM_READABLE(msg) > STREAM_WRITEABLE(msgc)) { |
1418 | zlog_warn("Cannot fit extended BFD header plus original message contents into ZAPI packet; dropping message"); | |
1419 | goto stream_failure; | |
1420 | } | |
1421 | ||
1422 | /* Copy original message, excluding header, into new message */ | |
1423 | stream_get_from(buf, msg, stream_get_getp(msg), STREAM_READABLE(msg)); | |
1424 | stream_put(msgc, buf, STREAM_READABLE(msg)); | |
1425 | ||
1426 | /* Update length field */ | |
1427 | stream_putw_at(msgc, 0, STREAM_READABLE(msgc)); | |
d3af6147 RZ |
1428 | |
1429 | zebra_ptm_send_bfdd(msgc); | |
011a7139 | 1430 | msgc = NULL; |
d3af6147 RZ |
1431 | |
1432 | /* Registrate process PID for shutdown hook. */ | |
1433 | STREAM_GETL(msg, ppid); | |
1434 | pp_new(ppid, zs); | |
1435 | ||
1436 | return; | |
1437 | ||
1438 | stream_failure: | |
011a7139 QY |
1439 | if (msgc) |
1440 | stream_free(msgc); | |
d3af6147 RZ |
1441 | zlog_err("%s:%d failed to registrate client pid", __FILE__, __LINE__); |
1442 | } | |
1443 | ||
1444 | void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS) | |
1445 | { | |
1446 | if (IS_ZEBRA_DEBUG_EVENT) | |
1447 | zlog_debug("bfd_dst_register msg from client %s: length=%d", | |
1448 | zebra_route_string(client->proto), hdr->length); | |
1449 | ||
45b000d0 | 1450 | _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_REGISTER); |
d3af6147 RZ |
1451 | } |
1452 | ||
1453 | void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS) | |
1454 | { | |
1455 | if (IS_ZEBRA_DEBUG_EVENT) | |
1456 | zlog_debug("bfd_dst_deregister msg from client %s: length=%d", | |
1457 | zebra_route_string(client->proto), hdr->length); | |
1458 | ||
45b000d0 | 1459 | _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_DEREGISTER); |
d3af6147 RZ |
1460 | } |
1461 | ||
1462 | void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS) | |
1463 | { | |
1464 | if (IS_ZEBRA_DEBUG_EVENT) | |
1465 | zlog_debug("bfd_client_register msg from client %s: length=%d", | |
1466 | zebra_route_string(client->proto), hdr->length); | |
1467 | ||
45b000d0 | 1468 | _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_CLIENT_REGISTER); |
d3af6147 RZ |
1469 | } |
1470 | ||
1471 | void zebra_ptm_bfd_dst_replay(ZAPI_HANDLER_ARGS) | |
1472 | { | |
1473 | struct stream *msgc; | |
1474 | size_t zmsglen, zhdrlen; | |
971532e2 | 1475 | uint32_t cmd; |
d3af6147 RZ |
1476 | |
1477 | /* | |
1478 | * NOTE: | |
1479 | * Replay messages with HAVE_BFDD are meant to be replayed to | |
1480 | * the client daemons. These messages are composed and | |
1481 | * originated from the `bfdd` daemon. | |
1482 | */ | |
1483 | if (IS_ZEBRA_DEBUG_EVENT) | |
1484 | zlog_debug("bfd_dst_update msg from client %s: length=%d", | |
1485 | zebra_route_string(client->proto), hdr->length); | |
1486 | ||
971532e2 RZ |
1487 | /* |
1488 | * Client messages must be re-routed, otherwise do the `bfdd` | |
1489 | * special treatment. | |
1490 | */ | |
1491 | if (client->proto != ZEBRA_ROUTE_BFD) { | |
45b000d0 | 1492 | _zebra_ptm_reroute(client, zvrf, msg, ZEBRA_BFD_DEST_REPLAY); |
971532e2 RZ |
1493 | return; |
1494 | } | |
1495 | ||
1496 | /* Figure out if this is an DEST_UPDATE or DEST_REPLAY. */ | |
1497 | if (stream_getl2(msg, &cmd) == false) { | |
1498 | zlog_err("%s: expected at least 4 bytes (command)", __func__); | |
1499 | return; | |
1500 | } | |
1501 | ||
d3af6147 RZ |
1502 | /* |
1503 | * Don't modify message in the zebra API. In order to do that we | |
1504 | * need to allocate a new message stream and copy the message | |
1505 | * provided by zebra. | |
1506 | */ | |
1507 | msgc = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
1508 | if (msgc == NULL) { | |
9df414fe | 1509 | zlog_debug("%s: not enough memory", __func__); |
d3af6147 RZ |
1510 | return; |
1511 | } | |
1512 | ||
1513 | /* Calculate our header size plus the message contents. */ | |
971532e2 RZ |
1514 | if (cmd != ZEBRA_BFD_DEST_REPLAY) { |
1515 | zhdrlen = ZEBRA_HEADER_SIZE; | |
1516 | zmsglen = msg->endp - msg->getp; | |
1517 | memcpy(msgc->data + zhdrlen, msg->data + msg->getp, zmsglen); | |
d3af6147 | 1518 | |
971532e2 RZ |
1519 | zclient_create_header(msgc, cmd, zvrf_id(zvrf)); |
1520 | ||
1521 | msgc->getp = 0; | |
1522 | msgc->endp = zhdrlen + zmsglen; | |
1523 | } else | |
1524 | zclient_create_header(msgc, cmd, zvrf_id(zvrf)); | |
d3af6147 RZ |
1525 | |
1526 | /* Update the data pointers. */ | |
d3af6147 RZ |
1527 | stream_putw_at(msgc, 0, stream_get_endp(msgc)); |
1528 | ||
1529 | zebra_ptm_send_clients(msgc); | |
1530 | } | |
1531 | ||
1532 | /* | |
1533 | * Unused functions. | |
1534 | */ | |
d3af6147 RZ |
1535 | void zebra_ptm_if_init(struct zebra_if *zifp __attribute__((__unused__))) |
1536 | { | |
1537 | /* NOTHING */ | |
1538 | } | |
1539 | ||
1540 | int zebra_ptm_get_enable_state(void) | |
1541 | { | |
5efdb801 | 1542 | return 0; |
d3af6147 RZ |
1543 | } |
1544 | ||
1545 | void zebra_ptm_show_status(struct vty *vty __attribute__((__unused__)), | |
c15dc24f | 1546 | json_object *json __attribute__((__unused__)), |
d3af6147 RZ |
1547 | struct interface *ifp __attribute__((__unused__))) |
1548 | { | |
1549 | /* NOTHING */ | |
1550 | } | |
1551 | ||
1552 | void zebra_ptm_write(struct vty *vty __attribute__((__unused__))) | |
1553 | { | |
1554 | /* NOTHING */ | |
1555 | } | |
1556 | ||
1557 | void zebra_ptm_if_write(struct vty *vty __attribute__((__unused__)), | |
1558 | struct zebra_if *zifp __attribute__((__unused__))) | |
1559 | { | |
1560 | /* NOTHING */ | |
1561 | } | |
1562 | void zebra_ptm_if_set_ptm_state(struct interface *i __attribute__((__unused__)), | |
1563 | struct zebra_if *zi __attribute__((__unused__))) | |
1564 | { | |
1565 | /* NOTHING */ | |
1566 | } | |
1567 | ||
1568 | #endif /* HAVE_BFDD */ |