]>
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> | |
d62a17ae | 22 | #include <sys/un.h> /* for sockaddr_un */ |
244c1cdc | 23 | #include <net/if.h> |
c8ed14dd | 24 | #include "vty.h" |
244c1cdc DS |
25 | #include "zebra/zserv.h" |
26 | #include "zebra/interface.h" | |
27 | #include "zebra/debug.h" | |
28 | #include "zebra/zebra_ptm.h" | |
29 | #include "if.h" | |
30 | #include "command.h" | |
c43ed2e4 DS |
31 | #include "stream.h" |
32 | #include "ptm_lib.h" | |
8f500a1c | 33 | #include "network.h" |
c8ed14dd | 34 | #include "buffer.h" |
c43ed2e4 | 35 | #include "zebra/zebra_ptm_redistribute.h" |
68fe91d6 | 36 | #include "bfd.h" |
d553294e | 37 | #include "vrf.h" |
7c551956 DS |
38 | #include "rib.h" |
39 | #include "zebra_vrf.h" | |
ae435b19 | 40 | #include "version.h" |
244c1cdc DS |
41 | |
42 | #define ZEBRA_PTM_RECONNECT_TIME_INITIAL 1 /* initial reconnect is 1s */ | |
43 | #define ZEBRA_PTM_RECONNECT_TIME_MAX 300 | |
c01acf98 | 44 | |
c01acf98 | 45 | #define PTM_MSG_LEN 4 |
0a56c844 | 46 | #define PTM_HEADER_LEN 37 |
c43ed2e4 DS |
47 | |
48 | const char ZEBRA_PTM_GET_STATUS_CMD[] = "get-status"; | |
49 | const char ZEBRA_PTM_BFD_START_CMD[] = "start-bfd-sess"; | |
50 | const char ZEBRA_PTM_BFD_STOP_CMD[] = "stop-bfd-sess"; | |
055c4dfc | 51 | const char ZEBRA_PTM_BFD_CLIENT_REG_CMD[] = "reg-bfd-client"; |
567b877d | 52 | const char ZEBRA_PTM_BFD_CLIENT_DEREG_CMD[] = "dereg-bfd-client"; |
c43ed2e4 | 53 | |
c8ed14dd | 54 | const char ZEBRA_PTM_CMD_STR[] = "cmd"; |
b255a4b1 | 55 | const char ZEBRA_PTM_CMD_STATUS_STR[] = "cmd_status"; |
c43ed2e4 DS |
56 | const char ZEBRA_PTM_PORT_STR[] = "port"; |
57 | const char ZEBRA_PTM_CBL_STR[] = "cbl status"; | |
58 | const char ZEBRA_PTM_PASS_STR[] = "pass"; | |
59 | const char ZEBRA_PTM_FAIL_STR[] = "fail"; | |
60 | const char ZEBRA_PTM_BFDSTATUS_STR[] = "state"; | |
61 | const char ZEBRA_PTM_BFDSTATUS_UP_STR[] = "Up"; | |
62 | const char ZEBRA_PTM_BFDSTATUS_DOWN_STR[] = "Down"; | |
63 | const char ZEBRA_PTM_BFDDEST_STR[] = "peer"; | |
64 | const char ZEBRA_PTM_BFDSRC_STR[] = "local"; | |
d553294e | 65 | const char ZEBRA_PTM_BFDVRF_STR[] = "vrf"; |
c43ed2e4 DS |
66 | const char ZEBRA_PTM_INVALID_PORT_NAME[] = "N/A"; |
67 | const char ZEBRA_PTM_INVALID_SRC_IP[] = "N/A"; | |
d553294e | 68 | const char ZEBRA_PTM_INVALID_VRF[] = "N/A"; |
c43ed2e4 DS |
69 | |
70 | const char ZEBRA_PTM_BFD_DST_IP_FIELD[] = "dstIPaddr"; | |
71 | const char ZEBRA_PTM_BFD_SRC_IP_FIELD[] = "srcIPaddr"; | |
72 | const char ZEBRA_PTM_BFD_MIN_RX_FIELD[] = "requiredMinRx"; | |
73 | const char ZEBRA_PTM_BFD_MIN_TX_FIELD[] = "upMinTx"; | |
74 | const char ZEBRA_PTM_BFD_DETECT_MULT_FIELD[] = "detectMult"; | |
75 | const char ZEBRA_PTM_BFD_MULTI_HOP_FIELD[] = "multiHop"; | |
76 | const char ZEBRA_PTM_BFD_CLIENT_FIELD[] = "client"; | |
77 | const char ZEBRA_PTM_BFD_SEQID_FIELD[] = "seqid"; | |
78 | const char ZEBRA_PTM_BFD_IFNAME_FIELD[] = "ifName"; | |
79 | const char ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD[] = "maxHopCnt"; | |
68fe91d6 | 80 | const char ZEBRA_PTM_BFD_SEND_EVENT[] = "sendEvent"; |
d553294e | 81 | const char ZEBRA_PTM_BFD_VRF_NAME_FIELD[] = "vrfName"; |
c01acf98 | 82 | |
c43ed2e4 | 83 | static ptm_lib_handle_t *ptm_hdl; |
244c1cdc | 84 | |
c8ed14dd DS |
85 | struct zebra_ptm_cb ptm_cb; |
86 | ||
244c1cdc | 87 | static int zebra_ptm_socket_init(void); |
244c1cdc | 88 | int zebra_ptm_sock_read(struct thread *); |
d62a17ae | 89 | static void zebra_ptm_install_commands(void); |
b255a4b1 | 90 | static int zebra_ptm_handle_msg_cb(void *arg, void *in_ctxt); |
d62a17ae | 91 | void zebra_bfd_peer_replay_req(void); |
950bd436 | 92 | void zebra_ptm_send_status_req(void); |
93 | void zebra_ptm_reset_status(int ptm_disable); | |
244c1cdc DS |
94 | |
95 | const char ZEBRA_PTM_SOCK_NAME[] = "\0/var/run/ptmd.socket"; | |
96 | ||
d62a17ae | 97 | void zebra_ptm_init(void) |
244c1cdc | 98 | { |
d62a17ae | 99 | char buf[64]; |
c43ed2e4 | 100 | |
d62a17ae | 101 | memset(&ptm_cb, 0, sizeof(struct zebra_ptm_cb)); |
c8ed14dd | 102 | |
d62a17ae | 103 | ptm_cb.out_data = calloc(1, ZEBRA_PTM_SEND_MAX_SOCKBUF); |
104 | if (!ptm_cb.out_data) { | |
105 | zlog_warn("%s: Allocation of send data failed", __func__); | |
106 | return; | |
107 | } | |
c8ed14dd | 108 | |
d62a17ae | 109 | ptm_cb.in_data = calloc(1, ZEBRA_PTM_MAX_SOCKBUF); |
110 | if (!ptm_cb.in_data) { | |
111 | zlog_warn("%s: Allocation of recv data failed", __func__); | |
112 | free(ptm_cb.out_data); | |
113 | return; | |
114 | } | |
c8ed14dd | 115 | |
d62a17ae | 116 | ptm_cb.pid = getpid(); |
117 | zebra_ptm_install_commands(); | |
c43ed2e4 | 118 | |
d62a17ae | 119 | sprintf(buf, "%s", FRR_PTM_NAME); |
120 | ptm_hdl = ptm_lib_register(buf, NULL, zebra_ptm_handle_msg_cb, | |
121 | zebra_ptm_handle_msg_cb); | |
122 | ptm_cb.wb = buffer_new(0); | |
c8ed14dd | 123 | |
d62a17ae | 124 | ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL; |
c8ed14dd | 125 | |
d62a17ae | 126 | ptm_cb.ptm_sock = -1; |
c8ed14dd DS |
127 | } |
128 | ||
d62a17ae | 129 | void zebra_ptm_finish(void) |
c8ed14dd | 130 | { |
d62a17ae | 131 | int proto; |
c8ed14dd | 132 | |
d62a17ae | 133 | for (proto = 0; proto < ZEBRA_ROUTE_MAX; proto++) |
134 | if (CHECK_FLAG(ptm_cb.client_flags[proto], | |
135 | ZEBRA_PTM_BFD_CLIENT_FLAG_REG)) | |
136 | zebra_ptm_bfd_client_deregister(proto); | |
2376c3f2 | 137 | |
d62a17ae | 138 | buffer_flush_all(ptm_cb.wb, ptm_cb.ptm_sock); |
c8ed14dd | 139 | |
d62a17ae | 140 | free(ptm_hdl); |
58ac32e2 | 141 | |
d62a17ae | 142 | if (ptm_cb.out_data) |
143 | free(ptm_cb.out_data); | |
c8ed14dd | 144 | |
d62a17ae | 145 | if (ptm_cb.in_data) |
146 | free(ptm_cb.in_data); | |
c8ed14dd | 147 | |
d62a17ae | 148 | /* Release threads. */ |
149 | if (ptm_cb.t_read) | |
150 | thread_cancel(ptm_cb.t_read); | |
151 | if (ptm_cb.t_write) | |
152 | thread_cancel(ptm_cb.t_write); | |
153 | if (ptm_cb.t_timer) | |
154 | thread_cancel(ptm_cb.t_timer); | |
2376c3f2 | 155 | |
d62a17ae | 156 | if (ptm_cb.wb) |
157 | buffer_free(ptm_cb.wb); | |
2376c3f2 | 158 | |
1e9f448f | 159 | if (ptm_cb.ptm_sock >= 0) |
d62a17ae | 160 | close(ptm_cb.ptm_sock); |
c8ed14dd DS |
161 | } |
162 | ||
d62a17ae | 163 | static int zebra_ptm_flush_messages(struct thread *thread) |
c8ed14dd | 164 | { |
d62a17ae | 165 | ptm_cb.t_write = NULL; |
166 | ||
167 | if (ptm_cb.ptm_sock == -1) | |
168 | return -1; | |
169 | ||
170 | errno = 0; | |
171 | ||
172 | switch (buffer_flush_available(ptm_cb.wb, ptm_cb.ptm_sock)) { | |
173 | case BUFFER_ERROR: | |
174 | zlog_warn("%s ptm socket error: %s", __func__, | |
175 | safe_strerror(errno)); | |
176 | close(ptm_cb.ptm_sock); | |
177 | ptm_cb.ptm_sock = -1; | |
178 | zebra_ptm_reset_status(0); | |
179 | ptm_cb.t_timer = NULL; | |
180 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
181 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
182 | return (-1); | |
183 | case BUFFER_PENDING: | |
184 | ptm_cb.t_write = NULL; | |
185 | thread_add_write(zebrad.master, zebra_ptm_flush_messages, NULL, | |
186 | ptm_cb.ptm_sock, &ptm_cb.t_write); | |
187 | break; | |
188 | case BUFFER_EMPTY: | |
189 | break; | |
190 | } | |
191 | ||
192 | return (0); | |
c8ed14dd DS |
193 | } |
194 | ||
d62a17ae | 195 | static int zebra_ptm_send_message(char *data, int size) |
c8ed14dd | 196 | { |
d62a17ae | 197 | errno = 0; |
198 | switch (buffer_write(ptm_cb.wb, ptm_cb.ptm_sock, data, size)) { | |
199 | case BUFFER_ERROR: | |
200 | zlog_warn("%s ptm socket error: %s", __func__, | |
201 | safe_strerror(errno)); | |
202 | close(ptm_cb.ptm_sock); | |
203 | ptm_cb.ptm_sock = -1; | |
204 | zebra_ptm_reset_status(0); | |
205 | ptm_cb.t_timer = NULL; | |
206 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
207 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
208 | return -1; | |
209 | case BUFFER_EMPTY: | |
210 | THREAD_OFF(ptm_cb.t_write); | |
211 | break; | |
212 | case BUFFER_PENDING: | |
213 | thread_add_write(zebrad.master, zebra_ptm_flush_messages, NULL, | |
214 | ptm_cb.ptm_sock, &ptm_cb.t_write); | |
215 | break; | |
216 | } | |
217 | ||
218 | return 0; | |
244c1cdc DS |
219 | } |
220 | ||
d62a17ae | 221 | int zebra_ptm_connect(struct thread *t) |
244c1cdc | 222 | { |
d62a17ae | 223 | int init = 0; |
224 | ||
225 | if (ptm_cb.ptm_sock == -1) { | |
226 | zebra_ptm_socket_init(); | |
227 | init = 1; | |
228 | } | |
229 | ||
230 | if (ptm_cb.ptm_sock != -1) { | |
231 | if (init) { | |
232 | ptm_cb.t_read = NULL; | |
233 | thread_add_read(zebrad.master, zebra_ptm_sock_read, | |
234 | NULL, ptm_cb.ptm_sock, &ptm_cb.t_read); | |
235 | zebra_bfd_peer_replay_req(); | |
236 | } | |
237 | zebra_ptm_send_status_req(); | |
238 | ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL; | |
239 | } else if (ptm_cb.reconnect_time < ZEBRA_PTM_RECONNECT_TIME_MAX) { | |
240 | ptm_cb.reconnect_time *= 2; | |
241 | if (ptm_cb.reconnect_time > ZEBRA_PTM_RECONNECT_TIME_MAX) | |
242 | ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_MAX; | |
243 | ||
244 | ptm_cb.t_timer = NULL; | |
245 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
246 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
247 | } else if (ptm_cb.reconnect_time >= ZEBRA_PTM_RECONNECT_TIME_MAX) { | |
248 | ptm_cb.reconnect_time = ZEBRA_PTM_RECONNECT_TIME_INITIAL; | |
249 | } | |
250 | ||
251 | return (errno); | |
244c1cdc DS |
252 | } |
253 | ||
244c1cdc DS |
254 | DEFUN (zebra_ptm_enable, |
255 | zebra_ptm_enable_cmd, | |
256 | "ptm-enable", | |
257 | "Enable neighbor check with specified topology\n") | |
258 | { | |
d62a17ae | 259 | struct vrf *vrf; |
d62a17ae | 260 | struct interface *ifp; |
261 | struct zebra_if *if_data; | |
262 | ||
263 | ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_ON; | |
264 | ||
a2addae8 | 265 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) |
451fda4f | 266 | FOR_ALL_INTERFACES (vrf, ifp) |
a2addae8 RW |
267 | if (!ifp->ptm_enable) { |
268 | if_data = (struct zebra_if *)ifp->info; | |
269 | if (if_data | |
270 | && (if_data->ptm_enable | |
9d303b37 | 271 | == ZEBRA_IF_PTM_ENABLE_UNSPEC)) { |
a2addae8 RW |
272 | ifp->ptm_enable = |
273 | ZEBRA_IF_PTM_ENABLE_ON; | |
274 | } | |
275 | /* Assign a default unknown status */ | |
276 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; | |
d62a17ae | 277 | } |
d62a17ae | 278 | |
279 | zebra_ptm_connect(NULL); | |
280 | ||
281 | return CMD_SUCCESS; | |
244c1cdc DS |
282 | } |
283 | ||
284 | DEFUN (no_zebra_ptm_enable, | |
285 | no_zebra_ptm_enable_cmd, | |
286 | "no ptm-enable", | |
287 | NO_STR | |
288 | "Enable neighbor check with specified topology\n") | |
289 | { | |
d62a17ae | 290 | ptm_cb.ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF; |
291 | zebra_ptm_reset_status(1); | |
292 | return CMD_SUCCESS; | |
244c1cdc DS |
293 | } |
294 | ||
986aa00f | 295 | DEFUN (zebra_ptm_enable_if, |
296 | zebra_ptm_enable_if_cmd, | |
297 | "ptm-enable", | |
298 | "Enable neighbor check with specified topology\n") | |
299 | { | |
d62a17ae | 300 | VTY_DECLVAR_CONTEXT(interface, ifp); |
301 | struct zebra_if *if_data; | |
302 | int old_ptm_enable; | |
303 | int send_linkdown = 0; | |
304 | ||
305 | if (ifp->ifindex == IFINDEX_INTERNAL) { | |
306 | return CMD_SUCCESS; | |
307 | } | |
308 | ||
309 | old_ptm_enable = ifp->ptm_enable; | |
310 | ifp->ptm_enable = ptm_cb.ptm_enable; | |
311 | ||
312 | if (if_is_no_ptm_operative(ifp)) | |
313 | send_linkdown = 1; | |
314 | ||
315 | if (!old_ptm_enable && ptm_cb.ptm_enable) { | |
316 | if (!if_is_operative(ifp) && send_linkdown) { | |
317 | if (IS_ZEBRA_DEBUG_EVENT) | |
318 | zlog_debug("%s: Bringing down interface %s\n", | |
319 | __func__, ifp->name); | |
320 | if_down(ifp); | |
321 | } | |
322 | } | |
323 | ||
324 | if_data = ifp->info; | |
325 | if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC; | |
326 | ||
327 | return CMD_SUCCESS; | |
986aa00f | 328 | } |
329 | ||
330 | DEFUN (no_zebra_ptm_enable_if, | |
331 | no_zebra_ptm_enable_if_cmd, | |
332 | "no ptm-enable", | |
333 | NO_STR | |
334 | "Enable neighbor check with specified topology\n") | |
335 | { | |
d62a17ae | 336 | VTY_DECLVAR_CONTEXT(interface, ifp); |
337 | int send_linkup = 0; | |
338 | struct zebra_if *if_data; | |
339 | ||
340 | if ((ifp->ifindex != IFINDEX_INTERNAL) && (ifp->ptm_enable)) { | |
341 | if (!if_is_operative(ifp)) | |
342 | send_linkup = 1; | |
343 | ||
344 | ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF; | |
345 | if (if_is_no_ptm_operative(ifp) && send_linkup) { | |
346 | if (IS_ZEBRA_DEBUG_EVENT) | |
347 | zlog_debug("%s: Bringing up interface %s\n", | |
348 | __func__, ifp->name); | |
349 | if_up(ifp); | |
350 | } | |
351 | } | |
352 | ||
353 | if_data = ifp->info; | |
354 | if_data->ptm_enable = ZEBRA_IF_PTM_ENABLE_OFF; | |
355 | ||
356 | return CMD_SUCCESS; | |
986aa00f | 357 | } |
358 | ||
359 | ||
d62a17ae | 360 | void zebra_ptm_write(struct vty *vty) |
244c1cdc | 361 | { |
d62a17ae | 362 | if (ptm_cb.ptm_enable) |
363 | vty_out(vty, "ptm-enable\n"); | |
244c1cdc | 364 | |
d62a17ae | 365 | return; |
244c1cdc DS |
366 | } |
367 | ||
d62a17ae | 368 | static int zebra_ptm_socket_init(void) |
244c1cdc | 369 | { |
d62a17ae | 370 | int ret; |
371 | int sock; | |
372 | struct sockaddr_un addr; | |
373 | ||
374 | ptm_cb.ptm_sock = -1; | |
375 | ||
376 | sock = socket(PF_UNIX, SOCK_STREAM, 0); | |
377 | if (sock < 0) | |
378 | return -1; | |
379 | if (set_nonblocking(sock) < 0) { | |
380 | if (IS_ZEBRA_DEBUG_EVENT) | |
381 | zlog_debug("%s: Unable to set socket non blocking[%s]", | |
382 | __PRETTY_FUNCTION__, safe_strerror(errno)); | |
383 | close(sock); | |
384 | return -1; | |
385 | } | |
386 | ||
387 | /* Make server socket. */ | |
388 | memset(&addr, 0, sizeof(struct sockaddr_un)); | |
389 | addr.sun_family = AF_UNIX; | |
390 | memcpy(&addr.sun_path, ZEBRA_PTM_SOCK_NAME, | |
391 | sizeof(ZEBRA_PTM_SOCK_NAME)); | |
392 | ||
393 | ret = connect(sock, (struct sockaddr *)&addr, | |
394 | sizeof(addr.sun_family) + sizeof(ZEBRA_PTM_SOCK_NAME) | |
395 | - 1); | |
396 | if (ret < 0) { | |
397 | if (IS_ZEBRA_DEBUG_EVENT) | |
398 | zlog_debug("%s: Unable to connect to socket %s [%s]", | |
399 | __func__, ZEBRA_PTM_SOCK_NAME, | |
400 | safe_strerror(errno)); | |
401 | close(sock); | |
402 | return -1; | |
403 | } | |
404 | ptm_cb.ptm_sock = sock; | |
405 | return sock; | |
244c1cdc DS |
406 | } |
407 | ||
d62a17ae | 408 | static void zebra_ptm_install_commands(void) |
244c1cdc | 409 | { |
d62a17ae | 410 | install_element(CONFIG_NODE, &zebra_ptm_enable_cmd); |
411 | install_element(CONFIG_NODE, &no_zebra_ptm_enable_cmd); | |
412 | install_element(INTERFACE_NODE, &zebra_ptm_enable_if_cmd); | |
413 | install_element(INTERFACE_NODE, &no_zebra_ptm_enable_if_cmd); | |
244c1cdc DS |
414 | } |
415 | ||
c43ed2e4 | 416 | /* BFD session goes down, send message to the protocols. */ |
d62a17ae | 417 | static void if_bfd_session_update(struct interface *ifp, struct prefix *dp, |
418 | struct prefix *sp, int status, | |
419 | vrf_id_t vrf_id) | |
c01acf98 | 420 | { |
d62a17ae | 421 | if (IS_ZEBRA_DEBUG_EVENT) { |
422 | char buf[2][INET6_ADDRSTRLEN]; | |
423 | ||
424 | if (ifp) { | |
425 | zlog_debug( | |
426 | "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d on %s" | |
427 | " %s event", | |
428 | inet_ntop(dp->family, &dp->u.prefix, buf[0], | |
429 | INET6_ADDRSTRLEN), | |
430 | dp->prefixlen, ifp->name, | |
431 | bfd_get_status_str(status)); | |
432 | } else { | |
433 | zlog_debug( | |
434 | "MESSAGE: ZEBRA_INTERFACE_BFD_DEST_UPDATE %s/%d " | |
a9ff90c4 | 435 | "with src %s/%d and vrf %u %s event", |
d62a17ae | 436 | inet_ntop(dp->family, &dp->u.prefix, buf[0], |
437 | INET6_ADDRSTRLEN), | |
438 | dp->prefixlen, | |
439 | inet_ntop(sp->family, &sp->u.prefix, buf[1], | |
440 | INET6_ADDRSTRLEN), | |
441 | sp->prefixlen, vrf_id, | |
442 | bfd_get_status_str(status)); | |
443 | } | |
444 | } | |
445 | ||
446 | zebra_interface_bfd_update(ifp, dp, sp, status, vrf_id); | |
c01acf98 DS |
447 | } |
448 | ||
d62a17ae | 449 | static int zebra_ptm_handle_bfd_msg(void *arg, void *in_ctxt, |
450 | struct interface *ifp) | |
244c1cdc | 451 | { |
d62a17ae | 452 | char bfdst_str[32]; |
453 | char dest_str[64]; | |
454 | char src_str[64]; | |
455 | char vrf_str[64]; | |
456 | struct prefix dest_prefix; | |
457 | struct prefix src_prefix; | |
458 | vrf_id_t vrf_id; | |
459 | ||
460 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSTATUS_STR, bfdst_str); | |
461 | ||
462 | if (bfdst_str[0] == '\0') { | |
463 | return -1; | |
464 | } | |
465 | ||
466 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDDEST_STR, dest_str); | |
467 | ||
468 | if (dest_str[0] == '\0') { | |
469 | zlog_debug("%s: Key %s not found in PTM msg", __func__, | |
470 | ZEBRA_PTM_BFDDEST_STR); | |
471 | return -1; | |
472 | } | |
473 | ||
474 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDSRC_STR, src_str); | |
475 | ||
476 | if (src_str[0] == '\0') { | |
477 | zlog_debug("%s: Key %s not found in PTM msg", __func__, | |
478 | ZEBRA_PTM_BFDSRC_STR); | |
479 | return -1; | |
480 | } | |
481 | ||
482 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_BFDVRF_STR, vrf_str); | |
483 | ||
484 | if (vrf_str[0] == '\0') { | |
485 | zlog_debug("%s: Key %s not found in PTM msg", __func__, | |
486 | ZEBRA_PTM_BFDVRF_STR); | |
487 | return -1; | |
488 | } | |
489 | ||
490 | if (IS_ZEBRA_DEBUG_EVENT) | |
491 | zlog_debug( | |
492 | "%s: Recv Port [%s] bfd status [%s] vrf [%s]" | |
493 | " peer [%s] local [%s]", | |
494 | __func__, ifp ? ifp->name : "N/A", bfdst_str, vrf_str, | |
495 | dest_str, src_str); | |
496 | ||
497 | if (str2prefix(dest_str, &dest_prefix) == 0) { | |
498 | zlog_err("%s: Peer addr %s not found", __func__, dest_str); | |
499 | return -1; | |
500 | } | |
501 | ||
502 | memset(&src_prefix, 0, sizeof(struct prefix)); | |
503 | if (strcmp(ZEBRA_PTM_INVALID_SRC_IP, src_str)) { | |
504 | if (str2prefix(src_str, &src_prefix) == 0) { | |
505 | zlog_err("%s: Local addr %s not found", __func__, | |
506 | src_str); | |
507 | return -1; | |
508 | } | |
509 | } | |
510 | ||
511 | if (!strcmp(ZEBRA_PTM_INVALID_VRF, vrf_str) && ifp) { | |
512 | vrf_id = ifp->vrf_id; | |
513 | } else { | |
514 | vrf_id = vrf_name_to_id(vrf_str); | |
515 | } | |
516 | ||
517 | if (!strcmp(bfdst_str, ZEBRA_PTM_BFDSTATUS_DOWN_STR)) { | |
518 | if_bfd_session_update(ifp, &dest_prefix, &src_prefix, | |
519 | BFD_STATUS_DOWN, vrf_id); | |
520 | } else { | |
521 | if_bfd_session_update(ifp, &dest_prefix, &src_prefix, | |
522 | BFD_STATUS_UP, vrf_id); | |
523 | } | |
524 | ||
525 | return 0; | |
c01acf98 DS |
526 | } |
527 | ||
d62a17ae | 528 | static int zebra_ptm_handle_cbl_msg(void *arg, void *in_ctxt, |
529 | struct interface *ifp, char *cbl_str) | |
c01acf98 | 530 | { |
d62a17ae | 531 | int send_linkup = 0; |
532 | ||
533 | if (IS_ZEBRA_DEBUG_EVENT) | |
534 | zlog_debug("%s: Recv Port [%s] cbl status [%s]", __func__, | |
535 | ifp->name, cbl_str); | |
536 | ||
537 | if (!strcmp(cbl_str, ZEBRA_PTM_PASS_STR) | |
538 | && (ifp->ptm_status != ZEBRA_PTM_STATUS_UP)) { | |
539 | ||
540 | if (ifp->ptm_status == ZEBRA_PTM_STATUS_DOWN) | |
541 | send_linkup = 1; | |
542 | ifp->ptm_status = ZEBRA_PTM_STATUS_UP; | |
543 | if (ifp->ptm_enable && if_is_no_ptm_operative(ifp) | |
544 | && send_linkup) | |
545 | if_up(ifp); | |
546 | } else if (!strcmp(cbl_str, ZEBRA_PTM_FAIL_STR) | |
547 | && (ifp->ptm_status != ZEBRA_PTM_STATUS_DOWN)) { | |
548 | ifp->ptm_status = ZEBRA_PTM_STATUS_DOWN; | |
549 | if (ifp->ptm_enable && if_is_no_ptm_operative(ifp)) | |
550 | if_down(ifp); | |
551 | } | |
552 | ||
553 | return 0; | |
b255a4b1 | 554 | } |
950bd436 | 555 | |
b255a4b1 | 556 | /* |
557 | * zebra_ptm_handle_msg_cb - The purpose of this callback function is to handle | |
558 | * all the command responses and notifications received from PTM. | |
559 | * | |
560 | * Command responses: Upon establishing connection with PTM, Zebra requests | |
561 | * status of all interfaces using 'get-status' command if global ptm-enable | |
562 | * knob is enabled. As a response to the get-status command PTM sends status | |
563 | * of all the interfaces as command responses. All other type of command | |
564 | * responses with cmd_status key word are dropped. The sole purpose of | |
565 | * registering this function as callback for the command responses is to | |
566 | * handle the responses to get-status command. | |
567 | * | |
568 | * Notifications: Cable status and BFD session status changes are sent as | |
569 | * notifications by PTM. So, this function is also the callback function for | |
570 | * processing all the notifications from the PTM. | |
571 | * | |
572 | */ | |
d62a17ae | 573 | static int zebra_ptm_handle_msg_cb(void *arg, void *in_ctxt) |
b255a4b1 | 574 | { |
d62a17ae | 575 | struct interface *ifp = NULL; |
576 | char port_str[128]; | |
577 | char cbl_str[32]; | |
578 | char cmd_status_str[32]; | |
579 | ||
580 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CMD_STATUS_STR, | |
581 | cmd_status_str); | |
582 | ||
583 | /* Drop command response messages */ | |
584 | if (cmd_status_str[0] != '\0') { | |
585 | return 0; | |
586 | } | |
587 | ||
588 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_PORT_STR, port_str); | |
589 | ||
590 | if (port_str[0] == '\0') { | |
591 | zlog_debug("%s: Key %s not found in PTM msg", __func__, | |
592 | ZEBRA_PTM_PORT_STR); | |
593 | return -1; | |
594 | } | |
595 | ||
596 | if (strcmp(ZEBRA_PTM_INVALID_PORT_NAME, port_str)) { | |
597 | ifp = if_lookup_by_name_all_vrf(port_str); | |
598 | ||
599 | if (!ifp) { | |
600 | zlog_err("%s: %s not found in interface list", __func__, | |
601 | port_str); | |
602 | return -1; | |
603 | } | |
604 | } | |
605 | ||
606 | ptm_lib_find_key_in_msg(in_ctxt, ZEBRA_PTM_CBL_STR, cbl_str); | |
607 | ||
608 | if (cbl_str[0] == '\0') { | |
609 | return zebra_ptm_handle_bfd_msg(arg, in_ctxt, ifp); | |
610 | } else { | |
611 | if (ifp) { | |
612 | return zebra_ptm_handle_cbl_msg(arg, in_ctxt, ifp, | |
613 | cbl_str); | |
614 | } else { | |
615 | return -1; | |
616 | } | |
617 | } | |
c01acf98 DS |
618 | } |
619 | ||
d62a17ae | 620 | int zebra_ptm_sock_read(struct thread *thread) |
c01acf98 | 621 | { |
d62a17ae | 622 | int sock, done = 0; |
623 | int rc; | |
624 | ||
625 | errno = 0; | |
626 | sock = THREAD_FD(thread); | |
627 | ||
628 | if (sock == -1) | |
629 | return -1; | |
630 | ||
631 | /* PTM communicates in CSV format */ | |
632 | while (!done) { | |
633 | rc = ptm_lib_process_msg(ptm_hdl, sock, ptm_cb.in_data, | |
634 | ZEBRA_PTM_MAX_SOCKBUF, NULL); | |
635 | if (rc <= 0) | |
636 | break; | |
637 | } | |
638 | ||
639 | if (rc <= 0) { | |
640 | if (((rc == 0) && !errno) | |
641 | || (errno && (errno != EWOULDBLOCK) && (errno != EAGAIN))) { | |
642 | zlog_warn("%s routing socket error: %s(%d) bytes %d", | |
643 | __func__, safe_strerror(errno), errno, rc); | |
644 | ||
645 | close(ptm_cb.ptm_sock); | |
646 | ptm_cb.ptm_sock = -1; | |
647 | zebra_ptm_reset_status(0); | |
648 | ptm_cb.t_timer = NULL; | |
649 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
650 | ptm_cb.reconnect_time, | |
651 | &ptm_cb.t_timer); | |
652 | return (-1); | |
653 | } | |
654 | } | |
655 | ||
656 | ptm_cb.t_read = NULL; | |
657 | thread_add_read(zebrad.master, zebra_ptm_sock_read, NULL, | |
658 | ptm_cb.ptm_sock, &ptm_cb.t_read); | |
659 | ||
660 | return 0; | |
c43ed2e4 DS |
661 | } |
662 | ||
663 | /* BFD peer/dst register/update */ | |
89f4e507 | 664 | void zebra_ptm_bfd_dst_register(ZAPI_HANDLER_ARGS) |
c43ed2e4 | 665 | { |
d62a17ae | 666 | struct stream *s; |
667 | struct prefix src_p; | |
668 | struct prefix dst_p; | |
669 | u_char multi_hop; | |
670 | u_char multi_hop_cnt; | |
671 | u_char detect_mul; | |
672 | unsigned int min_rx_timer; | |
673 | unsigned int min_tx_timer; | |
674 | char if_name[INTERFACE_NAMSIZ]; | |
675 | u_char len; | |
676 | void *out_ctxt; | |
677 | char buf[INET6_ADDRSTRLEN]; | |
678 | char tmp_buf[64]; | |
679 | int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
680 | unsigned int pid; | |
681 | ||
89f4e507 | 682 | if (hdr->command == ZEBRA_BFD_DEST_UPDATE) |
d62a17ae | 683 | client->bfd_peer_upd8_cnt++; |
684 | else | |
685 | client->bfd_peer_add_cnt++; | |
686 | ||
687 | if (IS_ZEBRA_DEBUG_EVENT) | |
688 | zlog_debug("bfd_dst_register msg from client %s: length=%d", | |
89f4e507 | 689 | zebra_route_string(client->proto), hdr->length); |
d62a17ae | 690 | |
691 | if (ptm_cb.ptm_sock == -1) { | |
692 | ptm_cb.t_timer = NULL; | |
693 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
694 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
8068a649 | 695 | return; |
d62a17ae | 696 | } |
697 | ||
698 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt); | |
699 | sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_START_CMD); | |
700 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf); | |
701 | sprintf(tmp_buf, "%s", zebra_route_string(client->proto)); | |
702 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD, | |
703 | tmp_buf); | |
704 | ||
1002497a | 705 | s = msg; |
d62a17ae | 706 | |
ec93aa12 | 707 | STREAM_GETL(s, pid); |
d62a17ae | 708 | sprintf(tmp_buf, "%d", pid); |
709 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD, | |
710 | tmp_buf); | |
711 | ||
ec93aa12 | 712 | STREAM_GETW(s, dst_p.family); |
d62a17ae | 713 | |
714 | if (dst_p.family == AF_INET) | |
715 | dst_p.prefixlen = IPV4_MAX_BYTELEN; | |
716 | else | |
717 | dst_p.prefixlen = IPV6_MAX_BYTELEN; | |
718 | ||
ec93aa12 | 719 | STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen); |
d62a17ae | 720 | if (dst_p.family == AF_INET) { |
721 | inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf)); | |
722 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
723 | ZEBRA_PTM_BFD_DST_IP_FIELD, buf); | |
724 | } else { | |
725 | inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf)); | |
726 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
727 | ZEBRA_PTM_BFD_DST_IP_FIELD, buf); | |
728 | } | |
729 | ||
ec93aa12 | 730 | STREAM_GETL(s, min_rx_timer); |
d62a17ae | 731 | sprintf(tmp_buf, "%d", min_rx_timer); |
732 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_RX_FIELD, | |
733 | tmp_buf); | |
ec93aa12 | 734 | STREAM_GETL(s, min_tx_timer); |
d62a17ae | 735 | sprintf(tmp_buf, "%d", min_tx_timer); |
736 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_MIN_TX_FIELD, | |
737 | tmp_buf); | |
ec93aa12 | 738 | STREAM_GETC(s, detect_mul); |
d62a17ae | 739 | sprintf(tmp_buf, "%d", detect_mul); |
740 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DETECT_MULT_FIELD, | |
741 | tmp_buf); | |
742 | ||
ec93aa12 | 743 | STREAM_GETC(s, multi_hop); |
d62a17ae | 744 | if (multi_hop) { |
745 | sprintf(tmp_buf, "%d", 1); | |
746 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
747 | ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf); | |
ec93aa12 | 748 | STREAM_GETW(s, src_p.family); |
d62a17ae | 749 | |
750 | if (src_p.family == AF_INET) | |
751 | src_p.prefixlen = IPV4_MAX_BYTELEN; | |
752 | else | |
753 | src_p.prefixlen = IPV6_MAX_BYTELEN; | |
754 | ||
ec93aa12 | 755 | STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen); |
d62a17ae | 756 | if (src_p.family == AF_INET) { |
757 | inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf)); | |
758 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
759 | ZEBRA_PTM_BFD_SRC_IP_FIELD, buf); | |
760 | } else { | |
761 | inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf)); | |
762 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
763 | ZEBRA_PTM_BFD_SRC_IP_FIELD, buf); | |
764 | } | |
765 | ||
ec93aa12 | 766 | STREAM_GETC(s, multi_hop_cnt); |
d62a17ae | 767 | sprintf(tmp_buf, "%d", multi_hop_cnt); |
768 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
769 | ZEBRA_PTM_BFD_MAX_HOP_CNT_FIELD, tmp_buf); | |
770 | ||
771 | if (zvrf_id(zvrf) != VRF_DEFAULT) | |
772 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
773 | ZEBRA_PTM_BFD_VRF_NAME_FIELD, | |
774 | zvrf_name(zvrf)); | |
775 | } else { | |
776 | if (dst_p.family == AF_INET6) { | |
ec93aa12 | 777 | STREAM_GETW(s, src_p.family); |
d62a17ae | 778 | |
779 | if (src_p.family == AF_INET) | |
780 | src_p.prefixlen = IPV4_MAX_BYTELEN; | |
781 | else | |
782 | src_p.prefixlen = IPV6_MAX_BYTELEN; | |
783 | ||
ec93aa12 | 784 | STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen); |
d62a17ae | 785 | if (src_p.family == AF_INET) { |
786 | inet_ntop(AF_INET, &src_p.u.prefix4, buf, | |
787 | sizeof(buf)); | |
788 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
789 | ZEBRA_PTM_BFD_SRC_IP_FIELD, | |
790 | buf); | |
791 | } else { | |
792 | inet_ntop(AF_INET6, &src_p.u.prefix6, buf, | |
793 | sizeof(buf)); | |
794 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
795 | ZEBRA_PTM_BFD_SRC_IP_FIELD, | |
796 | buf); | |
797 | } | |
798 | } | |
ec93aa12 DS |
799 | STREAM_GETC(s, len); |
800 | STREAM_GET(if_name, s, len); | |
d62a17ae | 801 | if_name[len] = '\0'; |
802 | ||
803 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
804 | ZEBRA_PTM_BFD_IFNAME_FIELD, if_name); | |
805 | } | |
806 | ||
807 | sprintf(tmp_buf, "%d", 1); | |
808 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEND_EVENT, | |
809 | tmp_buf); | |
810 | ||
811 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len); | |
812 | ||
813 | if (IS_ZEBRA_DEBUG_SEND) | |
814 | zlog_debug("%s: Sent message (%d) %s", __func__, data_len, | |
815 | ptm_cb.out_data); | |
816 | zebra_ptm_send_message(ptm_cb.out_data, data_len); | |
ec93aa12 | 817 | |
8068a649 | 818 | return; |
9cc46248 | 819 | |
ec93aa12 | 820 | stream_failure: |
a928d464 | 821 | ptm_lib_cleanup_msg(ptm_hdl, out_ctxt); |
c43ed2e4 DS |
822 | } |
823 | ||
824 | /* BFD peer/dst deregister */ | |
89f4e507 | 825 | void zebra_ptm_bfd_dst_deregister(ZAPI_HANDLER_ARGS) |
c43ed2e4 | 826 | { |
d62a17ae | 827 | struct stream *s; |
828 | struct prefix src_p; | |
829 | struct prefix dst_p; | |
830 | u_char multi_hop; | |
831 | char if_name[INTERFACE_NAMSIZ]; | |
832 | u_char len; | |
833 | char buf[INET6_ADDRSTRLEN]; | |
834 | char tmp_buf[64]; | |
835 | int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
836 | void *out_ctxt; | |
837 | unsigned int pid; | |
838 | ||
839 | client->bfd_peer_del_cnt++; | |
840 | ||
841 | if (IS_ZEBRA_DEBUG_EVENT) | |
842 | zlog_debug("bfd_dst_deregister msg from client %s: length=%d", | |
89f4e507 | 843 | zebra_route_string(client->proto), hdr->length); |
d62a17ae | 844 | |
845 | if (ptm_cb.ptm_sock == -1) { | |
846 | ptm_cb.t_timer = NULL; | |
847 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
848 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
8068a649 | 849 | return; |
d62a17ae | 850 | } |
851 | ||
852 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt); | |
853 | ||
854 | sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_STOP_CMD); | |
855 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf); | |
856 | ||
857 | sprintf(tmp_buf, "%s", zebra_route_string(client->proto)); | |
858 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD, | |
859 | tmp_buf); | |
860 | ||
1002497a | 861 | s = msg; |
d62a17ae | 862 | |
ec93aa12 | 863 | STREAM_GETL(s, pid); |
d62a17ae | 864 | sprintf(tmp_buf, "%d", pid); |
865 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD, | |
866 | tmp_buf); | |
867 | ||
ec93aa12 | 868 | STREAM_GETW(s, dst_p.family); |
d62a17ae | 869 | |
870 | if (dst_p.family == AF_INET) | |
871 | dst_p.prefixlen = IPV4_MAX_BYTELEN; | |
872 | else | |
873 | dst_p.prefixlen = IPV6_MAX_BYTELEN; | |
874 | ||
ec93aa12 | 875 | STREAM_GET(&dst_p.u.prefix, s, dst_p.prefixlen); |
d62a17ae | 876 | if (dst_p.family == AF_INET) |
877 | inet_ntop(AF_INET, &dst_p.u.prefix4, buf, sizeof(buf)); | |
878 | else | |
879 | inet_ntop(AF_INET6, &dst_p.u.prefix6, buf, sizeof(buf)); | |
880 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_DST_IP_FIELD, buf); | |
881 | ||
882 | ||
ec93aa12 | 883 | STREAM_GETC(s, multi_hop); |
d62a17ae | 884 | if (multi_hop) { |
885 | sprintf(tmp_buf, "%d", 1); | |
886 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
887 | ZEBRA_PTM_BFD_MULTI_HOP_FIELD, tmp_buf); | |
888 | ||
ec93aa12 | 889 | STREAM_GETW(s, src_p.family); |
d62a17ae | 890 | |
891 | if (src_p.family == AF_INET) | |
892 | src_p.prefixlen = IPV4_MAX_BYTELEN; | |
893 | else | |
894 | src_p.prefixlen = IPV6_MAX_BYTELEN; | |
895 | ||
ec93aa12 | 896 | STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen); |
d62a17ae | 897 | if (src_p.family == AF_INET) |
898 | inet_ntop(AF_INET, &src_p.u.prefix4, buf, sizeof(buf)); | |
899 | else | |
900 | inet_ntop(AF_INET6, &src_p.u.prefix6, buf, sizeof(buf)); | |
901 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
902 | ZEBRA_PTM_BFD_SRC_IP_FIELD, buf); | |
903 | ||
904 | if (zvrf_id(zvrf) != VRF_DEFAULT) | |
905 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
906 | ZEBRA_PTM_BFD_VRF_NAME_FIELD, | |
907 | zvrf_name(zvrf)); | |
908 | } else { | |
909 | if (dst_p.family == AF_INET6) { | |
ec93aa12 | 910 | STREAM_GETW(s, src_p.family); |
d62a17ae | 911 | |
912 | if (src_p.family == AF_INET) | |
913 | src_p.prefixlen = IPV4_MAX_BYTELEN; | |
914 | else | |
915 | src_p.prefixlen = IPV6_MAX_BYTELEN; | |
916 | ||
ec93aa12 | 917 | STREAM_GET(&src_p.u.prefix, s, src_p.prefixlen); |
d62a17ae | 918 | if (src_p.family == AF_INET) { |
919 | inet_ntop(AF_INET, &src_p.u.prefix4, buf, | |
920 | sizeof(buf)); | |
921 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
922 | ZEBRA_PTM_BFD_SRC_IP_FIELD, | |
923 | buf); | |
924 | } else { | |
925 | inet_ntop(AF_INET6, &src_p.u.prefix6, buf, | |
926 | sizeof(buf)); | |
927 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
928 | ZEBRA_PTM_BFD_SRC_IP_FIELD, | |
929 | buf); | |
930 | } | |
931 | } | |
932 | ||
ec93aa12 DS |
933 | STREAM_GETC(s, len); |
934 | STREAM_GET(if_name, s, len); | |
d62a17ae | 935 | if_name[len] = '\0'; |
936 | ||
937 | ptm_lib_append_msg(ptm_hdl, out_ctxt, | |
938 | ZEBRA_PTM_BFD_IFNAME_FIELD, if_name); | |
939 | } | |
940 | ||
941 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len); | |
942 | if (IS_ZEBRA_DEBUG_SEND) | |
943 | zlog_debug("%s: Sent message (%d) %s", __func__, data_len, | |
944 | ptm_cb.out_data); | |
945 | ||
946 | zebra_ptm_send_message(ptm_cb.out_data, data_len); | |
ec93aa12 | 947 | |
8068a649 | 948 | return; |
9cc46248 | 949 | |
ec93aa12 | 950 | stream_failure: |
a928d464 | 951 | ptm_lib_cleanup_msg(ptm_hdl, out_ctxt); |
c01acf98 | 952 | } |
c8ed14dd | 953 | |
055c4dfc | 954 | /* BFD client register */ |
89f4e507 | 955 | void zebra_ptm_bfd_client_register(ZAPI_HANDLER_ARGS) |
055c4dfc | 956 | { |
d62a17ae | 957 | struct stream *s; |
958 | unsigned int pid; | |
9cc46248 | 959 | void *out_ctxt = NULL; |
d62a17ae | 960 | char tmp_buf[64]; |
961 | int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
055c4dfc | 962 | |
d62a17ae | 963 | client->bfd_client_reg_cnt++; |
055c4dfc | 964 | |
d62a17ae | 965 | if (IS_ZEBRA_DEBUG_EVENT) |
966 | zlog_debug("bfd_client_register msg from client %s: length=%d", | |
89f4e507 | 967 | zebra_route_string(client->proto), hdr->length); |
055c4dfc | 968 | |
1002497a | 969 | s = msg; |
ec93aa12 | 970 | STREAM_GETL(s, pid); |
bf6e101c | 971 | |
d62a17ae | 972 | if (ptm_cb.ptm_sock == -1) { |
973 | ptm_cb.t_timer = NULL; | |
974 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
975 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
8068a649 | 976 | return; |
d62a17ae | 977 | } |
055c4dfc | 978 | |
d62a17ae | 979 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt); |
055c4dfc | 980 | |
d62a17ae | 981 | sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_REG_CMD); |
982 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf); | |
055c4dfc | 983 | |
d62a17ae | 984 | sprintf(tmp_buf, "%s", zebra_route_string(client->proto)); |
985 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD, | |
986 | tmp_buf); | |
055c4dfc | 987 | |
d62a17ae | 988 | sprintf(tmp_buf, "%d", pid); |
989 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_SEQID_FIELD, | |
990 | tmp_buf); | |
055c4dfc | 991 | |
d62a17ae | 992 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len); |
055c4dfc | 993 | |
d62a17ae | 994 | if (IS_ZEBRA_DEBUG_SEND) |
995 | zlog_debug("%s: Sent message (%d) %s", __func__, data_len, | |
996 | ptm_cb.out_data); | |
997 | zebra_ptm_send_message(ptm_cb.out_data, data_len); | |
2376c3f2 | 998 | |
d62a17ae | 999 | SET_FLAG(ptm_cb.client_flags[client->proto], |
1000 | ZEBRA_PTM_BFD_CLIENT_FLAG_REG); | |
9cc46248 | 1001 | |
8068a649 | 1002 | return; |
9cc46248 | 1003 | |
ec93aa12 | 1004 | stream_failure: |
6447dbb3 DS |
1005 | /* |
1006 | * IF we ever add more STREAM_GETXXX functions after the out_ctxt | |
1007 | * is allocated then we need to add this code back in | |
1008 | * | |
1009 | * if (out_ctxt) | |
1010 | * ptm_lib_cleanup_msg(ptm_hdl, out_ctxt); | |
1011 | */ | |
8068a649 | 1012 | return; |
055c4dfc | 1013 | } |
1014 | ||
567b877d | 1015 | /* BFD client deregister */ |
d62a17ae | 1016 | void zebra_ptm_bfd_client_deregister(int proto) |
567b877d | 1017 | { |
d62a17ae | 1018 | void *out_ctxt; |
1019 | char tmp_buf[64]; | |
1020 | int data_len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
567b877d | 1021 | |
d62a17ae | 1022 | if (proto != ZEBRA_ROUTE_OSPF && proto != ZEBRA_ROUTE_BGP |
1023 | && proto != ZEBRA_ROUTE_OSPF6 && proto != ZEBRA_ROUTE_PIM) | |
1024 | return; | |
567b877d | 1025 | |
d62a17ae | 1026 | if (IS_ZEBRA_DEBUG_EVENT) |
1027 | zlog_err("bfd_client_deregister msg for client %s", | |
1028 | zebra_route_string(proto)); | |
567b877d | 1029 | |
d62a17ae | 1030 | if (ptm_cb.ptm_sock == -1) { |
1031 | ptm_cb.t_timer = NULL; | |
1032 | thread_add_timer(zebrad.master, zebra_ptm_connect, NULL, | |
1033 | ptm_cb.reconnect_time, &ptm_cb.t_timer); | |
1034 | return; | |
1035 | } | |
567b877d | 1036 | |
d62a17ae | 1037 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, &out_ctxt); |
567b877d | 1038 | |
d62a17ae | 1039 | sprintf(tmp_buf, "%s", ZEBRA_PTM_BFD_CLIENT_DEREG_CMD); |
1040 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, tmp_buf); | |
567b877d | 1041 | |
d62a17ae | 1042 | sprintf(tmp_buf, "%s", zebra_route_string(proto)); |
1043 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_BFD_CLIENT_FIELD, | |
1044 | tmp_buf); | |
567b877d | 1045 | |
d62a17ae | 1046 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &data_len); |
567b877d | 1047 | |
d62a17ae | 1048 | if (IS_ZEBRA_DEBUG_SEND) |
1049 | zlog_debug("%s: Sent message (%d) %s", __func__, data_len, | |
1050 | ptm_cb.out_data); | |
2376c3f2 | 1051 | |
d62a17ae | 1052 | zebra_ptm_send_message(ptm_cb.out_data, data_len); |
1053 | UNSET_FLAG(ptm_cb.client_flags[proto], ZEBRA_PTM_BFD_CLIENT_FLAG_REG); | |
567b877d | 1054 | } |
1055 | ||
d62a17ae | 1056 | int zebra_ptm_get_enable_state(void) |
c8ed14dd | 1057 | { |
d62a17ae | 1058 | return ptm_cb.ptm_enable; |
c8ed14dd | 1059 | } |
950bd436 | 1060 | |
1061 | /* | |
1062 | * zebra_ptm_get_status_str - Convert status to a display string. | |
1063 | */ | |
d62a17ae | 1064 | static const char *zebra_ptm_get_status_str(int status) |
950bd436 | 1065 | { |
d62a17ae | 1066 | switch (status) { |
1067 | case ZEBRA_PTM_STATUS_DOWN: | |
1068 | return "fail"; | |
1069 | case ZEBRA_PTM_STATUS_UP: | |
1070 | return "pass"; | |
1071 | case ZEBRA_PTM_STATUS_UNKNOWN: | |
1072 | default: | |
1073 | return "n/a"; | |
1074 | } | |
950bd436 | 1075 | } |
1076 | ||
d62a17ae | 1077 | void zebra_ptm_show_status(struct vty *vty, struct interface *ifp) |
950bd436 | 1078 | { |
d62a17ae | 1079 | vty_out(vty, " PTM status: "); |
1080 | if (ifp->ptm_enable) { | |
1081 | vty_out(vty, "%s\n", zebra_ptm_get_status_str(ifp->ptm_status)); | |
1082 | } else { | |
1083 | vty_out(vty, "disabled\n"); | |
1084 | } | |
950bd436 | 1085 | } |
1086 | ||
d62a17ae | 1087 | void zebra_ptm_send_status_req(void) |
950bd436 | 1088 | { |
d62a17ae | 1089 | void *out_ctxt; |
1090 | int len = ZEBRA_PTM_SEND_MAX_SOCKBUF; | |
1091 | ||
1092 | if (ptm_cb.ptm_enable) { | |
1093 | ptm_lib_init_msg(ptm_hdl, 0, PTMLIB_MSG_TYPE_CMD, NULL, | |
1094 | &out_ctxt); | |
1095 | ptm_lib_append_msg(ptm_hdl, out_ctxt, ZEBRA_PTM_CMD_STR, | |
1096 | ZEBRA_PTM_GET_STATUS_CMD); | |
1097 | ptm_lib_complete_msg(ptm_hdl, out_ctxt, ptm_cb.out_data, &len); | |
1098 | ||
1099 | zebra_ptm_send_message(ptm_cb.out_data, len); | |
1100 | } | |
950bd436 | 1101 | } |
1102 | ||
d62a17ae | 1103 | void zebra_ptm_reset_status(int ptm_disable) |
950bd436 | 1104 | { |
d62a17ae | 1105 | struct vrf *vrf; |
d62a17ae | 1106 | struct interface *ifp; |
1107 | int send_linkup; | |
1108 | ||
a2addae8 | 1109 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) |
451fda4f | 1110 | FOR_ALL_INTERFACES (vrf, ifp) { |
a2addae8 RW |
1111 | send_linkup = 0; |
1112 | if (ifp->ptm_enable) { | |
1113 | if (!if_is_operative(ifp)) | |
1114 | send_linkup = 1; | |
1115 | ||
1116 | if (ptm_disable) | |
1117 | ifp->ptm_enable = | |
1118 | ZEBRA_IF_PTM_ENABLE_OFF; | |
1119 | ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN; | |
1120 | ||
1121 | if (if_is_operative(ifp) && send_linkup) { | |
1122 | if (IS_ZEBRA_DEBUG_EVENT) | |
1123 | zlog_debug( | |
1124 | "%s: Bringing up interface %s", | |
1125 | __func__, ifp->name); | |
1126 | if_up(ifp); | |
1127 | } | |
d62a17ae | 1128 | } |
1129 | } | |
950bd436 | 1130 | } |
986aa00f | 1131 | |
d62a17ae | 1132 | void zebra_ptm_if_init(struct zebra_if *zebra_ifp) |
986aa00f | 1133 | { |
d62a17ae | 1134 | zebra_ifp->ptm_enable = ZEBRA_IF_PTM_ENABLE_UNSPEC; |
986aa00f | 1135 | } |
1136 | ||
d62a17ae | 1137 | void zebra_ptm_if_set_ptm_state(struct interface *ifp, |
1138 | struct zebra_if *zebra_ifp) | |
986aa00f | 1139 | { |
d62a17ae | 1140 | if (zebra_ifp && zebra_ifp->ptm_enable != ZEBRA_IF_PTM_ENABLE_UNSPEC) |
1141 | ifp->ptm_enable = zebra_ifp->ptm_enable; | |
986aa00f | 1142 | } |
1143 | ||
d62a17ae | 1144 | void zebra_ptm_if_write(struct vty *vty, struct zebra_if *zebra_ifp) |
986aa00f | 1145 | { |
d62a17ae | 1146 | if (zebra_ifp->ptm_enable == ZEBRA_IF_PTM_ENABLE_OFF) |
1147 | vty_out(vty, " no ptm-enable\n"); | |
986aa00f | 1148 | } |