PadicoTM

Documentation

« back to PM2 home.
topology.c
Go to the documentation of this file.
1/*
2 * Padico -- a High Performance Parallel and Distributed Computing Environment
3 * Copyright (c) 2002-2026 INRIA and the University of Rennes 1
4 * Alexandre DENIS <Alexandre.Denis@inria.fr>
5 * Christian PEREZ <Christian.Perez@inria.fr>
6 *
7 * The software has been registered at the Agency for the Protection of
8 * Programs (APP) under the number IDDN.FR.001.260013.000.S.P.2002.000.10000.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
30#include <Padico/Puk.h>
31#include <Padico/Module.h>
32#include <Padico/PM2.h>
33#include <Padico/PadicoTM.h>
34
35#include "Topology-internals.h"
36
37#include <string.h>
38#include <unistd.h>
39#include <netdb.h>
40#include <netinet/in.h>
41#include <arpa/inet.h>
42#include <sys/un.h>
43#include <sys/socket.h>
44#include <sys/types.h>
45#include <sys/stat.h>
46#include <pwd.h>
47#include <resolv.h>
48#include <dirent.h>
49#include <poll.h>
50
51#ifdef HAVE_IFADDRS_H
52#include <ifaddrs.h>
53#include <netpacket/packet.h>
54#endif
55#include <sys/ioctl.h>
56
57#ifdef HAVE_LINUX_NETLINK_H
58#include <linux/netlink.h>
59#endif
60#ifdef HAVE_LINUX_RTNETLINK_H
61#include <linux/rtnetlink.h>
62#endif
63#ifdef HAVE_LINUX_SOCKIOS_H
64#include <linux/sockios.h>
65#endif
66#ifdef HAVE_LINUX_ETHTOOL_H
67#include <linux/ethtool.h>
68#endif
69#ifdef HAVE_LINUX_IF_H
70#include <linux/if.h>
71#else
72#include <net/if.h>
73#endif /* HAVE_LINUX_IF_H */
74
75static int padico_topology_init(void);
76static void padico_topology_finalize(void);
77
79
80PADICO_MODULE_ATTR(disable_dns, "PADICO_DISABLE_DNS", "do not try to resolve hostnames", bool, 0);
81PADICO_MODULE_ATTR(disable_ipv6, "PADICO_DISABLE_IPV6", "do not use IPv6 addresses", bool, 0);
82PADICO_MODULE_ATTR(autodetect, "PADICO_AUTODETECT", "auto-detect networks on startup and load drivers", bool, 1);
83
85
87{
88 return &na_topology;
89}
90
91static void topology_populate(void);
95static void topo_device_detect(void);
97static const char*topo_hwaddr_type_name(int type);
102
103
104/* ********************************************************* */
105
106/* Helper for addresses in bindings */
107
109{
110 struct topo_binding_s*const binding = (struct topo_binding_s*)_binding; /* wild cast to deal with iterators, but we don't write binding, really */
113 {
114 const struct topo_property_s*prop = *p;
115 if(prop->kind == TOPO_PROPERTY_ADDR)
116 {
117 return &prop->content.ADDR.addr;
118 }
119 }
120 return NULL;
121}
122
123/* ********************************************************* */
124/* ** hwaddr topology modification */
125
126padico_topo_hwaddr_t padico_topo_hwaddr_create(const char*name, int index, int type, size_t len, const void*addr)
127{
128 struct topo_hwaddr_s*hwaddr = padico_malloc(sizeof(struct topo_hwaddr_s) + len);
129 hwaddr->type = type;
130 hwaddr->index = index;
131 hwaddr->len = len;
132 memcpy(&hwaddr->_hwaddr_storage, addr, len);
135 hwaddr->name[TOPO_HWADDR_NAMSIZ - 1] = 0;
136 return hwaddr;
137}
138
139void padico_topo_hwaddr_get(padico_topo_hwaddr_t hwaddr, const char**name, int*index, int*type, size_t*len, const void**addr)
140{
141 if(name)
142 *name = hwaddr->name;
143 if(index)
144 *index = hwaddr->index;
145 if(type)
146 *type = hwaddr->type;
147 if(len)
148 *len = hwaddr->len;
149 if(addr)
150 *addr = &hwaddr->_hwaddr_storage;
151}
152
154{
157 if(byname == NULL)
158 {
159 /* store new hwaddr */
161 }
162 else if((byname->len == hwaddr->len) && (memcmp(byname->_hwaddr_storage, hwaddr->_hwaddr_storage, byname->len) == 0))
163 {
164 /* duplicate detected (same name, same address, index not compared since it may be invalid at this stage) */
165 padico_free((void*)hwaddr);
166 hwaddr = NULL;
167 *_hwaddr = byname;
168 }
169 else
170 {
171 padico_fatal("conflicting hwaddr with same name=%s; different address detected.\n", hwaddr->name);
172 }
173}
174
176{
177 padico_free((void*)hwaddr);
178}
179
180/* ********************************************************* */
181/* ** binding topology modification */
182
184{
186 const size_t addr_size = network->addr_size;
187 const int family = network->family;
188 struct topo_property_s*prop = padico_malloc(sizeof(struct topo_property_s) + addr_size);
189 prop->kind = TOPO_PROPERTY_ADDR;
190 prop->content.ADDR.addr.family = family;
191 prop->content.ADDR.addr.size = addr_size;
192 memcpy(&prop->content.ADDR.addr._addr_storage[0], addr, addr_size);
194}
195
197{
198 struct topo_property_s*prop = padico_malloc(sizeof(struct topo_property_s));
200 prop->content.HWADDR.hwaddr = hwaddr;
201 memcpy(prop->content.HWADDR.ifname, hwaddr->name, TOPO_HWADDR_NAMSIZ);
203}
204
206{
207 struct topo_property_s*prop = padico_malloc(sizeof(struct topo_property_s));
209 prop->content.INDEX.index = index;
211}
212
214{
216 struct topo_property_s*prop = NULL;
218 {
219 prop = *p;
220 if(prop->kind == TOPO_PROPERTY_GW_ADDR &&
221 memcmp(&prop->content.GW_ADDR.addr._addr_storage[0], inaddr, prop->content.GW_ADDR.addr.size) == 0)
222 return;
223 }
224 prop = padico_malloc(sizeof(struct topo_property_s) + sizeof(struct in_addr));
226 prop->content.GW_ADDR.addr.family = AF_INET;
227 prop->content.GW_ADDR.addr.size = sizeof(struct in_addr);
228 memcpy(&prop->content.GW_ADDR.addr._addr_storage[0], inaddr, sizeof(struct in_addr));
230}
231
233{
234 struct topo_property_s*prop = padico_malloc(sizeof(struct topo_property_s));
236 prop->content.GW_HWADDR.hwaddr = hwaddr;
238}
239
241{
242 struct topo_property_s*prop = padico_malloc(sizeof(struct topo_property_s));
244 prop->content.HOSTNAME.name = padico_strdup(name);
246}
247
249{
251 const size_t addr_size = network->addr_size;
252 const int family = network->family;
253 struct topo_property_s*prop = padico_malloc(sizeof(struct topo_property_s) + addr_size);
255 prop->content.ROUTE.dest = dest;
256 prop->content.ROUTE.gw.family = family;
257 prop->content.ROUTE.gw.size = addr_size;
258 memcpy(&prop->content.ROUTE.gw._addr_storage[0], gwaddr, addr_size);
260}
261
263{
265 struct topo_property_s*prop = padico_malloc(sizeof(struct topo_property_s));
267 prop->content.FILTER.filter.policy = policy;
268 prop->content.FILTER.filter.port = port;
269 prop->content.FILTER.filter.protocol = protocol;
271}
272
274{
275 struct topo_property_s*prop = padico_malloc(sizeof(struct topo_property_s));
276 prop->kind = TOPO_PROPERTY_PERF;
277 prop->content.PERF.perf.lat = latency;
278 prop->content.PERF.perf.bw = bandwidth;
280}
281
283{
286 {
287 const struct topo_property_s*prop = *p;
288 if(prop->kind == TOPO_PROPERTY_PERF)
289 {
290 if(latency_nsec != NULL)
292 if(bandwidth_kbps != NULL)
293 *bandwidth_kbps = prop->content.PERF.perf.bw;
294 return;
295 }
296 }
297}
298
300{
303 {
304 const struct topo_property_s*prop = *p;
305 if(prop->kind == TOPO_PROPERTY_HWADDR)
306 {
307 return prop->content.HWADDR.hwaddr;
308 }
309 }
310 return NULL;
311}
312
314{
317 {
318 const struct topo_property_s*prop = *p;
319 if(prop->kind == TOPO_PROPERTY_INDEX)
320 {
322 return 0;
323 }
324 }
325 return -1;
326}
327
328
330{
331 assert(addr && addrlen && *addrlen >= sizeof(struct sockaddr_pa));
332 memset(addr->spa_payload, 0, AF_PADICO_PAYLOAD); /* reset with 0s; better for debug! */
333 addr->spa_family = AF_PADICO;
334 addr->spa_magic = magic;
335}
336
338{
339 const struct sockaddr*saddr = (struct sockaddr*)addr;
340 if(saddr->sa_family != AF_PADICO)
341 {
342#ifndef NDEBUG
343 padico_warning("bad address family- sa_family=%d; expected=%d (AF_PADICO).\n",
344 saddr->sa_family, AF_PADICO);
345#endif
346 return EAFNOSUPPORT;
347 }
348 else if(addr->spa_magic != magic)
349 {
350#ifndef NDEBUG
353 padico_warning("bad Padico address magic- spa_magic=%d (%c%c%c%c); expected=%d (%c%c%c%c).\n",
354 addr->spa_magic, m.bytes[0], m.bytes[1], m.bytes[2], m.bytes[3],
355 magic, e.bytes[0], e.bytes[1], e.bytes[2], e.bytes[3]);
356#endif
357 return EADDRNOTAVAIL;
358 }
359 return 0;
360}
361
362/* ********************************************************* */
363
377
410
412static const struct hostent*topo_gethostbyaddr(const void*addr, socklen_t len, int family)
413{
414 static int disable_ipv6 = -1;
415 if(disable_ipv6 < 0)
416 {
418 if(disable_ipv6)
419 {
420 padico_warning("IPv6 disabled by user request.\n");
421 }
422 }
424 {
425 return NULL;
426 }
427 char s_addr[256] = { '\0' };
428 inet_ntop(family, addr, s_addr, 256);
429 if(disable_ipv6 && family == AF_INET6)
430 {
431 return NULL;
432 }
433 const struct hostent*he = gethostbyaddr(addr, len, family);
434 return he;
435}
436
438static const struct hostent*topo_gethostbyname2(const char*name, int family)
439{
440 static int disable_ipv6 = -1;
441 if(disable_ipv6 < 0)
442 {
444 if(disable_ipv6)
445 {
446 padico_warning("IPv6 disabled by user request.\n");
447 }
448 }
449 h_errno = 0;
450 if(disable_ipv6 && family == AF_INET6)
451 {
452 return NULL;
453 }
454 const struct hostent*he = gethostbyname2(name, family);
455 if(!he)
456 {
457 int count = 5;
458 while(he == NULL && h_errno == TRY_AGAIN && count > 0)
459 {
460 h_errno = 0;
461 he = gethostbyname2(name, family);
462 count--;
463 }
464 }
465 if(!he)
466 {
467 /* some resolver are picky with short names without final dot */
468 if(strchr(name, '.') == NULL)
469 {
470 char*tmp_name = padico_malloc(strlen(name) + 2);
472 strcat(tmp_name, ".");
473 he = gethostbyname2(tmp_name, family);
475 }
476 }
477 return he;
478}
479
481{
482#if defined(HAVE_LINUX_SOCKIOS_H) && defined(HAVE_LINUX_ETHTOOL_H) && defined(HAVE_LINUX_IF_H)
483 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
484 if(sock < 0)
485 return;
486 struct ifreq ifr;
487 struct ethtool_cmd edata;
488 int rc;
489 strncpy(ifr.ifr_name, hwaddr->name, sizeof(ifr.ifr_name));
490 ifr.ifr_data = &edata;
491 edata.cmd = ETHTOOL_GSET;
493 if (rc < 0)
494 goto close;
495 padico_topo_binding_add_perf(b, 300000, edata.speed*1000/8);
496 close:
497 close(sock);
498#endif
499}
500
504{
505#ifdef HAVE_IFADDRS_H
506 struct ifaddrs*ifa_list = NULL;
507 int rc = getifaddrs(&ifa_list);
508 if(rc == 0)
509 {
510 const struct ifaddrs*i;
511 for(i = ifa_list; i != NULL; i = i->ifa_next)
512 {
513 const int is_loopback = !!(i->ifa_flags & IFF_LOOPBACK);
514 const int is_up = !!(i->ifa_flags & IFF_UP);
515 if (i->ifa_addr && i->ifa_addr->sa_family == AF_INET)
516 {
517 /* ** detect IPv4 address */
518 const struct sockaddr_in*inaddr = (const struct sockaddr_in*)i->ifa_addr;
520 const uint32_t netmask = ntohl(((struct sockaddr_in*)i->ifa_netmask)->sin_addr.s_addr);
521 int prefix_len = 0;
522 while((prefix_len < 32) && ((netmask & (0x01UL << (31 - prefix_len))) != 0))
523 {
524 prefix_len ++;
525 }
526 padico_print("address %8s; inet=%15s/%2d (%s)\n",
527 i->ifa_name, inet_ntoa(inaddr->sin_addr),
529
530 if(is_up)
531 {
533 const struct hostent*he = topo_gethostbyaddr(&inaddr->sin_addr, sizeof(struct sockaddr_in), AF_INET);
534 if(he)
536 switch(v)
537 {
540 {
541 padico_topo_binding_t binding =
543 if(hwaddr)
544 padico_topo_binding_add_hwaddr(binding, hwaddr);
545 if(he)
546 padico_topo_binding_add_hostname(binding, he->h_name);
547 }
548 break;
550 {
552 const struct in_addr network_addr = { .s_addr = inaddr->sin_addr.s_addr & htonl(netmask) };
555 if(network == NULL)
556 {
558 PADICO_TOPO_SCOPE_HOST, sizeof(struct in_addr), AF_INET);
559 }
561 if(hwaddr)
562 {
565 }
566 if(he)
569 }
570 break;
572 {
574 padico_string_printf(network_name, "inet-[loopback/%s]", localhost->hostname);
576 if(network == NULL)
577 {
579 PADICO_TOPO_SCOPE_HOST, sizeof(struct in_addr), AF_INET);
580 }
583 }
584 break;
586 default:
587 padico_warning("not storing address in binding.\n");
588 }
589 }
590 }
591 else if(i->ifa_addr && i->ifa_addr->sa_family == AF_INET6)
592 {
593 /* ** detect IPv6 address */
594 const struct sockaddr_in6*in6addr = (const struct sockaddr_in6*)i->ifa_addr;
596 char addr[INET6_ADDRSTRLEN];
597 inet_ntop(AF_INET6, &in6addr->sin6_addr, addr, INET6_ADDRSTRLEN);
598 padico_print("address %8s; inet6=%46s (%s)\n", i->ifa_name, addr, topo_addr_visibility_name(v));
600 {
601 padico_topo_binding_t binding =
604 if(hwaddr)
605 padico_topo_binding_add_hwaddr(binding, hwaddr);
606 }
607 }
608 else if(i->ifa_addr && i->ifa_addr->sa_family == AF_PACKET)
609 {
610 /* ** detect hardware address */
611 const struct sockaddr_ll*lladdr = (const struct sockaddr_ll*)i->ifa_addr;
613 int j;
614 for(j = 0; j < lladdr->sll_halen; j++)
615 {
616 padico_string_catf(s_addr, (j==0)?"%02X":":%02X", lladdr->sll_addr[j]);
617 }
618 padico_print("hardware %8s; hwaddr=%s; index=%d (%s)\n",
619 i->ifa_name, padico_string_get(s_addr),
620 lladdr->sll_ifindex, topo_hwaddr_type_name(lladdr->sll_hatype));
622 if(lladdr->sll_hatype != ARPHRD_LOOPBACK)
623 {
624 padico_topo_hwaddr_t hwaddr =
625 padico_topo_hwaddr_create(i->ifa_name, lladdr->sll_ifindex, lladdr->sll_hatype, lladdr->sll_halen, lladdr->sll_addr);
627 }
628 }
629 else if(i->ifa_addr)
630 {
631 padico_print("unknown address family (%d)\n", i->ifa_addr->sa_family);
632 }
633 }
634 }
635 else
636 {
637 padico_warning("error while listing network interfaces (getifaddrs: %s).\n", strerror(errno));
638 return -1;
639 }
641 return 0;
642#else /* HAVE_IFADDRS_H */
643 return -1;
644#endif /* HAVE_IFADDRS_H */
645}
646
650{
651 const size_t max_ifindex = 32;
652 struct ifreq ifreqs[max_ifindex];
653 struct ifconf ifc =
654 {
655 .ifc_len = max_ifindex * sizeof(struct ifreq),
656 .ifc_buf = (void*)&ifreqs
657 };
658 memset(&ifreqs[0], 0, max_ifindex * sizeof(struct ifreq));
659 int sockfd = PUK_ABI_FSYS_WRAP(socket)(AF_INET, SOCK_DGRAM, 0); /* socket for netdevice ioctl() */
660 if(sockfd < 0)
661 {
662 padico_warning("error while enumerating addresses.\n");
663 return;
664 }
666 /* warning- SIOCGIFCONF only returns IPv4 addresses */
667 if(rc)
668 padico_fatal("error %s while enumerating addresses.\n",
670 int i;
671 for(i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++)
672 {
673 struct ifreq*ifr = &ifreqs[i];
674 padico_print("IF addr family=%d; if=%s\n",
675 ifr->ifr_addr.sa_family, ifr->ifr_name);
677 padico_print("IF hw addr family=%d; if=%s\n",
678 ifr->ifr_addr.sa_family, ifr->ifr_name);
679 }
681}
682
686{
687#if defined(HAVE_LINUX_NETLINK_H) && defined(HAVE_LINUX_RTNETLINK_H)
688 /* create netlink socket */
690 if(fd < 0)
691 {
692 padico_warning("cannot open netlink socket (socket: %s).\n", strerror(errno));
693 return -1;
694 }
695 struct sockaddr_nl addr = {
696 .nl_family = AF_NETLINK,
697 .nl_pad = 0,
698 .nl_pid = getpid(),
699 .nl_groups = 0
700 };
701 int rc = PUK_ABI_FSYS_WRAP(bind)(fd, (struct sockaddr*)&addr, sizeof(addr));
702 if(rc < 0)
703 {
704 padico_warning("cannot bind netlink socket.\n");
705 return -1;
706 }
707 /* netlink request */
708 struct
709 {
710 struct nlmsghdr nl;
711 struct ifaddrmsg ifa;
712 char buf[8192];
713 } req;
714 memset(&req, 0, sizeof(req));
715 req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
716 req.nl.nlmsg_type = RTM_GETADDR;
717 req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
718 req.nl.nlmsg_seq = 0;
719 req.nl.nlmsg_pid = getpid();
720 req.ifa.ifa_family = AF_INET;
721 struct iovec iov = { .iov_base = (void*)&req.nl, .iov_len = req.nl.nlmsg_len };
722 /* send request */
723 struct sockaddr_nl peer = {
724 .nl_family = AF_NETLINK,
725 .nl_pad = 0,
726 .nl_pid = 0, /* 0 = kernel */
727 .nl_groups = 0
728 };
729 struct msghdr msg = {
730 .msg_iov = &iov,
731 .msg_iovlen = 1,
732 .msg_name = (void*)&peer,
733 .msg_namelen = sizeof(peer)
734 };
735 rc = PUK_ABI_FSYS_WRAP(sendmsg)(fd, &msg, 0);
736 if(rc < 0)
737 padico_fatal("error while sending netlink request.\n");
738 /* receive reply */
739 char buf[8192];
740 memset(buf, 0, sizeof(buf));
741 char*p = buf;
742 int nll = 0;
743 while(1)
744 {
745 rc = PUK_ABI_FSYS_WRAP(recv)(fd, p, sizeof(buf) - nll, 0);
746 if(rc <= 0)
747 break;
748 struct nlmsghdr*nlp = (struct nlmsghdr*)p;
749 if(nlp->nlmsg_type == NLMSG_DONE)
750 break;
751 p += rc;
752 nll += rc;
753 }
754 /* parse reply */
755 const struct nlmsghdr*nlp = (struct nlmsghdr*)buf;
756 while(NLMSG_OK(nlp, nll))
757 {
758 const struct ifaddrmsg*ifa = (struct ifaddrmsg*)NLMSG_DATA(nlp);
759 const struct rtattr*rtap = (struct rtattr*)IFA_RTA(ifa);
760 int rtl = IFA_PAYLOAD(nlp);
761 while(RTA_OK(rtap, rtl))
762 {
763 switch(rtap->rta_type)
764 {
765 case IFA_LOCAL:
766 {
767 if(ifa->ifa_family == AF_INET)
768 {
769 const struct in_addr*inaddr = (const struct in_addr*)RTA_DATA(rtap);
770 const int prefix_len = ifa->ifa_prefixlen;
771 const int ifindex = ifa->ifa_index;
773 padico_print("netlink index=%d; inet=%15s/%2d (%s)\n",
775 switch(v)
776 {
779 {
781 }
782 break;
784 {
785 padico_warning("TODO- private address\n");
787 /*
788 padico_string_t network_name = padico_string_new();
789 const struct in_addr network_addr = { .s_addr = inaddr->s_addr & htonl(netmask) };
790 padico_string_printf(network_name, "inet-[%s/%d]", inet_ntoa(network_addr), prefix_len);
791 padico_topo_network_t network = padico_topo_network_getbyid(padico_string_get(network_name));
792 if(network == NULL)
793 {
794 network = padico_topo_network_create(padico_string_get(network_name),
795 PADICO_TOPO_SCOPE_HOST, sizeof(struct in_addr), AF_INET);
796 }
797 padico_topo_binding_t priv_binding = padico_topo_host_bind(localhost, network, &inaddr->sin_addr);
798 padico_string_delete(network_name);
799 */
800 }
801 break;
803 padico_warning("TODO- loopback\n");
804 break;
806 default:
807 padico_warning("not storing address in binding.\n");
808
809 }
810 }
811 }
812 break;
813 default:
814 break;
815 }
817 }
818 nlp = NLMSG_NEXT(nlp, nll);
819 }
820
821
822 PUK_ABI_FSYS_WRAP(close)(fd);
823 return 0;
824#else
825 return -1;
826#endif /* HAVE_LINUX_NETLINK_H && HAVE_LINUX_RTNETLINK_H */
827}
828
830{
831 /* list network interfaces */
833 if(rc != 0)
834 {
836 if(rc != 0)
837 {
839 }
840 }
841}
842
844{
845#if defined(HAVE_LINUX_NETLINK_H) && defined(HAVE_LINUX_RTNETLINK_H)
846 /* create rtnetlink socket */
848 if(fd < 0)
849 {
850 padico_warning("cannot open rtnetlink socket (socket: %s).\n", strerror(errno));
851 return;
852 }
853 struct sockaddr_nl addr = {
854 .nl_family = AF_NETLINK,
855 .nl_pad = 0,
856 .nl_pid = getpid(),
857 .nl_groups = 0
858 };
859 int rc = PUK_ABI_FSYS_WRAP(bind)(fd, (struct sockaddr*)&addr, sizeof(addr));
860 if(rc < 0)
861 {
862 PUK_ABI_FSYS_WRAP(close)(fd);
863 padico_warning("cannot bind rtnetlink socket.\n");
864 return;
865 }
866 /* rtnetlink request */
867 struct
868 {
869 struct nlmsghdr nl;
870 struct rtmsg rt;
871 char buf[8192];
872 } req;
873 memset(&req, 0, sizeof(req));
874 req.nl.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
875 req.nl.nlmsg_type = RTM_GETROUTE;
876 req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
877 req.nl.nlmsg_seq = 0;
878 req.nl.nlmsg_pid = getpid();
879 req.rt.rtm_family = AF_INET;
880 req.rt.rtm_table = RT_TABLE_MAIN;
881 req.rt.rtm_type = RTN_UNICAST; /* gateway */
882 struct iovec iov = { .iov_base = (void*)&req.nl, .iov_len = req.nl.nlmsg_len };
883 /* send request */
884 struct sockaddr_nl peer = {
885 .nl_family = AF_NETLINK,
886 .nl_pad = 0,
887 .nl_pid = 0, /* 0 = kernel */
888 .nl_groups = 0
889 };
890 struct msghdr msg = {
891 .msg_iov = &iov,
892 .msg_iovlen = 1,
893 .msg_name = (void*)&peer,
894 .msg_namelen = sizeof(peer)
895 };
896 rc = PUK_ABI_FSYS_WRAP(sendmsg)(fd, &msg, 0);
897 if(rc < 0)
898 {
899 PUK_ABI_FSYS_WRAP(close)(fd);
900 padico_warning("error while sending rtnetlink request.\n");
901 return;
902 }
903 /* receive reply */
904 char buf[8192];
905 memset(buf, 0, sizeof(buf));
906 char*p = buf;
907 int nll = 0;
908 while(1)
909 {
910 rc = PUK_ABI_FSYS_WRAP(recv)(fd, p, sizeof(buf) - nll, 0);
911 if(rc <= 0)
912 break;
913 struct nlmsghdr*nlp = (struct nlmsghdr*)p;
914 if(nlp->nlmsg_type == NLMSG_DONE)
915 break;
916 p += rc;
917 nll += rc;
918 }
919 /* parse reply */
920 const struct nlmsghdr*nlp = (struct nlmsghdr*)buf;
921 while(NLMSG_OK(nlp, nll))
922 {
923 const struct rtmsg*rtp = (struct rtmsg*)NLMSG_DATA(nlp);
924 if(rtp->rtm_table == RT_TABLE_MAIN)
925 {
926 int ifn = -1;
927 struct in_addr gw = { .s_addr = INADDR_NONE };
928 struct in_addr dest = { .s_addr = INADDR_NONE };
929 const struct rtattr*rtap = (struct rtattr*)RTM_RTA(rtp);
930 int rtl = RTM_PAYLOAD(nlp);
931 while(RTA_OK(rtap, rtl))
932 {
933 switch(rtap->rta_type)
934 {
935 case RTA_DST: /* destination IPv4 address */
936 dest = *(struct in_addr*)RTA_DATA(rtap);
937 break;
938 case RTA_GATEWAY: /* gateway IPv4 address */
939 gw = *(struct in_addr*)RTA_DATA(rtap);
940 break;
941 case RTA_OIF: /* outbound interface number */
942 ifn = *(int*)RTA_DATA(rtap);
943 default:
944 break;
945 }
947 }
948 if(ifn != -1 && gw.s_addr != INADDR_NONE)
949 {
951 char*dest_s = padico_strdup(inet_ntoa(dest));
952 padico_print("route %8s; gw = %15s; dest = %15s/%d\n", hwaddr->name, inet_ntoa(gw), dest_s, rtp->rtm_dst_len);
953 struct ifreq ifr;
954 struct arpreq arpreq;
955 int sockfd = PUK_ABI_FSYS_WRAP(socket)(AF_INET, SOCK_DGRAM, 0);
956 ifr.ifr_addr.sa_family = AF_INET;
957 strncpy(ifr.ifr_name, hwaddr->name, TOPO_HWADDR_NAMSIZ);
959 memset(&arpreq, 0, sizeof(struct arpreq));
960 struct sockaddr_in*gwaddr = (struct sockaddr_in*)&arpreq.arp_pa;
961 memcpy(&gwaddr->sin_addr, &gw, sizeof(struct in_addr));
962 gwaddr->sin_family = AF_INET;
963 strcpy(arpreq.arp_dev, hwaddr->name);
966 {
967 gw_hwaddr = padico_topo_hwaddr_create("", 0, ARPHRD_ETHER, 6, &arpreq.arp_ha.sa_data[0]);
968 }
970 struct in_addr local = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
974 {
975 /* find binding for given hwaddr */
978 {
979 padico_topo_binding_t binding = *b;
982 {
983 if((*p)->kind == TOPO_PROPERTY_HWADDR && (*p)->content.HWADDR.hwaddr == hwaddr)
984 {
986 if(gw_hwaddr)
988 if(dest.s_addr == INADDR_NONE)
989 {
991 if(!network->uplink && network->family == AF_INET)
992 {
994 uplink->entity.network = network;
995 uplink->entity.kind = TOPO_ENTITY_NETWORK;
996 network->uplink = uplink;
999 }
1000 }
1001 if(dest.s_addr != INADDR_NONE && dest_v == PADICO_TOPO_ADDR_VISIBILITY_PRIVATE)
1002 {
1004 padico_string_printf(network_name, "inet-[%s/%d]", dest_s, rtp->rtm_dst_len);
1006 if(!network)
1007 {
1009 PADICO_TOPO_SCOPE_HOST, sizeof(struct in_addr), AF_INET);
1011 }
1012 }
1013 break; /* props have been modified- iterator is now invalid */
1014 }
1015 }
1016 }
1017 }
1018 if(gw_hwaddr != NULL)
1019 {
1021 }
1023 }
1024 }
1025 nlp = NLMSG_NEXT(nlp, nll);
1026 }
1027 PUK_ABI_FSYS_WRAP(close)(fd);
1028#endif
1029}
1030
1032{
1033 unsigned long txq, rxq, retrnsmt, inode, tm;
1034 int sl, local_port, remote_port, state, tr, uid, timeout;
1035 char line[2048], local_addr[128], remote_addr[128], end[1024];
1036 size_t line_len = 0, rc = 0;
1037 line_len = sizeof(line);
1038 FILE *tcp_file = fopen("/proc/net/tcp", "r");
1039 setservent(1);
1040 if(tcp_file)
1041 {
1043 do
1044 {
1045 if(!fgets(line, line_len, tcp_file))
1046 break;
1047 rc = sscanf(line, "%d: %[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
1048 &sl, local_addr, &local_port, remote_addr, &remote_port, &state, &txq, &rxq, &tr, &tm, &retrnsmt, &uid, &timeout, &inode, end);
1049 if(rc <= 0)
1050 break;
1051 struct in_addr local_inaddr;
1052 sscanf(local_addr, "%X", &(local_inaddr.s_addr));
1053 local_inaddr.s_addr = htonl(local_inaddr.s_addr);
1054 if(state == TCP_LISTEN && local_inaddr.s_addr != INADDR_LOOPBACK && local_port < 1024)
1055 {
1056 struct servent*servent = getservbyport(htons(local_port), "tcp");
1057 if(servent)
1059 }
1060 }
1061 while(!feof(tcp_file));
1063 }
1064 endservent();
1065}
1066
1067/* if host have special devices, try load driver */
1068static void topo_device_detect(void)
1069{
1071 {
1072 /* ** auto-detect infiniband */
1073 struct stat s;
1074 int ret;
1076 ret = stat("/dev/infiniband", &s);
1077 if(!ret && S_ISDIR(s.st_mode))
1078 {
1079#ifdef HAVE_IBVERBS
1080 puk_mod_t mod = NULL;
1081 rc = padico_puk_mod_resolve(&mod, "InfinibandVerbs");
1082 if(mod)
1083 {
1085 }
1086 else
1087 {
1088 padico_warning("detected infiniband device but failed to load ibverbs driver.\n");
1089 }
1090#else /* HAVE_IBVERBS */
1091 padico_warning("detected infiniband device but ibverbs drivers not compiled.\n");
1092#endif /* HAVE_IBVERBS */
1093 }
1094 /* ** auto-detect PSM & PSM2 */
1095 int psm_detected = 0;
1096 int psm2_detected = 0;
1097 DIR*d = opendir("/sys/class/infiniband/");
1098 if(d)
1099 {
1100 struct dirent*dir;
1101 while((dir = readdir(d)) != NULL)
1102 {
1103 if(strncmp(dir->d_name, "qib", 3) == 0)
1104 {
1105 psm_detected = 1;
1106 }
1107 if(strncmp(dir->d_name, "hfi", 3) == 0)
1108 {
1109 psm2_detected = 1;
1110 }
1111 }
1112 closedir(d);
1113 }
1114 if(psm_detected)
1115 {
1116 padico_print("detected PSM device.\n");
1117#ifdef HAVE_PSM
1118 puk_mod_t mod = NULL;
1119 rc = padico_puk_mod_resolve(&mod, "PSM_discover");
1120 if(mod)
1121 {
1123 }
1124 else
1125 {
1126 padico_warning("detected PSM device but failed to load PSM driver.\n");
1127 }
1128#else /* HAVE_PSM */
1129 padico_warning("detected PSM device but psm driver not compiled.\n");
1130#endif /* HAVE_PSM */
1131 }
1132 if(psm2_detected)
1133 {
1134 padico_print("detected PSM2 device.\n");
1135#ifdef HAVE_PSM2
1136 puk_mod_t mod = NULL;
1137 rc = padico_puk_mod_resolve(&mod, "PSM2_discover");
1138 if(mod)
1139 {
1141 }
1142 else
1143 {
1144 padico_warning("detected PSM2 device but failed to load PSM2 driver.\n");
1145 }
1146#else /* HAVE_PSM2 */
1147 padico_warning("detected PSM2 device but psm2 driver not compiled.\n");
1148#endif /* HAVE_PSM2 */
1149 }
1150
1151 /* ** auto-detect BXI */
1152 ret = stat("/dev/bxi", &s);
1153 if(!ret && S_ISDIR(s.st_mode))
1154 {
1155#ifdef HAVE_PORTALS4
1156 puk_mod_t mod = NULL;
1157 rc = padico_puk_mod_resolve(&mod, "Portals4");
1158 if(mod)
1159 {
1161 }
1162 else
1163 {
1164 padico_warning("detected BXI device but failed to load portals4 driver.\n");
1165 }
1166#else /* HAVE_PORTALS4 */
1167 padico_warning("detected BXI device but portals4 drivers not compiled.\n");
1168#endif /* HAVE_PORTALS4 */
1169 }
1170
1171 /* ** auto-detect Cray CXI (Slingshot) */
1172 ret = stat("/dev/cxi0", &s);
1173 if(!ret)
1174 {
1175#ifdef HAVE_FABRIC_H
1176 puk_mod_t mod = NULL;
1177 rc = padico_puk_mod_resolve(&mod, "libfabric");
1178 if(mod)
1179 {
1181 }
1182 else
1183 {
1184 padico_warning("detected Cray Slingshot device but failed to load libfabric driver.\n");
1185 }
1186#else /* HAVE_FABRIC_H */
1187 padico_warning("detected Cray Slingshot device but libfabric not detect.\n");
1188#endif /* HAVE_FABRIC_H */
1189 }
1190
1191 /* TODO- add Myrinet detection */
1192 if(rc)
1194 }
1195}
1196
1198{
1201 if((inaddr & 0xff000000) == 0x7f000000) /* 127.0.0.x/8 */
1203 else if( ((inaddr & 0xff000000) == 0x0a000000) || /* 10.x.x.x/8 */
1204 ((inaddr & 0xfff00000) == 0xac100000) || /* 172.16.x.x/12 */
1205 ((inaddr & 0xffff0000) == 0xc0a80000) ) /* 192.168.x.x/16 */
1207 else if( ((inaddr & 0xffff0000) == 0xa9fe0000) || /* 169.254.x.x/16*/
1208 ((inaddr & 0xffffff00) == 0xc0000200) ) /* 192.0.2.x/24 */
1210 else
1212 return v;
1213}
1214
1216{
1217 if(memcmp(in6addr, &in6addr_loopback, sizeof(struct in6_addr)) == 0)
1219 else if((ntohs(in6addr->s6_addr16[0]) & 0xffc0) == 0xfe80) /* fe80::/10 */
1221 else if((ntohs(in6addr->s6_addr16[0]) & 0xfe00) == 0xfc00) /* fc00::/7 */
1223 else
1225}
1226
1229{
1230 switch(v)
1231 {
1233 return "none";
1235 return "loopback";
1237 return "link-local";
1239 return "private";
1241 return "public";
1242 default:
1243 return "unknown";
1244 }
1245}
1246
1248static const char*topo_hwaddr_type_name(int type)
1249{
1250 switch(type)
1251 {
1252 case ARPHRD_ETHER:
1253 case ARPHRD_EETHER:
1254 return "ethernet";
1255 case ARPHRD_LOOPBACK:
1256 return "loopback";
1257#ifdef ARPHRD_ATM
1258 case ARPHRD_ATM:
1259 return "atm";
1260#endif
1261#ifdef ARPHRD_IEEE1394
1262 case ARPHRD_IEEE1394:
1263 return "ieee1394";
1264#endif
1265#ifdef ARPHRD_INFINIBAND
1266 case ARPHRD_INFINIBAND:
1267 return "infiniband";
1268#endif
1269#ifdef ARPHRD_PPP
1270 case ARPHRD_PPP:
1271 return "ppp";
1272#endif
1273#ifdef ARPHRD_IRDA
1274 case ARPHRD_IRDA:
1275 return "IrDA";
1276#endif
1277#ifdef ARPHRD_IEEE80211
1278 case ARPHRD_IEEE80211:
1279 return "ieee802.11";
1280#endif
1281 default:
1282 return "unknown";
1283 }
1284}
1285
1286static void topology_populate(void)
1287{
1288 /* regular boot env */
1289 const char*attr_uuid = padico_getenv("PADICO_NODE_UUID");
1290 const char*boot_id = padico_getenv("PADICO_BOOT_ID");
1291 const char*boot_rank = padico_getenv("PADICO_BOOT_RANK");
1292 /* PMIx */
1293 const char*pmix_namespace = padico_getenv("PMIX_NAMESPACE");
1294 const char*pmix_rank = padico_getenv("PMIX_RANK");
1295 /* PMI2 */
1296 const char*pmi_job_id = padico_getenv("PMI_JOBID");
1297 const char*pmi_rank = padico_getenv("PMI_RANK");
1298 /* Puk sim */
1299 const char*puk_mult_root = padico_getenv("PUK_MULT_ROOT");
1300 char puk_mult_rank[16];
1301 /* retrieve job attributes (job id, rank) */
1302 if(boot_id != NULL && boot_rank != NULL)
1303 {
1304 padico_print("create node from padico-launch attributes.\n");
1305 }
1306 else if(pmix_namespace != NULL && pmix_rank != NULL)
1307 {
1308 /* try PMIx */
1309 padico_print("create node from PMIx information; namespace = %s; rank = %s.\n",
1313 }
1314 else if(pmi_job_id != NULL && pmi_rank != NULL)
1315 {
1316 /* try PMI2 */
1317 padico_print("create node from PMI/PMI2 information; job_id = %s; rank = %s.\n",
1321 }
1322 else if(padico_getenv("SLURM_JOB_ID") != NULL)
1323 {
1324 /* slurm without regular env, PMI2, PMIx- oops */
1325 padico_fatal("detected slurm job, but no padico-launch environment nor PMI2/PMIx environment. Please launch your binary either with padico-launch, srun --mpi=pmi2, or srun --mpi=pmix.\n");
1326 }
1327 else if(puk_mult_root != NULL)
1328 {
1329 /* simulation */
1330 boot_id = padico_getenv("PUK_MULT_BINARY");
1334 padico_print("create node from Puk simulation information; job_id = %s; rank = %s.\n",
1336 }
1337 else
1338 {
1339 /* single node */
1344 padico_print("single node: %s.\n", boot_id);
1345 }
1346 const int pid = getpid();
1347 const int rank = boot_rank ? atoi(boot_rank) : 0;
1348
1349 if(puk_mult_root)
1350 {
1351 /* some functions used to detect networks are not re-entrant
1352 * (rtnetlink, gethostent, etc.)
1353 * -> switch to exclusive mode when using simulation with multiple
1354 * nodes per process.
1355 */
1357 }
1358
1359 /* create localhost */
1360 const int local_hostname_size = 257;
1365 if(he == NULL)
1366 {
1367 padico_warning("local hostname = %s is not resolvable (%s).\n", local_hostname, hstrerror(h_errno));
1369 }
1370 else
1371 {
1374 const int uid = getuid();
1375 struct passwd*login = getpwuid(uid);
1376 if(login)
1377 padico_topo_host_add_property(localhost, "login", login->pw_name);
1378 }
1379
1380 /* create node name */
1382 if(puk_mult_root)
1383 {
1384 padico_string_printf(nodename, "%s#%d-%s/%s", localhost->hostname, pid, puk_getsymprefix(), boot_id);
1385 }
1386 else
1387 {
1388 padico_string_printf(nodename, "%s#%d/%s", localhost->hostname, pid, boot_id);
1389 }
1390 padico_print("this node is- %s\n", padico_string_get(nodename));
1391
1392 /* get (or create) node UUID */
1393 char*uuid = NULL;
1394 if(attr_uuid != NULL)
1395 {
1397 }
1398 else
1399 {
1400 static const char hex_digits[] = "0123456789ABCDEF\0";
1401 uuid = padico_malloc(sizeof(struct topo_uuid_s));
1403 uuid[0] = hex_digits[(crc & 0x0000000F) >> 0];
1404 uuid[1] = hex_digits[(crc & 0x000000F0) >> 4];
1405 uuid[2] = hex_digits[(crc & 0x00000F00) >> 8];
1406 uuid[3] = hex_digits[(crc & 0x0000F000) >> 12];
1407 uuid[4] = hex_digits[(crc & 0x000F0000) >> 16];
1408 uuid[5] = hex_digits[(crc & 0x00F00000) >> 20];
1409 uuid[6] = hex_digits[(crc & 0x0F000000) >> 24];
1410 uuid[7] = hex_digits[(crc & 0xF0000000) >> 28];
1411 srandom(getpid());
1412 int i;
1413 for(i = 8 ; i< PADICO_TOPO_UUID_SIZE; i++)
1414 {
1415 uuid[i] = hex_digits[random() % 16];
1416 }
1418 }
1419
1420 /* create node */
1425
1426 /* loopback and session networks */
1428 padico_string_printf(s_session, "Session-[%s]", boot_id);
1433 padico_string_printf(s_hloop, "LoopbackHost-[%s]", localhost->hostname);
1442
1443 /* detecte inet features */
1444 padico_print("this host is %s- detecting networks...\n", localhost->hostname);
1448
1449 /* detecte special device */
1451
1452 if(puk_mult_root)
1453 {
1455 }
1456}
1457
1458/* ********************************************************* */
1459
1477
1509
1511{
1513 if(!host)
1514 {
1515 if(strcmp(hostname, "localhost") == 0 ||
1516 strcmp(hostname, "ip6-localhost") == 0 ||
1517 strcmp(hostname, "ip6-loopback") == 0)
1518 {
1520 }
1521 }
1522 return host;
1523}
1524
1526{
1527 if(strcmp(_name, "localhost") == 0)
1528 {
1529 return;
1530 }
1531 if(strcmp(_name, "") == 0)
1532 {
1533 padico_warning("trying to add empty hostname.\n");
1534 return;
1535 }
1538 {
1539 if(strcmp((*i)->name, _name) == 0)
1540 return;
1541 }
1542 if(resolvable == -1)
1543 {
1545 {
1546 padico_warning("DNS query disabled by user request.\n");
1547 resolvable = 0;
1548 }
1549 else
1550 {
1551 unsigned char reply[1024];
1552 int len = 1024;
1553 int rc = res_init();
1554 if(rc != 0)
1555 padico_warning("cannot initialize resovler.\n");
1557 if(rc == -1)
1558 resolvable = 0;
1559 else
1560 resolvable = 1;
1561 }
1562 }
1563 struct topo_hostname_s*hname = padico_malloc(sizeof(struct topo_hostname_s));
1564 hname->name = padico_strdup(_name);
1565 hname->resolvable = resolvable;
1568}
1569
1571{
1572 struct topo_property_s*prop;
1574 size_t name_len = strlen(service_name);
1576 {
1577 prop = *p;
1578 if(prop->kind == TOPO_PROPERTY_SERVICE)
1579 {
1580 padico_topo_service_t service = &prop->content.SERVICE.service;
1582 && service_port == service->port)
1583 return;
1584 }
1585 }
1586 prop = padico_malloc(sizeof(struct topo_property_s) + name_len + 1);
1588 padico_topo_service_t service = &prop->content.SERVICE.service;
1590 service->name[name_len] = 0;
1593}
1594
1596{
1598 if(!find || (find && strcmp(find, value)))
1599 {
1600 struct topo_property_s*prop = padico_malloc(sizeof(struct topo_property_s));
1601 prop->kind = TOPO_PROPERTY_MISC;
1602 prop->content.MISC.label = padico_strdup(label);
1603 prop->content.MISC.value = padico_strdup(value);
1605 }
1606}
1607
1608/* creates a binding between a host and a network */
1616
1617/* creates a binding between a node and a network */
1625
1627{
1630 {
1632 }
1634 padico_free(binding);
1635}
1636
1638{
1639 switch(prop->kind)
1640 {
1642 /* hwaddr belong to host; don't free here */
1643 break;
1645 padico_free(prop->content.HOSTNAME.name);
1646 break;
1647 case TOPO_PROPERTY_MISC:
1648 padico_free(prop->content.MISC.label);
1649 padico_free(prop->content.MISC.value);
1650 break;
1651 default:
1652 break;
1653 }
1655}
1656
1659{
1660 assert(binding->network == b2->network);
1663 {
1664 int found = 0;
1665 struct topo_property_s*prop2 = *p2;
1666 /* check whether props from 'b2' are already present in 'binding' */
1668 {
1669 struct topo_property_s*prop1 = *p1;
1670 if(prop1->kind == prop2->kind)
1671 {
1672 switch(prop1->kind)
1673 {
1675 case TOPO_PROPERTY_ADDR:
1676 found |= (memcmp(prop1->content.ADDR.addr._addr_storage, prop2->content.ADDR.addr._addr_storage,
1677 binding->network->addr_size) == 0);
1678 break;
1681 found |= (prop1->content.HWADDR.hwaddr == prop2->content.HWADDR.hwaddr);
1682 break;
1684 found |= (prop1->content.GW_HOST.gw_host == prop2->content.GW_HOST.gw_host);
1685 break;
1687 found |= ((prop1->content.FILTER.filter.port == prop2->content.FILTER.filter.port) &&
1688 (prop1->content.FILTER.filter.policy == prop2->content.FILTER.filter.policy));
1689 break;
1691 found |= ((prop1->content.SERVICE.service.port == prop2->content.SERVICE.service.port) &&
1692 (strcmp(prop1->content.SERVICE.service.name, prop2->content.SERVICE.service.name) == 0));
1693 break;
1695 found |= (!strcmp(prop1->content.HOSTNAME.name, prop2->content.HOSTNAME.name));
1696 break;
1698 found |= (prop1->content.ROUTE.dest == prop2->content.ROUTE.dest &&
1699 !memcmp(prop1->content.ROUTE.gw._addr_storage, prop2->content.ROUTE.gw._addr_storage,
1700 binding->network->addr_size));
1701 break;
1702 case TOPO_PROPERTY_PERF:
1703 found |= ((prop1->content.PERF.perf.lat == prop2->content.PERF.perf.lat) &&
1704 (prop1->content.PERF.perf.bw == prop2->content.PERF.perf.bw));
1705 break;
1707 found |= (prop1->content.INDEX.index == prop2->content.INDEX.index);
1708 break;
1709 case TOPO_PROPERTY_MISC:
1710 found |= ( (strcmp(prop1->content.MISC.label, prop2->content.MISC.label) == 0) &&
1711 (strcmp(prop1->content.MISC.value, prop2->content.MISC.value) == 0) );
1712 break;
1713 default:
1714 padico_fatal("topo_binding_merge()- unknown property type.\n");
1715 break;
1716 }
1717 }
1718 }
1719 if(!found)
1720 {
1722 }
1723 else
1724 {
1726 }
1727 *p2 = NULL;
1728 }
1731 {
1732 if (*b == b2){
1734 break;
1735 }
1736 }
1739 {
1740 if(*p != NULL)
1742 }
1744 padico_free(b2);
1745}
1746
1748{
1750 assert(binding != NULL);
1751 /* resolve hwaddr stored by name */
1754 {
1755 if(((*p)->kind == TOPO_PROPERTY_HWADDR) && ((*p)->content.HWADDR.hwaddr == NULL))
1756 {
1757 padico_topo_hwaddr_t hwaddr = topo_hwaddr_getbyname(host, (*p)->content.HWADDR.ifname);
1758 (*p)->content.HWADDR.hwaddr = hwaddr;
1759 }
1760 }
1763 {
1764 if((*b)->network == binding->network)
1765 {
1766 const struct topo_address_s*addr1 = topo_binding_getaddr(binding);
1767 const struct topo_address_s*addr2 = topo_binding_getaddr(*b);
1768 if( (addr1->size == addr2->size) &&
1769 (memcmp(&addr1->_addr_storage[0], &addr2->_addr_storage[0], addr1->size) == 0) )
1770 {
1771 topo_binding_merge(*b, binding);
1772 *_binding = *b;
1773 return; /* avoid double-binding */
1774 }
1775 }
1776 }
1778 binding->entity.host = host;
1779 binding->entity.kind = TOPO_ENTITY_HOST;
1781}
1782
1784{
1786 assert(binding != NULL);
1789 {
1790 if((*b)->network == binding->network)
1791 {
1792 const struct topo_address_s*addr1 = topo_binding_getaddr(binding);
1793 const struct topo_address_s*addr2 = topo_binding_getaddr(*b);
1794 if( (addr1->size == addr2->size) &&
1795 (memcmp(&addr1->_addr_storage[0], &addr2->_addr_storage[0], addr1->size) == 0) )
1796 {
1797 topo_binding_merge(*b, binding);
1798 *_binding = *b;
1799 return; /* avoid double-binding */
1800 }
1801 }
1802 }
1804 binding->entity.node = node;
1805 binding->entity.kind = TOPO_ENTITY_NONE;
1807}
1808
1809
1814 const char*uuid,
1815 const char*boot_id)
1816{
1818 padico_print("new node- [%s] %s\n", uuid, name);
1819 /* create a node */
1822 node->host = host;
1825 assert(uuid != NULL);
1828 node->uuid.zero = 0;
1830 if(node_by_uuid != NULL)
1831 {
1832 padico_fatal("uuid = %s colision; old node = %s; new node = %s\n",
1833 uuid, name, node_by_uuid->canonical_nodename);
1834 }
1837 return node;
1838}
1839
1852
1854{
1856 if(network != NULL)
1857 {
1858 padico_warning("duplicate declaration for network %s\n", id);
1859 return network;
1860 }
1861 network = padico_malloc(sizeof(struct topo_network_s));
1865 network->scope = scope;
1866 network->addr_size = addr_size;
1868 network->uplink = NULL;
1869 network->perf.lat = 0;
1870 network->perf.bw = 0;
1872 return network;
1873}
1874
1876{
1877 padico_topo_binding_vect_destroy(&network->bindings); /* don't free bindings here; they belong to node/host */
1879 if(network->uplink)
1880 {
1882 }
1884}
1885
1886/* *** Topology inspectors ********************************* */
1887
1888
1889/* *** local info */
1890
1895
1896/* *** node inspectors */
1897
1911
1916
1921
1926
1931
1940
1941
1942/* *** host inspector */
1943
1945{
1946 return host->hostname;
1947}
1948
1950{
1953 {
1954 if((*i)->network == network)
1955 {
1957 puk_vect_foreach(p, topo_property, &(*i)->props)
1958 {
1959 struct topo_property_s*prop = *p;
1960 if(prop->kind == TOPO_PROPERTY_HOSTNAME)
1961 {
1962 return prop->content.HOSTNAME.name;
1963 }
1964 }
1965 }
1966 }
1969 {
1970 if((*h)->resolvable)
1971 return (*h)->name;
1972 }
1973 const struct in_addr*addr = padico_topo_host_getaddr2(host, network);
1974 return inet_ntoa(*addr);
1975}
1976
1978{
1979 const struct in_addr*inaddr = NULL;
1982 /* find a binding for network 'inet' */
1984 {
1985 if((*i)->network == na_topology.inet)
1986 {
1987 const struct in_addr*_inaddr = topo_address_as_inaddr(topo_binding_getaddr(*i));
1989 if(v > max_v)
1990 {
1991 inaddr = _inaddr;
1992 max_v = v;
1993 }
1994 }
1995 }
1996 /* in case of failure, find a binding for any AF_INET network */
1997 if(!inaddr)
1998 {
2000 {
2001 if((*i)->network->family == AF_INET)
2002 {
2003 const struct in_addr*_inaddr = topo_address_as_inaddr(topo_binding_getaddr(*i));
2005 if(v > max_v)
2006 {
2007 inaddr = _inaddr;
2008 max_v = v;
2009 }
2010 }
2011 }
2012 }
2013 if(!inaddr)
2014 {
2015 padico_warning("host %s has no inet address.\n", host->hostname);
2016 }
2017 return inaddr;
2018}
2019
2033
2035{
2036 const struct in_addr*inaddr = NULL;
2039 {
2040 if((*i)->network == network &&
2041 (*i)->network->family == AF_INET)
2042 {
2044 }
2045 }
2046 return inaddr;
2047}
2048
2053
2068
2070{
2071 padico_topo_filter_policy_t policy = 0;
2072 if((network->family == AF_INET || network->family == AF_INET6) && protocol)
2073 {
2074 /* find binding between host and network */
2075 padico_topo_binding_t binding = NULL;
2076 if(entity.kind == TOPO_ENTITY_HOST)
2077 {
2078 padico_topo_host_t host = entity.host;
2081 {
2082 padico_topo_network_t network2 = (*b)->network;
2083 if(network2 == network)
2084 {
2085 binding = *b;
2086 break;
2087 }
2088 while(network2->uplink != NULL)
2089 {
2090 if(network2->uplink->network == network)
2091 {
2092 binding = network2->uplink;
2093 break;
2094 }
2095 else
2096 {
2097 network2 = network2->uplink->network;
2098 }
2099 }
2100 }
2101 }
2102 else if(entity.kind == TOPO_ENTITY_NETWORK)
2103 {
2104 if(entity.network->uplink != NULL && entity.network->uplink->network == network)
2105 binding = entity.network->uplink;
2106 }
2107 /* compute policy from binding */
2108 if(binding)
2109 {
2112 {
2113 const struct topo_property_s*prop = *p;
2114 if((prop->kind == TOPO_PROPERTY_FILTER)
2115 &&
2116 (prop->content.FILTER.filter.protocol == protocol)
2117 &&
2118 (prop->content.FILTER.filter.port == port ||
2119 prop->content.FILTER.filter.port == PORT_ANY))
2120 {
2121 if( ((prop->content.FILTER.filter.policy & PADICO_TOPO_FILTER_MASK_OUT) && ((policy & PADICO_TOPO_FILTER_MASK_OUT) == 0))
2122 ||
2123 ((prop->content.FILTER.filter.policy & PADICO_TOPO_FILTER_MASK_IN) && ((policy & PADICO_TOPO_FILTER_MASK_IN) == 0)))
2124 {
2125 policy |= prop->content.FILTER.filter.policy;
2126 }
2127 }
2128 }
2129 /* apply default policy */
2130 if(!(policy & PADICO_TOPO_FILTER_MASK_IN))
2132 if(!(policy & PADICO_TOPO_FILTER_MASK_OUT))
2134 }
2135 }
2136 else
2137 {
2138 /* default policy for non-IP network */
2141 }
2142 return policy;
2143}
2144
2145
2153
2161
2163{
2164 struct topo_net_entity_s e;
2165 e.kind = TOPO_ENTITY_NETWORK;
2166 e.network = subnet;
2168}
2169
2171{
2172 struct topo_net_entity_s e;
2173 e.kind = TOPO_ENTITY_NETWORK;
2174 e.network = subnet;
2176}
2177
2179{
2180 struct topo_property_s*prop;
2183 {
2184 prop = *p;
2185 if(prop->kind == TOPO_PROPERTY_SERVICE)
2186 {
2187 padico_topo_service_t service = &prop->content.SERVICE.service;
2189 return service->port;
2190 }
2191 }
2192 return 0;
2193}
2194
2196{
2197 struct topo_property_s*prop;
2200 {
2201 prop = *p;
2202 if(prop->kind == TOPO_PROPERTY_MISC)
2203 {
2204 if(!strcmp((*p)->content.MISC.label, label))
2205 return (*p)->content.MISC.value;
2206 }
2207 }
2208 return NULL;
2209}
2210
2230
2231/* *** network inspector */
2232
2241static inline int padico_topo_network_inet_info(const padico_topo_network_t network, struct in_addr*addr, int*cidr)
2242{
2243 char addr_s[16];
2244 memset(addr_s, 0, 16);
2245 if(sscanf(network->network_id, "inet-[%[^/]/%d]", addr_s, cidr) != 2)
2246 return 0;
2247 return inet_pton(AF_INET, addr_s, addr);
2248}
2250{
2253 {
2254 if((*b)->network == network)
2255 return 1;
2256 }
2257 return 0;
2258}
2267
2269{
2272 {
2273 const struct topo_address_s*addr = topo_binding_getaddr(*b);
2274 if(addr && (memcmp(addr->_addr_storage, _addr, network->addr_size) == 0))
2275 {
2276 return (*b)->entity;
2277 }
2278 }
2280 e.as_ptr = NULL;
2281 return e;
2282}
2283
2285{
2286 if(n->scope == PADICO_TOPO_SCOPE_HOST)
2287 {
2290 {
2291 const topo_net_entity_t e = (*b)->entity;
2292 if(e.kind == TOPO_ENTITY_HOST)
2293 {
2295 }
2296 }
2297 }
2298}
2299
2301{
2302 if(n1->scope == PADICO_TOPO_SCOPE_HOST && n2->scope == PADICO_TOPO_SCOPE_HOST)
2303 {
2306 {
2307 const topo_net_entity_t e1 = (*b1)->entity;
2308 if(e1.kind == TOPO_ENTITY_HOST)
2309 {
2312 {
2313 const topo_net_entity_t e2 = (*b2)->entity;
2314 if(e2.kind == TOPO_ENTITY_HOST && e1.host == e2.host)
2315 {
2317 }
2318 }
2319 }
2320 }
2321 }
2322}
2323
2325{
2326 assert(n != NULL);
2327 if(latency_nsec > 0)
2328 n->perf.lat = latency_nsec;
2329 if(bandwidth_kbps > 0)
2330 n->perf.bw = bandwidth_kbps;
2331}
2332
2334{
2335 if(latency_nsec != NULL)
2336 *latency_nsec = n->perf.lat;
2337 if(bandwidth_kbps != NULL)
2338 *bandwidth_kbps = n->perf.bw;
2339}
2340
2341
2343{
2344 return &address->_addr_storage[0];
2345}
2346
2347/* *** lookup */
2348
2349/* network lookup */
2350
2352{
2353 int i;
2355 {
2357 if(strcmp(network->network_id, network_id) == 0)
2358 {
2359 return network;
2360 }
2361 }
2362 return NULL;
2363}
2364
2379
2380/* node lookup */
2381
2388
2390{
2391 padico_out(50, "looking up node with name: %s\n", nodename);
2393 if(node)
2394 {
2395 padico_out(50, "node found: %s\n", nodename);
2396 return node;
2397 }
2398 padico_out(50, "no node found. Trying hosts.\n");
2400 if(host)
2401 {
2403 }
2404 return NULL;
2405}
2406
2411
2412/* host lookup */
2413
2423
2437
2451
2453{
2454 padico_out(50, "resolving up host with addr: %s\n", inet_ntoa(*addr));
2456 if(!host)
2457 {
2458 /* no host in database with this address- perform a full resolve */
2459 char s_addr[INET_ADDRSTRLEN];
2462 }
2463 return host;
2464}
2465
2467{
2469 if(!host)
2470 {
2471 /* no host in database with this address- perform a full resolve */
2475 }
2476 return host;
2477}
2478
2482static int topo_host_ping(const struct in_addr*inaddr)
2483{
2484 int err = -1;
2485 int ping = 0;
2486 static const int ping_port = 9; /* DISCARD port */
2487 /* open UDP socket */
2489 if(fd < 0)
2490 {
2491 padico_fatal("failed to open socket; err = %s", strerror(errno));
2492 }
2493 struct sockaddr_in addr;
2494 memset(&addr, 0, sizeof(struct sockaddr_in));
2495 addr.sin_addr = *inaddr;
2496 addr.sin_port = htons(ping_port);
2497 addr.sin_family = AF_INET;
2498 int rc = PUK_ABI_FSYS_WRAP(connect)(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in));
2499 if(rc < 0)
2500 {
2501 padico_warning("failed to connect to %s.", inet_ntoa(*inaddr));
2502 goto errors;
2503 }
2504 /* send a msg to nowhere */
2505 char*hello = "hello";
2506 rc = PUK_ABI_FSYS_WRAP(sendto)(fd, hello, strlen(hello), 0, (struct sockaddr*)&addr, sizeof(struct sockaddr_in));
2507 if(rc < 0)
2508 {
2509 padico_warning("failed to send to %s.", inet_ntoa(*inaddr));
2510 goto errors;
2511 }
2512 /* wait for error */
2513 struct pollfd p = { .fd = fd, .events = POLLIN | POLLERR };
2514 rc = PUK_ABI_FSYS_WRAP(poll)(&p, 1, 100 /* milliseconds */);
2515 padico_out(50, "poll()- rc = %d; in = %d; err = %d\n", rc, p.revents & POLLIN, p.revents & POLLERR);
2516 if(rc < 0)
2517 {
2518 padico_warning("poll() failed for ping to %s.", inet_ntoa(*inaddr));
2519 ping = 0;
2520 goto out;
2521 }
2522 if(rc == 0)
2523 {
2524 /* no error; remote host exists */
2525 ping = 1;
2526 goto out;
2527 }
2528 /* get error */
2529#define BUFSIZE 512
2530 char buffer[BUFSIZE];
2531 socklen_t addrlen = sizeof(addr);
2532 rc = PUK_ABI_FSYS_WRAP(recvfrom)(fd, buffer, BUFSIZE, MSG_DONTWAIT, (struct sockaddr*)&addr, &addrlen);
2533 if(rc == 0)
2534 {
2535 /* no error; someone is listening on DISCARD port */
2536 ping = 1;
2537 goto out;
2538 }
2539 else if(rc > 0)
2540 {
2541 /* we received a message */
2542 padico_warning("recvfrom() received unexpected message from %s.\n", inet_ntoa(*inaddr));
2543 ping = 1;
2544 goto out;
2545 }
2546
2547 errors: /* interpret errors */
2548 err = errno;
2549 switch(err)
2550 {
2551 case EHOSTUNREACH:
2552 case ENETUNREACH:
2553 ping = 0;
2554 break;
2555 case ECONNREFUSED: /* connexion was refused, but packet arrived succesfully */
2556 ping = 1;
2557 break;
2558 default:
2559 padico_warning("unknown errno = %d (%s) for ping to %s",
2561 ping = 0;
2562 break;
2563 }
2564
2565 out:
2566 PUK_ABI_FSYS_WRAP(close)(fd);
2567 return ping;
2568}
2569
2572{
2573 assert(he != NULL);
2575 if(!host)
2576 {
2577 padico_print("creating host %s from resolvable host entry.\n", he->h_name);
2578 host = padico_topo_host_create(he->h_name);
2579 /* add aliases */
2580 char**alias = he->h_aliases;
2581 while(*alias)
2582 {
2583 padico_print("adding alias %s for host %s.\n", *alias, host->hostname);
2585 alias++;
2586 }
2587 /* add 'inet' addresses */
2589 if(he)
2590 {
2591 const struct in_addr**addr = (const struct in_addr**)he->h_addr_list;
2592 while(*addr)
2593 {
2596 {
2597 padico_print("adding inet address %s for host %s.\n", inet_ntoa(**addr), host->hostname);
2599 }
2601 {
2602 /* add remote host to private network if it shares a subnet with localhost */
2603 int added = 0;
2607 {
2608 if((*b)->network != na_topology.inet)
2609 {
2610 int cidr;
2611 struct in_addr network_addr;
2612 if(padico_topo_network_inet_info((*b)->network, &network_addr, &cidr) == 1)
2613 {
2614 assert(cidr > 0 && cidr < 32);
2615 uint32_t netmask = (0xffffffff << (32 - cidr));
2617 if(((*addr)->s_addr & netmask) == network_addr.s_addr)
2618 {
2619 padico_print("adding address %s for host %s on network %s.\n",
2620 inet_ntoa(**addr), host->hostname, (*b)->network->network_id);
2621 padico_topo_host_bind(host, (*b)->network, *addr);
2622 added = 1;
2623 break;
2624 }
2625 }
2626 }
2627 }
2628 if(!added)
2629 {
2630 padico_print("not adding address %s for host %s; private network not known by localhost.\n",
2631 inet_ntoa(**addr), host->hostname);
2632 const int ping = topo_host_ping(*addr);
2633 padico_print("ping = %d for remote address %s for host %s.\n", ping, inet_ntoa(**addr), host->hostname);
2634 if(ping)
2635 {
2636 /* remote host pingable, but on a different private network; add it to network with a gw */
2638 {
2639 if((*b)->network != na_topology.inet)
2640 {
2642 puk_vect_foreach(p, topo_property, &(*b)->props)
2643 {
2644 struct topo_property_s*prop = *p;
2645 if(prop->kind == TOPO_PROPERTY_GW_ADDR)
2646 {
2647 /* this private network has a gw; binds the new host to the same network */
2648 padico_print("adding address %s for host %s on network %s through gw.\n",
2649 inet_ntoa(**addr), host->hostname, (*b)->network->network_id);
2650 padico_topo_host_bind(host, (*b)->network, *addr);
2651 break;
2652 }
2653 }
2654 }
2655 }
2656 }
2657 }
2658 }
2659 addr++;
2660 }
2661 }
2662 /* add 'inet6' addresses */
2664 if(he)
2665 {
2666 const struct in6_addr**addr = (const struct in6_addr**)he->h_addr_list;
2667 while(*addr)
2668 {
2671 padico_print("adding inet6 address %s for host %s.\n", s_addr, host->hostname);
2673 addr++;
2674 }
2675 }
2676 }
2677 return host;
2678}
2679
2682{
2683 const struct hostent*he = NULL;
2684 /* try to lookup in topology database. */
2686 if(host)
2687 {
2688 return host;
2689 }
2690 /* try to interpret as IPv4 address */
2691 struct sockaddr_in inaddr;
2692 const int isv4 = inet_pton(AF_INET, hostname, &inaddr);
2693 if(isv4 == 1)
2694 {
2696 if(!host)
2697 {
2698 he = topo_gethostbyaddr(&inaddr, sizeof(struct in_addr), AF_INET);
2699 if(he)
2700 {
2702 }
2703 else
2704 {
2705 padico_print("creating host %s as non-resolvable IPv4 address.\n", hostname);
2706 host = padico_topo_host_create(hostname);
2708 }
2709 }
2710 return host;
2711 }
2712 /* try to interpret as IPv6 address */
2713 struct sockaddr_in6 in6addr;
2714 const int isv6 = inet_pton(AF_INET6, hostname, &in6addr);
2715 if(isv6 == 1)
2716 {
2718 if(!host)
2719 {
2720 he = topo_gethostbyaddr(&in6addr, sizeof(struct in6_addr), AF_INET6);
2721 if(he)
2722 {
2724 }
2725 else
2726 {
2727 padico_print("creating host %s as non-resolvable IPv6 address.\n", hostname);
2728 host = padico_topo_host_create(hostname);
2730 }
2731 }
2732 return host;
2733 }
2734 /* try a DNS request with the given name */
2735 he = topo_gethostbyname2(hostname, AF_INET);
2736 if(!he)
2737 {
2738 /* host is *not* DNS-resolvable- create address-less anyway */
2739 int err = h_errno;
2740 padico_warning("hostname %s cannot be resolved (h_errno=%d, %s). Creating anyway with no address.\n",
2741 hostname, err, hstrerror(err));
2742 host = padico_topo_host_create(hostname);
2743 return host;
2744 }
2745 if(strcmp(he->h_name, hostname) != 0)
2746 {
2747 host = topo_host_lookup(he->h_name);
2748 if(host)
2749 {
2750 /* host was already known with an alternate name- add the name in database */
2751 padico_topo_host_add_name(host, hostname, -1);
2752 return host;
2753 }
2754 }
2755 /* create host from hostent */
2757 return host;
2758}
2759
2760/* *** Disconnection *************************************** */
2761
2767{
2769 if(n == NULL)
2770 return;
2771 padico_print("node lost- [%s] %s\n", node->uuid.bytes, node->canonical_nodename);
2775 {
2776 padico_topo_network_t network = (*v)->network;
2779 {
2780 if((*b) == (*v))
2781 {
2783 break;
2784 }
2785 }
2787 /* do not remove v from its vector since we are iterating on it; the vector
2788 * is trimmed all at once after the loop */
2789 }
2791}
assert(slab_size > 0)
ns_rulebucket_hashtable_t h
struct ns_rulebucket_hash_entry_s * e
ssize_t n
struct ns_rulebucket_hash_entry_s * b
return NULL
Definition Shm.h:88
puk_tagged_ptr_t p
Definition Shm.h:88
uintptr_t s
Definition Shm.h:88
internal definitions for module Topology
static const struct in_addr * topo_address_as_inaddr(padico_topo_address_t addr)
padico_topo_host_t host
static padico_topo_binding_t topo_binding_create(padico_topo_network_t network)
padico_topo_host_t const char const char * boot_id
static padico_topo_hwaddr_t topo_hwaddr_getbyindex(padico_topo_host_t host, int index)
@ TOPO_PROPERTY_MISC
arbitrary attribute (string), without semantics
@ TOPO_PROPERTY_PERF
performance properties- TODO
@ TOPO_PROPERTY_HWADDR
hardware address/iface
@ TOPO_PROPERTY_ADDR
protocol address for the binding
@ TOPO_PROPERTY_FILTER
filtering- TODO
@ TOPO_PROPERTY_ROUTE
describe a route between private networks
@ TOPO_PROPERTY_GW_HWADDR
hardware address of the gateway
@ TOPO_PROPERTY_GW_ADDR
IPv4 address of the gateway.
@ TOPO_PROPERTY_HOSTNAME
name of the host on the network
@ TOPO_PROPERTY_GW_HOST
gateway as a host
@ TOPO_PROPERTY_INDEX
index of the board or port number
@ TOPO_PROPERTY_SERVICE
service which run on a host
padico_topo_binding_t b2
padico_topo_host_t const char * uuid
#define TOPO_HWADDR_NAMSIZ
static padico_topo_hwaddr_t topo_hwaddr_getbyname(padico_topo_host_t host, const char *name)
static padico_topo_node_t peer
static char * buffer
static struct sockaddr_in remote_addr
static struct sockaddr_in local_addr
void *static bench_vect_buffer_queue_t v
Definition bench_vect.c:22
static int done
Definition console-mod.c:49
static int rank
Definition datacheck.c:35
static int fd
Definition datacheck.c:32
char * name
Definition PadicoTM.h:168
#define PUK_ABI_FSYS_WRAP(SYM)
Definition PadicoTM.h:88
padico_rc_t rc
Definition PadicoTM.h:146
#define __puk_abi_wrap__errno
Definition PadicoTM.h:101
int padico_topo_host_can_output(padico_topo_host_t host, padico_topo_network_t network, uint16_t port, int protocol)
Verify if the port is filtered output between host and network.
Definition topology.c:2154
static padico_topo_node_t padico_topo_host_getnode(padico_topo_host_t host)
Get the first node of a host.
Definition Topology.h:220
#define AF_PADICO_PAYLOAD
Definition Topology.h:429
int padico_sockaddr_check(const struct sockaddr_pa *addr, int magic)
Definition topology.c:337
void padico_topo_network_get_perf(padico_topo_network_t n, int *latency_nsec, int *bandwidth_kbps)
Definition topology.c:2333
padico_topo_host_vect_t padico_topo_gethosts(void)
Get known hosts.
Definition topology.c:2211
void padico_topo_binding_add_perf(padico_topo_binding_t binding, int latency, int bandwidth)
Definition topology.c:273
padico_topo_node_t padico_topo_network_getnodebyaddr(padico_topo_network_t network, const void *_addr)
Get a node descriptor from an address.
Definition topology.c:2382
int padico_topo_network_can_output(padico_topo_network_t subnet, padico_topo_network_t network, uint16_t port, int protocol)
Definition topology.c:2170
#define PADICO_TOPO_FILTER_MASK_OUT
Definition Topology.h:118
#define AF_PADICO_MAKE(I)
Definition Topology.h:416
padico_topo_node_vect_t padico_topo_host_getnodes(padico_topo_host_t host)
Get the nodes on a host.
Definition topology.c:2049
void padico_topo_binding_add_addr(padico_topo_binding_t binding, const void *addr)
Definition topology.c:183
int padico_topo_host_can_input(padico_topo_host_t host, padico_topo_network_t network, uint16_t port, int protocol)
Verify if the port is filtered input between host and network.
Definition topology.c:2146
void padico_topo_host_add_name(padico_topo_host_t host, const char *_name, int resolvable)
Add a name to a host;.
Definition topology.c:1525
void padico_topo_node_getnetworks(padico_topo_node_t node, padico_topo_network_vect_t networks)
Get the networks a node is connected to.
Definition topology.c:1932
int padico_topo_network_size(padico_topo_network_t network)
Definition topology.c:2237
typdef struct padico_topo_binding_vect_s * padico_topo_binding_vect_t
Definition Topology.h:87
void padico_topo_host_add_service(padico_topo_host_t host, const char *service_name, uint16_t service_port)
Add a service to a host.
Definition topology.c:1570
padico_topo_addr_visibility_t
Definition Topology.h:100
padico_topo_network_t padico_topo_network_create(const char *id, padico_topo_binding_scope_t scope, size_t addr_size, int family)
Definition topology.c:1853
padico_topo_host_t padico_topo_host_resolve_byinaddr(const struct in_addr *addr)
Get a host description record from an inet address (create if it didn't exist)
Definition topology.c:2452
padico_topo_host_t padico_topo_network_gethostbyaddr(padico_topo_network_t network, const void *_addr)
Get a host description record from an address.
Definition topology.c:2414
void padico_sockaddr_init(struct sockaddr_pa *addr, socklen_t *addrlen, int magic)
Definition topology.c:329
padico_topo_node_t padico_topo_getlocalnode(void)
Definition topology.c:1891
#define PADICO_TOPO_FILTER_MASK_IN
Definition Topology.h:117
#define PADICO_TOPO_UUID_SIZE
Definition Topology.h:73
const char * padico_topo_node_getname(padico_topo_node_t node)
Get the canonical name of a node.
Definition topology.c:1917
padico_topo_host_t padico_topo_node_gethost(padico_topo_node_t node)
Get the host hosting a node.
Definition topology.c:1927
typdef struct padico_topo_host_vect_s * padico_topo_host_vect_t
Definition Topology.h:86
padico_topo_filter_policy_t
Definition Topology.h:109
padico_topo_binding_vect_t padico_topo_host_getbindings(padico_topo_host_t host, padico_topo_network_t network)
Get the bindings of a host on a network.
Definition topology.c:2020
padico_topo_host_t padico_topo_host_getbyin6addr(const struct in6_addr *addr)
Get a host from an inet6 address (return NULL if host doesn't exist).
Definition topology.c:2438
padico_topo_address_t padico_topo_node_getaddress(padico_topo_node_t node, padico_topo_network_t network)
Get the address of the node in the given network.
Definition topology.c:1898
padico_topo_network_vect_t padico_topo_networks_getbyfamily(int family)
Get networks of a given address family (e.g.
Definition topology.c:2365
void padico_topo_host_getnetworks(padico_topo_host_t host, padico_topo_network_vect_t networks)
Get the networks a host is connected to.
Definition topology.c:2054
void padico_topo_binding_add_gw_addr(padico_topo_binding_t binding, struct in_addr *inaddr)
Definition topology.c:213
padico_topo_hwaddr_t padico_topo_hwaddr_create(const char *name, int index, int type, size_t len, const void *addr)
Definition topology.c:126
const char * padico_topo_network_getname(padico_topo_network_t network)
Definition topology.c:2233
const char * padico_topo_host_getproperty(padico_topo_host_t host, const char *label)
Get property on a host.
Definition topology.c:2195
padico_topo_network_t padico_topo_network_getbyid(const char *network_id)
Definition topology.c:2351
const char * padico_topo_host_getname2(padico_topo_host_t host, padico_topo_network_t network)
Get the canonical name of a host on a network.
Definition topology.c:1949
void padico_topo_binding_add_hostname(padico_topo_binding_t binding, const char *name)
Definition topology.c:240
void padico_topo_binding_add_filter(padico_topo_binding_t binding, padico_topo_filter_policy_t policy, uint16_t port, int protocol)
Definition topology.c:262
padico_topo_host_t padico_topo_host_getbyinaddr(const struct in_addr *addr)
Get a host from an inet address (return NULL if host doesn't exist).
Definition topology.c:2424
#define padico_topo_getlocalhost()
Definition Topology.h:319
void padico_topo_hwaddr_get(padico_topo_hwaddr_t hwaddr, const char **name, int *index, int *type, size_t *len, const void **addr)
Definition topology.c:139
padico_topo_addr_visibility_t padico_topo_inet6_visibility(const struct in6_addr *in6addr)
Definition topology.c:1215
void padico_topo_network_set_perf(padico_topo_network_t n, int latency_nsec, int bandwidth_kbps)
Definition topology.c:2324
void padico_topo_node_disconnect(padico_topo_node_t node)
Handle node disconnection.
Definition topology.c:2766
int padico_topo_network_contains_node(padico_topo_network_t network, padico_topo_node_t node)
tests whether the given node belong to the network
Definition topology.c:2259
const struct in_addr * padico_topo_host_getaddr(padico_topo_host_t host)
Get the canonical IP address of a host.
Definition topology.c:1977
const void * padico_topo_address_getbytes(padico_topo_address_t address)
Definition topology.c:2342
typdef struct padico_topo_node_vect_s * padico_topo_node_vect_t
Definition Topology.h:84
void padico_topo_network_gethosts(padico_topo_network_t n, padico_topo_host_vect_t hosts)
Definition topology.c:2284
padico_topo_uuid_t padico_topo_node_getuuid(padico_topo_node_t node)
Get the UUID of a node.
Definition topology.c:1912
padico_topo_host_t padico_topo_host_create(const char *hostname)
Create a new 'host' record from a given canonical name.
Definition topology.c:1463
void padico_topo_network_get_common_host(padico_topo_network_t n1, padico_topo_network_t n2, padico_topo_host_vect_t hosts)
Definition topology.c:2300
padico_topo_binding_scope_t
Definition Topology.h:92
padico_topo_binding_t padico_topo_node_bind(padico_topo_node_t node, padico_topo_network_t network, const void *address)
Declare a new binding for a node.
Definition topology.c:1618
int padico_topo_network_can_input(padico_topo_network_t subnet, padico_topo_network_t network, uint16_t port, int protocol)
Definition topology.c:2162
void padico_topo_hwaddr_add(padico_topo_host_t host, padico_topo_hwaddr_t *_hwaddr)
Definition topology.c:153
padico_topo_host_t padico_topo_host_resolve_byin6addr(const struct in6_addr *addr)
Get a host description record from an inet6 address (create if it didn't exist)
Definition topology.c:2466
void padico_topo_binding_add_index(padico_topo_binding_t binding, int64_t index)
Definition topology.c:205
void padico_topo_binding_get_perf(padico_topo_binding_t binding, int *latency_nsec, int *bandwidth_kbps)
Definition topology.c:282
padico_topo_addr_visibility_t padico_topo_inet_visibility(in_addr_t _inaddr)
Definition topology.c:1197
void padico_topo_hwaddr_destroy(padico_topo_hwaddr_t hwaddr)
Definition topology.c:175
int padico_topo_host_getservice(padico_topo_host_t host, char *service_name)
Get the service port of the service on a host.
Definition topology.c:2178
padico_topo_node_t padico_topo_getnodebyname(const char *nodename)
node lookup, by node name
Definition topology.c:2389
padico_topo_node_t padico_topo_getnodebyuuid(padico_topo_uuid_t uuid)
node lookup, by UUID
Definition topology.c:2407
const char * padico_topo_host_getname(padico_topo_host_t host)
Get the canonical name of a host.
Definition topology.c:1944
padico_topo_binding_t padico_topo_host_bind(padico_topo_host_t host, padico_topo_network_t network, const void *address)
Declare a new binding for a host.
Definition topology.c:1609
void padico_topo_host_add_property(padico_topo_host_t host, const char *label, const char *value)
Add a property to a host.
Definition topology.c:1595
padico_topo_hwaddr_t padico_topo_binding_gethwaddr(padico_topo_binding_t binding)
Definition topology.c:299
const struct in_addr * padico_topo_host_getaddr2(padico_topo_host_t host, padico_topo_network_t network)
Get the canonical IP address of a host on a network.
Definition topology.c:2034
padico_topo_host_t padico_topo_host_resolvebyname(const char *hostname)
resolve a host from name
Definition topology.c:2681
void padico_topo_binding_add_route(padico_topo_binding_t binding, padico_topo_network_t dest, const void *gwaddr)
Definition topology.c:248
int padico_topo_binding_get_index(padico_topo_binding_t binding, int64_t *index)
Definition topology.c:313
#define AF_PADICO
Definition Topology.h:409
typdef struct padico_topo_network_vect_s * padico_topo_network_vect_t
Definition Topology.h:85
const char * padico_topo_node_getsession(padico_topo_node_t node)
Get the boot session ID.
Definition topology.c:1922
void padico_topo_binding_add_hwaddr(padico_topo_binding_t binding, padico_topo_hwaddr_t hwaddr)
Definition topology.c:196
#define PORT_ANY
Definition Topology.h:122
@ PADICO_TOPO_ADDR_VISIBILITY_PRIVATE
private address- some node/host cannot see it
Definition Topology.h:104
@ PADICO_TOPO_ADDR_VISIBILITY_PUBLIC
public address
Definition Topology.h:105
@ PADICO_TOPO_ADDR_VISIBILITY_NONE
Definition Topology.h:101
@ PADICO_TOPO_ADDR_VISIBILITY_LOOPBACK
loopback address- only local node/host can see it
Definition Topology.h:102
@ PADICO_TOPO_ADDR_VISIBILITY_LINKLOCAL
link-local address- nodes on the same physical link can see it
Definition Topology.h:103
@ PADICO_TOPO_FILTER_ALLOW_OUT
Definition Topology.h:114
@ _PADICO_TOPO_FILTER_MAX
Definition Topology.h:115
@ PADICO_TOPO_FILTER_BLOCK_IN
Definition Topology.h:111
@ PADICO_TOPO_FILTER_ALLOW_IN
Definition Topology.h:113
@ _PADICO_TOPO_FILTER_NONE
Definition Topology.h:110
@ PADICO_TOPO_SCOPE_HOST
Definition Topology.h:95
@ PADICO_TOPO_SCOPE_NODE
Definition Topology.h:94
padico_topo_node_t node
Definition na-control.c:65
static const uint8_t magic[4]
Definition ps.c:80
struct @25::@27 recv
const int timeout
Definition psp-mx.c:119
mx_request_t req
Definition psp-mx.c:103
padico_topo_network_t network
Definition psp-mx.c:113
int pid
Definition sf-srtun.c:112
Shared structure to describes the "known world".
padico_topo_network_t inet6
global network- IPv6 internet
struct topo_host_by_name_hashtable_s hostsbyname
index of hosts, hashed by name (there may be duplicate entries)
padico_topo_network_t inet
global network- public IPv4 internet
padico_topo_node_t local_node
local node descriptor
padico_topo_network_vect_t networks
vector of known networks
struct topo_node_by_name_hashtable_s nodesbyname
index of nodes, hashed by nodename (canonical+alias, there may be duplicate entries)
struct topo_host_by_name_hashtable_s host_table
index of hosts, hashed by fqdn
struct topo_node_by_uuid_hashtable_s node_table
index of node, hashed by UUID
uint32_t spa_magic
Definition Topology.h:434
a network address used in bindings
char _addr_storage[1]
placeholder for address- more bytes are actually allocated
int family
address family, using AF_*
a binding of an entity with a network (protocol level)
struct topo_property_vect_s props
binding properties, e.g.
padico_topo_network_t network
container network
topo_net_entity_t entity
contained entity
padico_topo_filter_policy_t policy
struct topo_hostname_vect_s hnames
names of the host, including short hostname and fqdn
struct padico_topo_node_vect_s nodes
nodes on this host
struct topo_property_vect_s props
host properties, e.g service
struct padico_topo_binding_vect_s bindings
protocol bindings
const char * hostname
canonical fully qualified domain name, as reported by gethostbyname(3)
struct topo_hwaddr_vect_s hwaddrs
hardware addresses
a hardware address- identifies a host
char name[16]
hw interface name, as seen through netdevice interface
unsigned short int type
ARPHRD_* as defined in <net/if_arp.h>
char _hwaddr_storage[1]
placeholder area for address
int index
hw interface number, as seen through rtnetlink interface
size_t len
actual length of hardware address
an entity that can be member of a network through a binding
padico_topo_node_t node
enum topo_net_entity_s::@87 kind
padico_topo_host_t host
padico_topo_network_t network
a network, containing entities with bindings
size_t addr_size
size of the addresses on the given network
int family
address family used on tyhe network- AF_UNSPEC if unknown
padico_topo_binding_scope_t scope
scope (host/node) of the bindings on this network
const char * network_id
session ID of the network
struct topo_perf_s perf
estimated network performance
struct topo_binding_s * uplink
uplink to another network, if any
struct padico_topo_binding_vect_s bindings
bindings (nodes/hosts) of this network
padico_topo_host_t host
host containing this node
const char * canonical_nodename
fqdn_hostname:num
struct padico_topo_binding_vect_s bindings
network bindings of the node
const char * boot_id
session ID identifying our 'padico-launch' instance
struct topo_uuid_s uuid
universally unique ID for the node
int bw
network bandwidth in KB/s
int lat
network latency in nanoseconds
struct topo_property_s::@90::@98 PERF
padico_topo_hwaddr_t hwaddr
pointer to hwaddr allocated in host
struct topo_address_s addr
union topo_property_s::@90 content
struct topo_property_s::@90::@96 HOSTNAME
struct topo_property_s::@90::@92 FILTER
for FILTER*
struct topo_property_s::@90::@99 INDEX
struct topo_service_s service
struct topo_filter_s filter
padico_topo_network_t dest
struct topo_perf_s perf
struct topo_property_s::@90::@93 HWADDR
struct topo_property_s::@90::@91 ADDR
struct topo_property_s::@90::@100 MISC
for MISC
a service which running on a host
Node UUID, in plain text (hex encoding)
struct padico_topology_s * topology_get(void)
Definition topology.c:86
static puk_opt_type_bool_t padico_module_attr_disable_ipv6_getvalue(void)
Definition topology.c:81
static padico_topo_host_t topo_host_from_hostent(const struct hostent *he)
Create a new host from host entry.
Definition topology.c:2571
static int na_bindings_vect_contains_network(struct padico_topo_binding_vect_s *bindings, padico_topo_network_t network)
Definition topology.c:2249
int padico_topo_network_contains_host(padico_topo_network_t network, padico_topo_host_t host)
Definition topology.c:2263
static const char * topo_addr_visibility_name(padico_topo_addr_visibility_t v)
inet address visibility pretty print
Definition topology.c:1228
static void topo_host_destroy(padico_topo_host_t host)
Definition topology.c:1478
void topo_binding_merge(padico_topo_binding_t binding, padico_topo_binding_t b2)
merge properties from b2 into 'binding', then destroy b2
Definition topology.c:1658
static void topo_detect_link_perf(padico_topo_binding_t b, const padico_topo_hwaddr_t hwaddr)
Definition topology.c:480
static const char * topo_hwaddr_type_name(int type)
MAC address type pretty print.
Definition topology.c:1248
static void topo_device_detect(void)
Definition topology.c:1068
void padico_topo_binding_add_gw_hwaddr(padico_topo_binding_t binding, padico_topo_hwaddr_t hwaddr)
Definition topology.c:232
static int padico_topo_network_inet_info(const padico_topo_network_t network, struct in_addr *addr, int *cidr)
Definition topology.c:2241
static puk_opt_type_bool_t padico_module_attr_disable_dns_getvalue(void)
Definition topology.c:80
void topo_binding_bind_node(padico_topo_binding_t *_binding, padico_topo_node_t node)
Definition topology.c:1783
static const struct hostent * topo_gethostbyname2(const char *name, int family)
frontend to gethostbyname2 to manage TRY_AGAIN error
Definition topology.c:438
static struct padico_topology_s na_topology
Definition topology.c:84
static int topo_host_ping(const struct in_addr *inaddr)
try to ping a host to detect whether it is reachable.
Definition topology.c:2482
void topo_binding_bind_host(padico_topo_binding_t *_binding, padico_topo_host_t host)
Definition topology.c:1747
static void topo_inet_route_detect(padico_topo_host_t localhost)
Definition topology.c:843
static void topology_populate(void)
Definition topology.c:1286
padico_topo_node_t topo_node_create(const char *name, padico_topo_host_t host, const char *uuid, const char *boot_id)
Create a new node, either locally or received as XML description.
Definition topology.c:1812
static int topo_inet_addr_detect_netlink(padico_topo_host_t localhost)
detect network interfaces using netlink socket
Definition topology.c:685
static void topo_inet_addr_detect_siocgifconf(padico_topo_host_t localhost)
detect network interfaces using netdevice ioctl
Definition topology.c:649
static void topo_inet_addr_detect(padico_topo_host_t localhost)
Definition topology.c:829
static padico_topo_filter_policy_t padico_topo_host_policy(topo_net_entity_t entity, padico_topo_network_t network, uint16_t port, int protocol)
Definition topology.c:2069
static int padico_topology_init(void)
Definition topology.c:364
static puk_opt_type_bool_t padico_module_attr_autodetect_getvalue(void)
Definition topology.c:82
static const struct hostent * topo_gethostbyaddr(const void *addr, socklen_t len, int family)
frontend to gethostbyaddr
Definition topology.c:412
static topo_net_entity_t na_network_getentity_byaddr(padico_topo_network_t network, const void *_addr)
Definition topology.c:2268
static void topo_network_destroy(padico_topo_network_t network)
Definition topology.c:1875
void topo_property_destroy(struct topo_property_s *prop)
Definition topology.c:1637
static void padico_topology_finalize(void)
Definition topology.c:378
#define BUFSIZE
static int topo_inet_addr_detect_getifaddrs(padico_topo_host_t localhost)
detect network interfaces using Linux getifaddrs, if available
Definition topology.c:503
void topo_binding_destroy(struct topo_binding_s *binding)
Definition topology.c:1626
static void topo_inet_service_detect(padico_topo_host_t localhost)
Definition topology.c:1031
padico_topo_host_t topo_host_lookup(const char *hostname)
Definition topology.c:1510
void topo_node_destroy(padico_topo_node_t node)
Definition topology.c:1840
static int sockfd
Definition tube_gk-mod.c:46
static int port
Definition vrp-test.c:15