1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
|
/*
* Copyright (c) 2014-2016 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2015 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2018-2020 Amazon.com, Inc. or its affiliates. All Rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <assert.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#endif
#include "opal/mca/threads/mutex.h"
#include "opal/types.h"
#include "opal/util/event.h"
#include "opal/util/fd.h"
#include "opal/util/output.h"
#include "opal/util/printf.h"
#include "opal/util/show_help.h"
#include "opal/util/string_copy.h"
#include "opal_stdint.h"
#include "btl_usnic.h"
#include "btl_usnic_connectivity.h"
/**************************************************************************
* Agent data and methods
**************************************************************************/
/*
* Local variables
*/
static int ipc_accept_fd = -1;
static char *ipc_filename = NULL;
static opal_event_t ipc_event;
static struct timeval ack_timeout;
static opal_list_t udp_port_listeners;
static opal_list_t ipc_listeners;
static volatile uint32_t ipc_accepts = 0;
/* JMS The pings_pending and ping_results should probably both be hash
tables for more efficient lookups */
static opal_list_t pings_pending;
static opal_list_t ping_results;
static volatile bool agent_initialized = false;
/*
* Holds all the information about a UDP port that the agent thread is
* listening on (for incoming PINGs and ACKs).
*/
typedef struct {
opal_list_item_t super;
/* Data from the LISTEN command message */
uint32_t ipv4_addr;
uint32_t netmask;
char ipv4_addr_str[IPV4STRADDRLEN];
uint32_t max_msg_size;
char *nodename;
char *usnic_name;
/* File descriptor, UDP port, buffer to receive messages, and event */
int fd;
uint32_t udp_port;
uint8_t *buffer;
opal_event_t event;
bool active;
opal_btl_usnic_module_t *module;
} agent_udp_port_listener_t;
OBJ_CLASS_DECLARATION(agent_udp_port_listener_t);
/*
* Holds information for a local IPC socket fd (i.e., a connection
* from a local process to this agent).
*/
typedef struct {
opal_list_item_t super;
int client_fd;
opal_event_t event;
bool active;
} agent_ipc_listener_t;
OBJ_CLASS_DECLARATION(agent_ipc_listener_t);
typedef enum { AGENT_MSG_TYPE_PING = 17, AGENT_MSG_TYPE_ACK } agent_udp_message_type_t;
// Arbitrary 64 bit numbers
#define MAGIC_ORIGINATOR 0x9a9e2fbce63a11e5
#define MAGIC_TARGET 0x60735c68f368aace
/*
* Ping and ACK messages
*/
typedef struct {
uint8_t message_type;
/* The sender's IP address and port (i.e., where the ACK can be
sent). This is actually redundant with the sockaddr that we
get from recvfrom(), but that's ok -- it provides a sanity
check. */
uint32_t src_ipv4_addr;
uint32_t src_udp_port;
/* A magic number that helps determine that the sender was Open
MPI */
uint64_t magic_number;
uint32_t major_version, minor_version;
/* If this is a PING, the message should be this size.
If this is an ACK, we are ACKing a ping of this size. */
uint32_t size;
} agent_udp_message_t;
typedef struct {
opal_list_item_t super;
/* Data from the PING command message */
uint32_t src_ipv4_addr; /* in network byte order */
uint32_t src_udp_port;
agent_udp_port_listener_t *listener;
uint32_t dest_ipv4_addr; /* in network byte order */
uint32_t dest_netmask;
uint32_t dest_udp_port;
struct sockaddr_in dest_sockaddr;
char *dest_nodename;
/* The sizes and corresponding buffers of the PING messages that
we'll send, and whether each of those PING messages have been
ACKed yet */
#define NUM_PING_SIZES 2
size_t sizes[NUM_PING_SIZES];
uint8_t *buffers[NUM_PING_SIZES];
bool acked[NUM_PING_SIZES];
/* Number of times we've sent this ping */
int num_sends;
/* Timer used to re-send the PING, and whether the timer is active
or not */
opal_event_t timer;
bool timer_active;
} agent_ping_t;
OBJ_CLASS_DECLARATION(agent_ping_t);
/**************************************************************************
* Utility functions, constructors, destructors
**************************************************************************/
static void udp_port_listener_zero(agent_udp_port_listener_t *obj)
{
obj->ipv4_addr = obj->netmask = obj->max_msg_size = 0;
obj->nodename = obj->usnic_name = NULL;
memset(obj->ipv4_addr_str, 0, sizeof(obj->ipv4_addr_str));
obj->fd = -1;
obj->udp_port = -1;
obj->buffer = NULL;
obj->active = false;
}
static void udp_port_listener_constructor(agent_udp_port_listener_t *obj)
{
udp_port_listener_zero(obj);
}
static void udp_port_listener_destructor(agent_udp_port_listener_t *obj)
{
/* Find any pings that are pending on this listener and delete
them */
agent_ping_t *ap, *apnext;
OPAL_LIST_FOREACH_SAFE (ap, apnext, &pings_pending, agent_ping_t) {
if (ap->src_ipv4_addr == obj->ipv4_addr) {
opal_list_remove_item(&pings_pending, &ap->super);
OBJ_RELEASE(ap);
}
}
if (-1 != obj->fd) {
close(obj->fd);
}
if (NULL != obj->nodename) {
free(obj->nodename);
}
if (NULL != obj->usnic_name) {
free(obj->usnic_name);
}
if (NULL != obj->buffer) {
free(obj->buffer);
}
/* If the "active" flag is set, then the event is active and the
item is on the udp_port_listeners list */
if (obj->active) {
opal_event_del(&obj->event);
opal_list_remove_item(&udp_port_listeners, &obj->super);
}
udp_port_listener_zero(obj);
}
OBJ_CLASS_INSTANCE(agent_udp_port_listener_t, opal_list_item_t, udp_port_listener_constructor,
udp_port_listener_destructor);
static void ipc_listener_zero(agent_ipc_listener_t *obj)
{
obj->client_fd = -1;
obj->active = false;
}
static void ipc_listener_constructor(agent_ipc_listener_t *obj)
{
ipc_listener_zero(obj);
}
static void ipc_listener_destructor(agent_ipc_listener_t *obj)
{
if (-1 != obj->client_fd) {
close(obj->client_fd);
}
/* If the "active" flag is set, then the event is active and the
item is on the ipc_listeners list */
if (obj->active) {
opal_event_del(&obj->event);
opal_list_remove_item(&ipc_listeners, &obj->super);
}
ipc_listener_zero(obj);
}
OBJ_CLASS_INSTANCE(agent_ipc_listener_t, opal_list_item_t, ipc_listener_constructor,
ipc_listener_destructor);
static void agent_ping_result_zero(agent_ping_t *obj)
{
obj->src_ipv4_addr = 0;
obj->src_udp_port = 0;
obj->listener = NULL;
obj->dest_ipv4_addr = 0;
obj->dest_udp_port = 0;
obj->num_sends = 0;
obj->timer_active = false;
for (int i = 0; i < NUM_PING_SIZES; ++i) {
obj->sizes[i] = 0;
obj->buffers[i] = NULL;
obj->acked[i] = false;
}
}
static void agent_ping_result_constructor(agent_ping_t *obj)
{
agent_ping_result_zero(obj);
}
static void agent_ping_result_destructor(agent_ping_t *obj)
{
for (int i = 0; i < NUM_PING_SIZES; ++i) {
if (NULL != obj->buffers[i]) {
free(obj->buffers[i]);
}
}
if (obj->timer_active) {
opal_event_del(&obj->timer);
}
agent_ping_result_zero(obj);
}
OBJ_CLASS_INSTANCE(agent_ping_t, opal_list_item_t, agent_ping_result_constructor,
agent_ping_result_destructor);
/*
* Wrapper around sendto() loop
*/
static void agent_sendto(int fd, char *buffer, ssize_t numbytes, struct sockaddr *addr)
{
ssize_t rc;
while (1) {
rc = sendto(fd, buffer, numbytes, 0, addr, sizeof(*addr));
/* Note that since this is UDP, so we don't need to check
for 0 < rc < numbytes */
if (rc == numbytes) {
return;
} else if (rc < 0) {
if (errno == EAGAIN || errno == EINTR) {
continue;
} else if (errno == EPERM) {
// We're sending too fast
usleep(5);
continue;
}
char *msg;
opal_asprintf(&msg, "Unexpected sendto() error: errno=%d (%s)", errno, strerror(errno));
ABORT(msg);
/* Will not return */
}
/* We should never get here, but just in case we do, sleep a
little, just so we don't hammer the CPU */
usleep(1);
}
/* Will not get here */
}
/**************************************************************************
* All of the following functions run in agent thread
**************************************************************************/
/*
* Handle an incoming PING message (send an ACK)
*/
static void agent_thread_handle_ping(agent_udp_port_listener_t *listener, ssize_t numbytes,
struct sockaddr *from)
{
/* If the size we received isn't equal to what the sender says it
sent, do the simple thing: just don't send an ACK */
agent_udp_message_t *msg = (agent_udp_message_t *) listener->buffer;
struct sockaddr_in *src_addr_in = (struct sockaddr_in *) from;
if (msg->size != numbytes) {
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &src_addr_in->sin_addr, str, sizeof(str));
opal_output_verbose(
20, USNIC_OUT,
"usNIC connectivity got bad ping: %d bytes from %s, expected %d (discarded)",
(int) numbytes, str, (int) msg->size);
return;
}
/* Ensure that the sender sent the ping from the IP address that
they think they sent it from. If they didn't, then drop it
(i.e., it's a bad ping because the sender sent it from an
unexpected interface). This should probably never happen, but
it's a good failsafe for unexpected scenarios. */
char msg_ipv4_addr_str[IPV4STRADDRLEN];
char real_ipv4_addr_str[IPV4STRADDRLEN];
opal_btl_usnic_snprintf_ipv4_addr(msg_ipv4_addr_str, sizeof(msg_ipv4_addr_str),
msg->src_ipv4_addr, 0);
opal_btl_usnic_snprintf_ipv4_addr(real_ipv4_addr_str, sizeof(real_ipv4_addr_str),
src_addr_in->sin_addr.s_addr, 0);
if (msg->src_ipv4_addr != src_addr_in->sin_addr.s_addr) {
opal_output_verbose(
20, USNIC_OUT,
"usNIC connectivity got bad ping (from unexpected address: %s != %s, discarded)",
msg_ipv4_addr_str, real_ipv4_addr_str);
return;
}
if (msg->magic_number != MAGIC_ORIGINATOR) {
opal_output_verbose(20, USNIC_OUT,
"usNIC connectivity got bad ping (magic number: %" PRIu64
", discarded)",
msg->magic_number);
return;
}
if (msg->major_version != OPAL_MAJOR_VERSION || msg->minor_version != OPAL_MINOR_VERSION) {
opal_output_verbose(20, USNIC_OUT,
"usNIC connectivity got bad ping (originator version: %d.%d, expected "
"%d.%d, discarded)",
msg->major_version, msg->minor_version, OPAL_MAJOR_VERSION,
OPAL_MINOR_VERSION);
return;
}
/* Ok, this is a good ping. Send the ACK back. The PING sender
will verify that the ACK came back from the IP address that it
expected. */
opal_output_verbose(20, USNIC_OUT,
"usNIC connectivity got PING (size=%ld) from %s; sending ACK", numbytes,
msg_ipv4_addr_str);
/* Send back an ACK. No need to allocate a new buffer; just
re-use the same buffer we just got. Note that msg->size is
already set. We simply echo back the sender's IP address/port
in the msg (the sender will use the msg fields and the
recvfrom() src_addr to check for a match). */
msg->message_type = AGENT_MSG_TYPE_ACK;
msg->magic_number = MAGIC_TARGET;
agent_sendto(listener->fd, (char *) listener->buffer, sizeof(*msg), from);
}
/*
* Handle an incoming ACK message
*/
static void agent_thread_handle_ack(agent_udp_port_listener_t *listener, ssize_t numbytes,
struct sockaddr *from)
{
char str[INET_ADDRSTRLEN];
struct sockaddr_in *src_addr_in = (struct sockaddr_in *) from;
inet_ntop(AF_INET, &src_addr_in->sin_addr, str, sizeof(str));
/* If we got a wonky ACK message that is the wrong length, just
return */
agent_udp_message_t *msg = (agent_udp_message_t *) listener->buffer;
if (numbytes != sizeof(*msg)) {
opal_output_verbose(
20, USNIC_OUT,
"usNIC connectivity got bad ACK: %d bytes from %s, expected %d (discarded)",
(int) numbytes, str, (int) sizeof(*msg));
return;
}
if (msg->magic_number != MAGIC_TARGET) {
opal_output_verbose(20, USNIC_OUT,
"usNIC connectivity got bad ACK (magic number: %" PRIu64 ", discarded)",
msg->magic_number);
return;
}
/* Find the pending ping request (on this interface) for this ACK.
If we don't find a match, we'll drop it. */
agent_ping_t *ap;
uint32_t src_in_port = ntohs(src_addr_in->sin_port);
OPAL_LIST_FOREACH (ap, &pings_pending, agent_ping_t) {
if (ap->dest_ipv4_addr == src_addr_in->sin_addr.s_addr && ap->dest_udp_port == src_in_port
&& ap->src_ipv4_addr == msg->src_ipv4_addr && ap->src_udp_port == msg->src_udp_port) {
/* Found it -- indicate that it has been acked */
for (int i = 0; i < NUM_PING_SIZES; ++i) {
if (ap->sizes[i] == msg->size) {
ap->acked[i] = true;
return;
}
}
}
}
/* If we didn't find the matching ping for this ACK, then just
discard it */
opal_output_verbose(20, USNIC_OUT,
"usNIC connectivity got unexpected ACK: %d bytes from %s (discarded)",
(int) numbytes, str);
}
/*
* Receive a message from the listening UDP socket
*/
static void agent_thread_receive_ping(int fd, short flags, void *context)
{
agent_udp_port_listener_t *listener = (agent_udp_port_listener_t *) context;
assert(NULL != listener);
/* Receive the message */
ssize_t numbytes;
struct sockaddr src_addr;
struct sockaddr_in *src_addr_in = (struct sockaddr_in *) &src_addr;
socklen_t addrlen = sizeof(src_addr);
while (1) {
numbytes = recvfrom(listener->fd, listener->buffer, listener->max_msg_size, 0, &src_addr,
&addrlen);
if (numbytes > 0) {
break;
} else if (numbytes < 0) {
if (errno == EAGAIN || errno == EINTR) {
continue;
}
ABORT("Unexpected error from recvfrom");
/* Will not return */
}
}
char str[INET_ADDRSTRLEN];
agent_udp_message_t *msg;
msg = (agent_udp_message_t *) listener->buffer;
switch (msg->message_type) {
case AGENT_MSG_TYPE_PING:
agent_thread_handle_ping(listener, numbytes, &src_addr);
break;
case AGENT_MSG_TYPE_ACK:
agent_thread_handle_ack(listener, numbytes, &src_addr);
break;
default:
/* Ignore unknown pings */
inet_ntop(AF_INET, &src_addr_in->sin_addr, str, sizeof(str));
opal_output_verbose(20, USNIC_OUT,
"usNIC connectivity agent received unknown message: %d bytes from %s",
(int) numbytes, str);
break;
}
}
static agent_udp_port_listener_t *agent_thread_find_listener(uint32_t ipv4_addr, uint32_t *udp_port)
{
agent_udp_port_listener_t *listener;
OPAL_LIST_FOREACH (listener, &udp_port_listeners, agent_udp_port_listener_t) {
if (listener->ipv4_addr == ipv4_addr) {
*udp_port = listener->udp_port;
return listener;
}
}
return NULL;
}
/*
* Send reply back from the LISTEN command: send back the IP address
* and UDP port that we're listening on.
*/
static int agent_thread_cmd_listen_reply(int fd, uint32_t addr, int32_t udp_port)
{
int ret;
opal_btl_usnic_connectivity_cmd_listen_reply_t cmd = {.cmd = CONNECTIVITY_AGENT_CMD_LISTEN,
.ipv4_addr = addr,
.udp_port = udp_port};
ret = opal_fd_write(fd, sizeof(cmd), &cmd);
if (OPAL_SUCCESS != ret) {
OPAL_ERROR_LOG(ret);
ABORT("usnic connectivity agent IPC write failed");
/* Will not return */
}
return OPAL_SUCCESS;
}
/*
* Receive and process the rest of a LISTEN command from a local IPC
* client.
*/
static void agent_thread_cmd_listen(agent_ipc_listener_t *ipc_listener)
{
/* Read the rest of the LISTEN command from the IPC socket */
int ret;
opal_btl_usnic_connectivity_cmd_listen_t cmd;
ret = opal_fd_read(ipc_listener->client_fd, sizeof(cmd), &cmd);
if (OPAL_SUCCESS != ret) {
OPAL_ERROR_LOG(ret);
ABORT("usnic connectivity agent IPC LISTEN read failed");
/* Will not return */
}
/* If we're already listening on this address, send the UDP port
back to the client. */
uint32_t udp_port;
agent_udp_port_listener_t *udp_listener;
udp_listener = agent_thread_find_listener(cmd.ipv4_addr, &udp_port);
if (NULL != udp_listener) {
/* If we get a non-NULL "module" pointer value from the
client, it means that this client is the same process as
this agent, and we should save this pointer value (all
non-agent MPI procs will send NULL as their "module"
pointer value -- i.e., some non-agent MPI proc was the
first one to send the LISTEN command). */
if (NULL == udp_listener->module) {
udp_listener->module = cmd.module;
}
agent_thread_cmd_listen_reply(ipc_listener->client_fd, cmd.ipv4_addr, udp_port);
return;
}
/* We're not listening on this interface already, so create a
UDP port listener entry */
udp_listener = OBJ_NEW(agent_udp_port_listener_t);
if (NULL == udp_listener) {
OPAL_ERROR_LOG(OPAL_ERR_OUT_OF_RESOURCE);
ABORT("Out of memory");
/* Will not return */
}
udp_listener->module = cmd.module;
udp_listener->max_msg_size = cmd.max_msg_size;
udp_listener->ipv4_addr = cmd.ipv4_addr;
udp_listener->netmask = cmd.netmask;
udp_listener->usnic_name = strdup(cmd.usnic_name);
/* Fill in the ipv4_addr_str. Since we don't have the IPv4
address in sockaddr_in form, it's not worth using
inet_ntop() */
opal_btl_usnic_snprintf_ipv4_addr(udp_listener->ipv4_addr_str,
sizeof(udp_listener->ipv4_addr_str), cmd.ipv4_addr,
cmd.netmask);
udp_listener->buffer = malloc(udp_listener->max_msg_size);
if (NULL == udp_listener->buffer) {
OPAL_ERROR_LOG(OPAL_ERR_OUT_OF_RESOURCE);
ABORT("Out of memory");
/* Will not return */
}
/* Create the listening socket */
udp_listener->fd = socket(AF_INET, SOCK_DGRAM, 0);
if (udp_listener->fd < 0) {
OPAL_ERROR_LOG(udp_listener->fd);
ABORT("Could not open listening socket");
/* Will not return */
}
/* Bind it to the designated interface */
struct sockaddr_in inaddr;
memset(&inaddr, 0, sizeof(inaddr));
inaddr.sin_family = AF_INET;
inaddr.sin_addr.s_addr = cmd.ipv4_addr;
inaddr.sin_port = htons(0);
ret = bind(udp_listener->fd, (struct sockaddr *) &inaddr, sizeof(inaddr));
if (ret < 0) {
OPAL_ERROR_LOG(ret);
ABORT("Could not bind listening socket");
/* Will not return */
}
/* Find out the port we got */
opal_socklen_t addrlen = sizeof(struct sockaddr_in);
ret = getsockname(udp_listener->fd, (struct sockaddr *) &inaddr, &addrlen);
if (ret < 0) {
OPAL_ERROR_LOG(ret);
ABORT("Could not get UDP port number from listening socket");
/* Will not return */
}
udp_listener->udp_port = ntohs(inaddr.sin_port);
opal_output_verbose(20, USNIC_OUT, "usNIC connectivity agent listening on %s:%d, (%s)",
udp_listener->ipv4_addr_str, udp_listener->udp_port,
udp_listener->usnic_name);
/* Set the "don't fragment" bit on outgoing frames because we
want MTU-sized messages to get through successfully to the
peer, or fail if they have to fragment because of an MTU
mismatch somewhere enroute */
int val = IP_PMTUDISC_DO;
ret = setsockopt(udp_listener->fd, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val));
if (0 != ret) {
OPAL_ERROR_LOG(ret);
ABORT("Unable to set \"do not fragment\" on UDP socket");
/* Will not return */
}
/* Set the send and receive buffer sizes to our MTU size */
int temp;
temp = (int) udp_listener->max_msg_size;
if ((ret = setsockopt(udp_listener->fd, SOL_SOCKET, SO_RCVBUF, &temp, sizeof(temp))) < 0
|| (ret = setsockopt(udp_listener->fd, SOL_SOCKET, SO_SNDBUF, &temp, sizeof(temp))) < 0) {
OPAL_ERROR_LOG(ret);
ABORT("Could not set socket buffer sizes");
/* Will not return */
}
/* Create a listening event */
opal_event_set(mca_btl_usnic_component.opal_evbase, &udp_listener->event, udp_listener->fd,
OPAL_EV_READ | OPAL_EV_PERSIST, agent_thread_receive_ping, udp_listener);
opal_event_add(&udp_listener->event, 0);
/* Save this listener on the list of udp_port_listeners */
opal_list_append(&udp_port_listeners, &udp_listener->super);
udp_listener->active = true;
/* Return the port number to the sender */
ret = agent_thread_cmd_listen_reply(ipc_listener->client_fd, cmd.ipv4_addr,
udp_listener->udp_port);
/* All done! */
return;
}
/*
* Send a ping
*/
static void agent_thread_send_ping(int fd, short flags, void *context)
{
agent_ping_t *ap = (agent_ping_t *) context;
ap->timer_active = false;
char dest_ipv4_addr_str[IPV4STRADDRLEN];
opal_btl_usnic_snprintf_ipv4_addr(dest_ipv4_addr_str, sizeof(dest_ipv4_addr_str),
ap->dest_ipv4_addr, ap->dest_netmask);
/* If we got all the ACKs for this ping, then move this ping from
the "pending" list to the "results" list. We can also free the
buffers associated with this ping result, just to save some
space in the long run. */
if (ap->acked[0] && ap->acked[1]) {
opal_list_remove_item(&pings_pending, &ap->super);
opal_list_append(&ping_results, &ap->super);
opal_output_verbose(20, USNIC_OUT, "usNIC connectivity GOOD between %s <--> %s",
ap->listener->ipv4_addr_str, dest_ipv4_addr_str);
for (int i = 0; i < 2; ++i) {
if (NULL != ap->buffers[i]) {
free(ap->buffers[i]);
ap->buffers[i] = NULL;
}
}
return;
}
/* If we've resent too many times, then just abort */
if (ap->num_sends > mca_btl_usnic_component.connectivity_num_retries) {
char *topic;
if (ap->acked[0] && !ap->acked[1]) {
// For the show_help topic checker script
// SHOW_HELP:"help-mpi-btl-usnic.txt","connectivity error: small ok, large bad"
topic = "connectivity error: small ok, large bad";
} else if (!ap->acked[0] && ap->acked[1]) {
// For the show_help topic checker script
// SHOW_HELP:"help-mpi-btl-usnic.txt","connectivity error: small bad, large ok"
topic = "connectivity error: small bad, large ok";
} else {
// For the show_help topic checker script
// SHOW_HELP:"help-mpi-btl-usnic.txt","connectivity error: small bad, large bad"
topic = "connectivity error: small bad, large bad";
}
char ipv4_addr_str[IPV4STRADDRLEN];
opal_btl_usnic_snprintf_ipv4_addr(ipv4_addr_str, sizeof(ipv4_addr_str), ap->dest_ipv4_addr,
ap->dest_netmask);
opal_show_help("help-mpi-btl-usnic.txt", topic, true, opal_process_info.nodename,
ap->listener->ipv4_addr_str, ap->listener->usnic_name, ap->dest_nodename,
ipv4_addr_str, ap->sizes[0], ap->sizes[1]);
opal_btl_usnic_exit(NULL);
/* Will not return */
}
time_t t = time(NULL);
opal_output_verbose(20, USNIC_OUT, "usNIC connectivity pinging %s:%d (%s) from %s (%s) at %s",
dest_ipv4_addr_str, ntohs(ap->dest_sockaddr.sin_port), ap->dest_nodename,
ap->listener->ipv4_addr_str, ap->listener->usnic_name, ctime(&t));
/* Send the ping messages to the peer */
for (int i = 0; i < NUM_PING_SIZES; ++i) {
agent_sendto(ap->listener->fd, (char *) ap->buffers[i], ap->sizes[i],
(struct sockaddr *) &ap->dest_sockaddr);
}
/* Set a timer to check if these pings are ACKed */
opal_event_set(mca_btl_usnic_component.opal_evbase, &ap->timer, -1, 0, agent_thread_send_ping,
ap);
opal_event_add(&ap->timer, &ack_timeout);
ap->timer_active = true;
/* Count how many times we've done this */
++ap->num_sends;
}
/*
* Receive and process the rest of a PING command from a local IPC
* client.
*/
static void agent_thread_cmd_ping(agent_ipc_listener_t *ipc_listener)
{
/* Read the rest of the PING command from the IPC socket */
int ret;
opal_btl_usnic_connectivity_cmd_ping_t cmd;
ret = opal_fd_read(ipc_listener->client_fd, sizeof(cmd), &cmd);
if (OPAL_SUCCESS != ret) {
OPAL_ERROR_LOG(ret);
ABORT("usnic connectivity agent IPC PING read failed");
/* Will not return */
}
/* Have we already pinged this IP address / port? */
agent_ping_t *ap;
OPAL_LIST_FOREACH (ap, &ping_results, agent_ping_t) {
if (ap->dest_ipv4_addr == cmd.dest_ipv4_addr && ap->dest_udp_port == cmd.dest_udp_port) {
/* We already have results from pinging this IP address /
port, so there's no need for further action */
return;
}
}
/* Are we in the middle of pinging this IP address / port? */
OPAL_LIST_FOREACH (ap, &pings_pending, agent_ping_t) {
if (ap->dest_ipv4_addr == cmd.dest_ipv4_addr && ap->dest_udp_port == cmd.dest_udp_port) {
/* We're already in the middle of pinging this IP address
/ port, so there's no need for further action */
return;
}
}
/* This is a new ping request. Find the listener with this source
ipv4 address */
bool found = false;
agent_udp_port_listener_t *udp_listener;
OPAL_LIST_FOREACH (udp_listener, &udp_port_listeners, agent_udp_port_listener_t) {
if (udp_listener->ipv4_addr == cmd.src_ipv4_addr) {
found = true;
break;
}
}
if (!found) {
ABORT("Could not ping listener for ping request");
/* Will not return */
}
/* This is a new ping request; track it */
ap = OBJ_NEW(agent_ping_t);
if (NULL == ap) {
OPAL_ERROR_LOG(OPAL_ERR_OUT_OF_RESOURCE);
ABORT("Out of memory");
/* Will not return */
}
ap->src_ipv4_addr = cmd.src_ipv4_addr;
ap->src_udp_port = cmd.src_udp_port;
ap->listener = udp_listener;
ap->dest_ipv4_addr = cmd.dest_ipv4_addr;
ap->dest_netmask = cmd.dest_netmask;
ap->dest_udp_port = cmd.dest_udp_port;
ap->dest_sockaddr.sin_family = AF_INET;
ap->dest_sockaddr.sin_addr.s_addr = cmd.dest_ipv4_addr;
ap->dest_sockaddr.sin_port = htons(cmd.dest_udp_port);
ap->dest_nodename = strdup(cmd.dest_nodename);
/* The first message we send will be "short" (a simple control
message); the second will be "long" (i.e., caller-specified
length) */
ap->sizes[0] = sizeof(agent_udp_message_t);
/* Note that the MTU is the max Ethernet frame payload. So from
that MTU, we have to subtract off the max IP header (e.g., if
all IP options are enabled, which is 60 bytes), and then also
subtract off the UDP header (which is 8 bytes). So we need to
subtract off 68 bytes from the MTU, and that's the largest ping
payload we can send. */
ap->sizes[1] = cmd.max_msg_size - 68;
/* Allocate a buffer for each size. Make sure the smallest size
is at least sizeof(agent_udp_message_t). */
agent_udp_message_t *msg;
for (size_t i = 0; i < NUM_PING_SIZES; ++i) {
ap->buffers[i] = calloc(1, ap->sizes[i]);
if (NULL == ap->buffers[i]) {
OPAL_ERROR_LOG(OPAL_ERR_OUT_OF_RESOURCE);
ABORT("Out of memory");
/* Will not return */
}
/* Fill in the message with return addressing information */
msg = (agent_udp_message_t *) ap->buffers[i];
msg->message_type = AGENT_MSG_TYPE_PING;
msg->src_ipv4_addr = ap->src_ipv4_addr;
msg->src_udp_port = ap->src_udp_port;
msg->magic_number = MAGIC_ORIGINATOR;
msg->major_version = OPAL_MAJOR_VERSION;
msg->minor_version = OPAL_MINOR_VERSION;
msg->size = ap->sizes[i];
}
/* Save this ping request on the "pending" list */
opal_list_append(&pings_pending, &ap->super);
/* Send the ping */
agent_thread_send_ping(0, 0, ap);
}
/*
* Receive and process the rest of an UNLISTEN command from a local IPC
* client.
*/
static void agent_thread_cmd_unlisten(agent_ipc_listener_t *ipc_listener)
{
/* Read the rest of the UNLISTEN command from the IPC socket */
int ret;
opal_btl_usnic_connectivity_cmd_unlisten_t cmd;
ret = opal_fd_read(ipc_listener->client_fd, sizeof(cmd), &cmd);
if (OPAL_SUCCESS != ret) {
OPAL_ERROR_LOG(ret);
ABORT("usnic connectivity agent IPC UNLISTEN read failed");
/* Will not return */
}
/* If we are listening on this address (and we should be), then
stop listening on it. */
uint32_t udp_port;
agent_udp_port_listener_t *udp_listener;
udp_listener = agent_thread_find_listener(cmd.ipv4_addr, &udp_port);
if (NULL != udp_listener) {
OBJ_RELEASE(udp_listener);
}
/* All done! */
return;
}
/*
* Called when we get an incoming IPC message
*/
static void agent_thread_ipc_receive(int fd, short flags, void *context)
{
int32_t command;
agent_ipc_listener_t *ipc_listener = (agent_ipc_listener_t *) context;
/* Read the command */
command = -1;
int ret = opal_fd_read(fd, sizeof(command), &command);
if (OPAL_ERR_TIMEOUT == ret) {
/* We get OPAL_ERR_TIMEOUT if the remote side hung up */
OBJ_RELEASE(ipc_listener);
return;
} else if (OPAL_SUCCESS != ret) {
OPAL_ERROR_LOG(ret);
ABORT("usnic connectivity agent IPC command read failed");
/* Will not return */
}
assert(CONNECTIVITY_AGENT_CMD_LISTEN == command || CONNECTIVITY_AGENT_CMD_PING == command
|| CONNECTIVITY_AGENT_CMD_UNLISTEN == command);
switch (command) {
case CONNECTIVITY_AGENT_CMD_LISTEN:
agent_thread_cmd_listen(ipc_listener);
break;
case CONNECTIVITY_AGENT_CMD_PING:
agent_thread_cmd_ping(ipc_listener);
break;
case CONNECTIVITY_AGENT_CMD_UNLISTEN:
agent_thread_cmd_unlisten(ipc_listener);
break;
default:
ABORT("Unexpected connectivity agent command");
break;
}
}
/*
* We got a new connection on the IPC named socket. Add it to the
* event base.
*/
static void agent_thread_accept(int fd, short flags, void *context)
{
struct sockaddr addr;
socklen_t len;
agent_ipc_listener_t *listener = NULL;
len = sizeof(addr);
int client_fd = accept(fd, &addr, &len);
if (client_fd < 0) {
OPAL_ERROR_LOG(OPAL_ERR_IN_ERRNO);
ABORT("accept() failed");
/* Will not return */
}
/* If we got a good client, verify that it sent the magic token */
int tlen = strlen(CONNECTIVITY_MAGIC_TOKEN);
char *msg = alloca(tlen + 1);
if (NULL == msg) {
OPAL_ERROR_LOG(OPAL_ERR_OUT_OF_RESOURCE);
ABORT("Out of memory");
/* Will not return */
}
if (OPAL_SUCCESS != opal_fd_read(client_fd, tlen, msg)) {
OPAL_ERROR_LOG(OPAL_ERR_IN_ERRNO);
ABORT("usnic connectivity agent IPC read failed");
/* Will not return */
}
if (0 != memcmp(msg, CONNECTIVITY_MAGIC_TOKEN, tlen)) {
opal_output_verbose(
20, USNIC_OUT,
"usNIC connectivity got bad IPC client (wrong magic token); disconnected");
close(client_fd);
return;
}
/* Remember how many accepts we have successfully completed */
++ipc_accepts;
/* Make a listener object for this peer */
listener = OBJ_NEW(agent_ipc_listener_t);
listener->client_fd = client_fd;
/* Write back the magic token to ACK that we got the peer's
magic token and all is kosher */
if (OPAL_SUCCESS != opal_fd_write(client_fd, tlen, CONNECTIVITY_MAGIC_TOKEN)) {
OPAL_ERROR_LOG(OPAL_ERR_OUT_OF_RESOURCE);
ABORT("usnic connectivity agent IPC read failed");
/* Will not return */
}
/* Add this IPC listener to the event base */
opal_event_set(mca_btl_usnic_component.opal_evbase, &listener->event, client_fd,
OPAL_EV_READ | OPAL_EV_PERSIST, agent_thread_ipc_receive, listener);
opal_event_add(&listener->event, 0);
/* Save this listener on the list of ipc_listeners */
opal_list_append(&ipc_listeners, &listener->super);
listener->active = true;
return;
}
/*
* Tear down all active events.
*
* This is done as an event callback in the agent threaf so that there
* is no race condition in the teardown. Specifically: the progress
* thread will only fire one event at a time. Therefore, this one
* event can "atomically" delete all the events and data structures
* and not have to worry about concurrent access from some event
* firing in the middle of the teardown process.
*/
static void agent_thread_finalize(int fd, short flags, void *context)
{
/* Free the event that triggered this call */
free(context);
/* Ensure that all the local IPC clients have connected to me (so
that we don't shut down before someone tries to connect to me),
or 10 seconds have passed (i.e., if 10 seconds pass and they
don't all connect to me, then something else is wrong, and we
should just give up). */
static bool first = true;
static time_t timestamp = 0;
if (first) {
timestamp = time(NULL);
first = false;
}
if (ipc_accepts < opal_process_info.num_local_peers && time(NULL) < timestamp + 10) {
opal_output_verbose(
20, USNIC_OUT,
"usNIC connectivity agent delaying shutdown until all clients connect...");
opal_event_t *ev = calloc(sizeof(*ev), 1);
struct timeval finalize_retry = {.tv_sec = 0, .tv_usec = 10000};
opal_event_set(mca_btl_usnic_component.opal_evbase, ev, -1, 0, agent_thread_finalize, ev);
opal_event_add(ev, &finalize_retry);
return;
}
if (ipc_accepts < opal_process_info.num_local_peers) {
opal_output_verbose(20, USNIC_OUT,
"usNIC connectivity agent: only %d of %d clients connected, but "
"timeout has expired -- exiting anyway",
ipc_accepts, opal_process_info.num_local_peers);
}
/* Remove the agent listening event from the opal async event
base */
opal_event_del(&ipc_event);
/* Shut down all active udp_port_listeners */
agent_udp_port_listener_t *udp_listener, *ulnext;
OPAL_LIST_FOREACH_SAFE (udp_listener, ulnext, &udp_port_listeners, agent_udp_port_listener_t) {
OBJ_RELEASE(udp_listener);
}
/* Destroy the pending pings and ping results */
agent_ping_t *request, *pnext;
OPAL_LIST_FOREACH_SAFE (request, pnext, &pings_pending, agent_ping_t) {
opal_list_remove_item(&pings_pending, &request->super);
OBJ_RELEASE(request);
}
OPAL_LIST_FOREACH_SAFE (request, pnext, &ping_results, agent_ping_t) {
opal_list_remove_item(&ping_results, &request->super);
OBJ_RELEASE(request);
}
/* Shut down all active ipc_listeners */
agent_ipc_listener_t *ipc_listener, *inext;
OPAL_LIST_FOREACH_SAFE (ipc_listener, inext, &ipc_listeners, agent_ipc_listener_t) {
OBJ_RELEASE(ipc_listener);
}
agent_initialized = false;
}
/**************************************************************************
* All of the following functions run in the main application thread
**************************************************************************/
/*
* Setup the agent and start its event loop running in a dedicated
* thread
*/
int opal_btl_usnic_connectivity_agent_init(void)
{
/* Only do this initialization if I am the agent (the agent is
local rank 0) */
if (opal_process_info.my_local_rank != 0) {
return OPAL_SUCCESS;
}
if (agent_initialized) {
return OPAL_SUCCESS;
}
/* Make a struct timeval for use with timer events. Note that the
MCA param is expressed in terms of *milli*seconds, but the
timeval timeout is expressed in terms of *micro*seconds. */
ack_timeout.tv_sec = mca_btl_usnic_component.connectivity_ack_timeout / 1000;
ack_timeout.tv_usec = 1000 * (mca_btl_usnic_component.connectivity_ack_timeout % 1000);
/* Create lists */
OBJ_CONSTRUCT(&udp_port_listeners, opal_list_t);
OBJ_CONSTRUCT(&ipc_listeners, opal_list_t);
OBJ_CONSTRUCT(&pings_pending, opal_list_t);
OBJ_CONSTRUCT(&ping_results, opal_list_t);
/********************************************************************
* Once all of the above is setup, create the unix domain socket
* and start the event loop.
********************************************************************/
/* Create the unix domain socket in the job session directory */
ipc_accept_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (ipc_accept_fd < 0) {
OPAL_ERROR_LOG(OPAL_ERR_IN_ERRNO);
ABORT("socket() failed");
/* Will not return */
}
opal_asprintf(&ipc_filename, "%s/%s", opal_process_info.job_session_dir,
CONNECTIVITY_SOCK_NAME);
if (NULL == ipc_filename) {
OPAL_ERROR_LOG(OPAL_ERR_IN_ERRNO);
ABORT("Out of memory");
/* Will not return */
}
unlink(ipc_filename);
struct sockaddr_un address;
assert(strlen(ipc_filename) < sizeof(address.sun_path));
memset(&address, 0, sizeof(struct sockaddr_un));
address.sun_family = AF_UNIX;
opal_string_copy(address.sun_path, ipc_filename, sizeof(address.sun_path));
if (bind(ipc_accept_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0) {
OPAL_ERROR_LOG(OPAL_ERR_IN_ERRNO);
ABORT("bind() failed");
/* Will not return */
}
/* Give an arbitrarily large backlog number so that connecting
clients will never be backlogged (note for Future Jeff: please
don't laugh at Past Jeff if 256 has become a trivially small
number of on-server procs in a single job). */
if (listen(ipc_accept_fd, 256) != 0) {
OPAL_ERROR_LOG(OPAL_ERR_IN_ERRNO);
ABORT("listen() failed");
/* Will not return */
}
/* Add the socket to the event base */
opal_event_set(mca_btl_usnic_component.opal_evbase, &ipc_event, ipc_accept_fd,
OPAL_EV_READ | OPAL_EV_PERSIST, agent_thread_accept, NULL);
opal_event_add(&ipc_event, 0);
opal_output_verbose(20, USNIC_OUT, "usNIC connectivity agent initialized");
agent_initialized = true;
return OPAL_SUCCESS;
}
/*
* Shut down the agent
*/
int opal_btl_usnic_connectivity_agent_finalize(void)
{
/* Only do this if I have the agent running */
if (!agent_initialized) {
return OPAL_SUCCESS;
}
/* Submit an event to the async thread and tell it to delete all
the usNIC events. See the rationale for doing this in the
comment in the agent_thread_finalize() function. */
opal_event_t *ev = calloc(sizeof(*ev), 1);
opal_event_set(mca_btl_usnic_component.opal_evbase, ev, -1, OPAL_EV_WRITE,
agent_thread_finalize, ev);
opal_event_active(ev, OPAL_EV_WRITE, 1);
/* Wait for the event to fire and complete */
while (agent_initialized) {
struct timespec tp = {.tv_sec = 0, .tv_nsec = 1000};
nanosleep(&tp, NULL);
}
/* Close the local IPC socket and remove the file */
if (ipc_accept_fd != -1) {
close(ipc_accept_fd);
ipc_accept_fd = -1;
}
if (NULL != ipc_filename) {
unlink(ipc_filename);
free(ipc_filename);
ipc_filename = NULL;
}
opal_output_verbose(20, USNIC_OUT, "usNIC connectivity client finalized");
return OPAL_SUCCESS;
}
|