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