Skip to content

Commit 5d3d1b7

Browse files
committed
pimd: refactor pim_sock_read to be used with unicast bsr msgs
Signed-off-by: Jafar Al-Gharaibeh <[email protected]>
1 parent 52a344d commit 5d3d1b7

File tree

3 files changed

+42
-92
lines changed

3 files changed

+42
-92
lines changed

pimd/pim_bsm.c

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -258,66 +258,11 @@ static inline void pim_bs_timer_restart(struct bsm_scope *scope, int bs_timeout)
258258
static void bsm_unicast_sock_read(struct event *t)
259259
{
260260
struct bsm_scope *scope = EVENT_ARG(t);
261-
struct sockaddr_storage from;
262-
struct sockaddr_storage to;
263-
socklen_t fromlen = sizeof(from);
264-
socklen_t tolen = sizeof(to);
265-
ifindex_t ifindex = 0;
266-
struct interface *ifp;
267-
uint8_t buf[PIM_PIM_BUFSIZE_READ];
268-
int len, i;
261+
262+
pim_sock_read_helper(scope->unicast_sock, scope->pim, false);
269263

270264
event_add_read(router->master, bsm_unicast_sock_read, scope,
271265
scope->unicast_sock, &scope->unicast_read);
272-
273-
for (i = 0; i < router->packet_process; i++) {
274-
pim_sgaddr sg;
275-
276-
len = pim_socket_recvfromto(scope->unicast_sock, buf,
277-
sizeof(buf), &from, &fromlen, &to,
278-
&tolen, &ifindex);
279-
if (len < 0) {
280-
if (errno == EINTR)
281-
continue;
282-
if (errno == EWOULDBLOCK || errno == EAGAIN)
283-
break;
284-
285-
if (PIM_DEBUG_PIM_PACKETS)
286-
zlog_debug("Received errno: %d %s", errno,
287-
safe_strerror(errno));
288-
break;
289-
}
290-
291-
#if PIM_IPV == 4
292-
sg.src = ((struct sockaddr_in *)&from)->sin_addr;
293-
sg.grp = ((struct sockaddr_in *)&to)->sin_addr;
294-
#else
295-
sg.src = ((struct sockaddr_in6 *)&from)->sin6_addr;
296-
sg.grp = ((struct sockaddr_in6 *)&to)->sin6_addr;
297-
#endif
298-
299-
/*
300-
* What? So with vrf's the incoming packet is received
301-
* on the vrf interface but recvfromto above returns
302-
* the right ifindex, so just use it. We know
303-
* it's the right interface because we bind to it
304-
*/
305-
ifp = if_lookup_by_index(ifindex, scope->pim->vrf->vrf_id);
306-
if (!ifp) {
307-
zlog_warn("Received incoming PIM packet on unknown ifindex %d",
308-
ifindex);
309-
break;
310-
}
311-
312-
int fail = pim_pim_packet(ifp, buf, len, sg, false);
313-
314-
if (fail) {
315-
if (PIM_DEBUG_PIM_PACKETS)
316-
zlog_debug("%s: pim_pim_packet() return=%d",
317-
__func__, fail);
318-
break;
319-
}
320-
}
321266
}
322267

323268
void pim_bsm_proc_init(struct pim_instance *pim)

pimd/pim_pim.c

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "network.h"
1414

1515
#include "pimd.h"
16-
#include "pim_instance.h"
1716
#include "pim_pim.h"
1817
#include "pim_time.h"
1918
#include "pim_iface.h"
@@ -355,30 +354,19 @@ int pim_pim_packet(struct interface *ifp, uint8_t *buf, size_t len,
355354
}
356355
}
357356

358-
static void pim_sock_read_on(struct interface *ifp);
359-
360-
static void pim_sock_read(struct event *t)
357+
int pim_sock_read_helper(int fd, struct pim_instance *pim, bool is_mcast)
361358
{
362-
struct interface *ifp, *orig_ifp;
363-
struct pim_interface *pim_ifp;
364-
int fd;
359+
struct interface *ifp = NULL;
365360
struct sockaddr_storage from;
366361
struct sockaddr_storage to;
367362
socklen_t fromlen = sizeof(from);
368363
socklen_t tolen = sizeof(to);
369364
uint8_t buf[PIM_PIM_BUFSIZE_READ];
370365
int len;
371366
ifindex_t ifindex = -1;
372-
int result = -1; /* defaults to bad */
373-
static long long count = 0;
374-
int cont = 1;
375-
376-
orig_ifp = ifp = EVENT_ARG(t);
377-
fd = EVENT_FD(t);
378-
379-
pim_ifp = ifp->info;
367+
int i;
380368

381-
while (cont) {
369+
for (i = 0; i < router->packet_process; i++) {
382370
pim_sgaddr sg;
383371

384372
len = pim_socket_recvfromto(fd, buf, sizeof(buf), &from,
@@ -392,7 +380,7 @@ static void pim_sock_read(struct event *t)
392380
if (PIM_DEBUG_PIM_PACKETS)
393381
zlog_debug("Received errno: %d %s", errno,
394382
safe_strerror(errno));
395-
goto done;
383+
return -1;
396384
}
397385

398386
/*
@@ -401,14 +389,21 @@ static void pim_sock_read(struct event *t)
401389
* the right ifindex, so just use it. We know
402390
* it's the right interface because we bind to it
403391
*/
404-
ifp = if_lookup_by_index(ifindex, pim_ifp->pim->vrf->vrf_id);
405-
if (!ifp || !ifp->info) {
392+
if (pim != NULL)
393+
ifp = if_lookup_by_index(ifindex, pim->vrf->vrf_id);
394+
395+
/*
396+
* unicast BSM pkts (C-RP) may arrive on non pim interfaces
397+
* mcast pkts are only expected in pim interfaces
398+
*/
399+
if (!ifp || (is_mcast && !ifp->info)) {
406400
if (PIM_DEBUG_PIM_PACKETS)
407-
zlog_debug(
408-
"%s: Received incoming pim packet on interface(%s:%d) not yet configured for pim",
409-
__func__, ifp ? ifp->name : "Unknown",
410-
ifindex);
411-
goto done;
401+
zlog_debug("%s: Received incoming pim packet on interface(%s:%d)%s",
402+
__func__,
403+
ifp ? ifp->name : "Unknown", ifindex,
404+
is_mcast ? " not yet configured for pim"
405+
: "");
406+
return -1;
412407
}
413408
#if PIM_IPV == 4
414409
sg.src = ((struct sockaddr_in *)&from)->sin_addr;
@@ -418,27 +413,34 @@ static void pim_sock_read(struct event *t)
418413
sg.grp = ((struct sockaddr_in6 *)&to)->sin6_addr;
419414
#endif
420415

421-
int fail = pim_pim_packet(ifp, buf, len, sg, true);
416+
int fail = pim_pim_packet(ifp, buf, len, sg, is_mcast);
422417
if (fail) {
423418
if (PIM_DEBUG_PIM_PACKETS)
424419
zlog_debug("%s: pim_pim_packet() return=%d",
425420
__func__, fail);
426-
goto done;
421+
return -1;
427422
}
428-
429-
count++;
430-
if (count % router->packet_process == 0)
431-
cont = 0;
432423
}
424+
return 0;
425+
}
426+
427+
static void pim_sock_read_on(struct interface *ifp);
428+
429+
static void pim_sock_read(struct event *t)
430+
{
431+
struct interface *ifp;
432+
struct pim_interface *pim_ifp;
433+
int fd;
433434

434-
result = 0; /* good */
435+
ifp = EVENT_ARG(t);
436+
fd = EVENT_FD(t);
435437

436-
done:
437-
pim_sock_read_on(orig_ifp);
438+
pim_ifp = ifp->info;
438439

439-
if (result) {
440+
if (pim_sock_read_helper(fd, pim_ifp->pim, true) == 0)
440441
++pim_ifp->pim_ifstat_hello_recvfail;
441-
}
442+
443+
pim_sock_read_on(ifp);
442444
}
443445

444446
static void pim_sock_read_on(struct interface *ifp)

pimd/pim_pim.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <zebra.h>
1111

1212
#include "if.h"
13+
#include "pim_instance.h"
1314

1415
#define PIM_PIM_BUFSIZE_READ (20000)
1516
#define PIM_PIM_BUFSIZE_WRITE (20000)
@@ -48,4 +49,6 @@ int pim_msg_send(int fd, pim_addr src, pim_addr dst, uint8_t *pim_msg,
4849
int pim_msg_size, struct interface *ifp);
4950

5051
int pim_hello_send(struct interface *ifp, uint16_t holdtime);
52+
53+
int pim_sock_read_helper(int fd, struct pim_instance *pim, bool is_mcast);
5154
#endif /* PIM_PIM_H */

0 commit comments

Comments
 (0)