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 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
|
/*
* Copyright (c) 2013-2015 Intel Corporation. All rights reserved.
* Copyright (c) 2014-2016, Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2015 Los Alamos Nat. Security, LLC. All rights reserved.
* Copyright (c) 2016 Cray Inc. All rights reserved.
*
* This software is available to you under the BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <config.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <assert.h>
#include <getopt.h>
#include <inttypes.h>
#include <netdb.h>
#include <poll.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <ofi_mem.h>
#include <rdma/fabric.h>
#include <rdma/fi_cm.h>
#include <rdma/fi_domain.h>
#include <rdma/fi_endpoint.h>
#include <rdma/fi_eq.h>
#include <rdma/fi_errno.h>
#include <rdma/fi_tagged.h>
#ifndef OFI_MR_BASIC_MAP
#define OFI_MR_BASIC_MAP (FI_MR_ALLOCATED | FI_MR_PROV_KEY | FI_MR_VIRT_ADDR)
#endif
#ifndef AI_NUMERICSERV
#define AI_NUMERICSERV 0
#endif
static const uint64_t TAG = 1234;
enum precision {
NANO = 1,
MICRO = 1000,
MILLI = 1000000,
};
enum {
PP_OPT_ACTIVE = 1 << 0,
PP_OPT_ITER = 1 << 1,
PP_OPT_SIZE = 1 << 2,
PP_OPT_VERIFY_DATA = 1 << 3,
};
struct pp_opts {
uint16_t src_port;
uint16_t dst_port;
char *dst_addr;
int iterations;
int transfer_size;
int sizes_enabled;
int options;
};
#define PP_SIZE_MAX_POWER_TWO 22
#define PP_MAX_DATA_MSG \
((1 << PP_SIZE_MAX_POWER_TWO) + (1 << (PP_SIZE_MAX_POWER_TWO - 1)))
#define PP_STR_LEN 32
#define PP_MAX_CTRL_MSG 64
#define PP_CTRL_BUF_LEN 64
#define PP_MR_KEY 0xC0DE
#define PP_MAX_ADDRLEN 1024
#define INTEG_SEED 7
#define PP_ENABLE_ALL (~0)
#define PP_DEFAULT_SIZE (1 << 0)
#define PP_MSG_CHECK_PORT_OK "port ok"
#define PP_MSG_LEN_PORT 5
#define PP_MSG_CHECK_CNT_OK "cnt ok"
#define PP_MSG_LEN_CNT 10
#define PP_MSG_SYNC_Q "q"
#define PP_MSG_SYNC_A "a"
#define PP_PRINTERR(call, retv) \
fprintf(stderr, "%s(): %s:%-4d, ret=%d (%s)\n", call, __FILE__, \
__LINE__, (int)retv, fi_strerror((int) -retv))
#define PP_ERR(fmt, ...) \
fprintf(stderr, "[%s] %s:%-4d: " fmt "\n", "error", __FILE__, \
__LINE__, ##__VA_ARGS__)
int pp_debug;
int pp_ipv6;
#define PP_DEBUG(fmt, ...) \
do { \
if (pp_debug) { \
fprintf(stderr, "[%s] %s:%-4d: " fmt, "debug", \
__FILE__, __LINE__, ##__VA_ARGS__); \
} \
} while (0)
#define PP_CLOSE_FID(fd) \
do { \
int ret; \
if ((fd)) { \
ret = fi_close(&(fd)->fid); \
if (ret) \
PP_ERR("fi_close (%d) fid %d", ret, \
(int)(fd)->fid.fclass); \
fd = NULL; \
} \
} while (0)
#ifndef MAX
#define MAX(a, b) \
({ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
_a > _b ? _a : _b; \
})
#endif
#ifndef MIN
#define MIN(a, b) \
({ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
_a < _b ? _a : _b; \
})
#endif
struct ct_pingpong {
struct fi_info *fi_pep, *fi, *hints;
struct fid_fabric *fabric;
struct fid_domain *domain;
struct fid_pep *pep;
struct fid_ep *ep;
struct fid_cq *txcq, *rxcq;
struct fid_mr *mr;
struct fid_av *av;
struct fid_eq *eq;
struct fid_mr no_mr;
void *tx_ctx_ptr, *rx_ctx_ptr;
struct fi_context tx_ctx[2], rx_ctx[2];
uint64_t remote_cq_data;
uint64_t tx_seq, rx_seq, tx_cq_cntr, rx_cq_cntr;
fi_addr_t local_fi_addr, remote_fi_addr;
void *buf, *tx_buf, *rx_buf;
size_t buf_size, tx_size, rx_size;
size_t rx_prefix_size, tx_prefix_size;
int timeout_sec;
uint64_t start, end;
struct fi_av_attr av_attr;
struct fi_eq_attr eq_attr;
struct fi_cq_attr cq_attr;
struct pp_opts opts;
long cnt_ack_msg;
SOCKET ctrl_connfd;
char ctrl_buf[PP_CTRL_BUF_LEN + 1];
void *local_name, *rem_name;
};
static const char integ_alphabet[] =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* Size does not include trailing new line */
static const int integ_alphabet_length =
(sizeof(integ_alphabet) / sizeof(*integ_alphabet)) - 1;
/*******************************************************************************
* Utils
******************************************************************************/
static uint64_t pp_gettime_us(void)
{
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_sec * 1000000 + now.tv_usec;
}
static long parse_ulong(char *str, long max)
{
long ret;
char *end;
errno = 0;
ret = strtol(str, &end, 10);
if (*end != '\0' || errno != 0) {
if (errno == 0)
ret = -EINVAL;
else
ret = -errno;
fprintf(stderr, "Error parsing \"%s\": %s\n", str,
strerror(-ret));
return ret;
}
if ((ret < 0) || (max > 0 && ret > max)) {
ret = -ERANGE;
fprintf(stderr, "Error parsing \"%s\": %s\n", str,
strerror(-ret));
return ret;
}
return ret;
}
static void pp_banner_fabric_info(struct ct_pingpong *ct)
{
PP_DEBUG("Running pingpong test with fi_info:\n%s\n",
fi_tostr(ct->fi, FI_TYPE_INFO));
}
static void pp_banner_options(struct ct_pingpong *ct)
{
char size_msg[50];
char iter_msg[50];
struct pp_opts opts = ct->opts;
if ((opts.dst_addr == NULL) || (opts.dst_addr[0] == '\0'))
opts.dst_addr = "None";
if (opts.sizes_enabled == PP_ENABLE_ALL)
snprintf(size_msg, 50, "%s", "All sizes");
else if (opts.options & PP_OPT_SIZE)
snprintf(size_msg, 50, "selected size = %d",
opts.transfer_size);
else
snprintf(size_msg, 50, "default size = %d",
opts.transfer_size);
if (opts.options & PP_OPT_ITER)
snprintf(iter_msg, 50, "selected iterations: %d",
opts.iterations);
else {
snprintf(iter_msg, 50, "default iterations: %d",
opts.iterations);
}
PP_DEBUG(" * PingPong options:\n");
PP_DEBUG(" - %-20s: [%" PRIu16 "]\n", "src_port", opts.src_port);
PP_DEBUG(" - %-20s: [%s]\n", "dst_addr", opts.dst_addr);
PP_DEBUG(" - %-20s: [%" PRIu16 "]\n", "dst_port", opts.dst_port);
PP_DEBUG(" - %-20s: %s\n", "sizes_enabled", size_msg);
PP_DEBUG(" - %-20s: %s\n", "iterations", iter_msg);
if (ct->hints->fabric_attr->prov_name)
PP_DEBUG(" - %-20s: %s\n", "provider",
ct->hints->fabric_attr->prov_name);
if (ct->hints->domain_attr->name)
PP_DEBUG(" - %-20s: %s\n", "domain",
ct->hints->domain_attr->name);
}
/*******************************************************************************
* Control Messaging
******************************************************************************/
static int pp_getaddrinfo(char *name, uint16_t port, struct addrinfo **results)
{
int ret;
const char *err_msg;
char port_s[6];
struct addrinfo hints = {
.ai_family = pp_ipv6 ? AF_INET6 : AF_INET,
.ai_socktype = SOCK_STREAM, /* TCP socket */
.ai_protocol = IPPROTO_TCP, /* Any protocol */
.ai_flags = AI_NUMERICSERV /* numeric port is used */
};
snprintf(port_s, 6, "%" PRIu16, port);
ret = getaddrinfo(name, port_s, &hints, results);
if (ret != 0) {
err_msg = (const char *) gai_strerror(ret);
PP_ERR("getaddrinfo : %s", err_msg);
ret = -EXIT_FAILURE;
goto out;
}
ret = EXIT_SUCCESS;
out:
return ret;
}
static void pp_print_addrinfo(struct addrinfo *ai, char *msg)
{
char s[80] = {0};
void *addr;
if (ai->ai_family == AF_INET6)
addr = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
else
addr = &((struct sockaddr_in *)ai->ai_addr)->sin_addr;
inet_ntop(ai->ai_family, addr, s, 80);
PP_DEBUG("%s %s\n", msg, s);
}
static int pp_ctrl_init_client(struct ct_pingpong *ct)
{
struct addrinfo *results;
struct addrinfo *rp;
int errno_save = 0;
int ret;
ret = pp_getaddrinfo(ct->opts.dst_addr, ct->opts.dst_port, &results);
if (ret)
return ret;
if (!results) {
PP_ERR("getaddrinfo returned NULL list");
return -EXIT_FAILURE;
}
for (rp = results; rp; rp = rp->ai_next) {
ct->ctrl_connfd = ofi_socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (ct->ctrl_connfd == INVALID_SOCKET) {
errno_save = ofi_sockerr();
continue;
}
if (ct->opts.src_port != 0) {
if (pp_ipv6) {
struct sockaddr_in6 in6_addr = {0};
in6_addr.sin6_family = AF_INET6;
in6_addr.sin6_port = htons(ct->opts.src_port);
in6_addr.sin6_addr = in6addr_any;
ret =
bind(ct->ctrl_connfd, (struct sockaddr *)&in6_addr,
sizeof(in6_addr));
} else {
struct sockaddr_in in_addr = {0};
in_addr.sin_family = AF_INET;
in_addr.sin_port = htons(ct->opts.src_port);
in_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ret =
bind(ct->ctrl_connfd, (struct sockaddr *)&in_addr,
sizeof(in_addr));
}
if (ret == -1) {
errno_save = ofi_sockerr();
ofi_close_socket(ct->ctrl_connfd);
continue;
}
}
pp_print_addrinfo(rp, "CLIENT: connecting to");
ret = connect(ct->ctrl_connfd, rp->ai_addr, (socklen_t) rp->ai_addrlen);
if (ret != -1)
break;
errno_save = ofi_sockerr();
ofi_close_socket(ct->ctrl_connfd);
}
if (!rp || ret == -1) {
ret = -errno_save;
ct->ctrl_connfd = -1;
PP_ERR("failed to connect: %s", strerror(errno_save));
} else {
PP_DEBUG("CLIENT: connected\n");
}
freeaddrinfo(results);
return ret;
}
static int pp_ctrl_init_server(struct ct_pingpong *ct)
{
int optval = 1;
SOCKET listenfd;
int ret;
listenfd = ofi_socket(pp_ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0);
if (listenfd == INVALID_SOCKET) {
ret = -ofi_sockerr();
PP_PRINTERR("socket", ret);
return ret;
}
ret = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
(const char *)&optval, sizeof(optval));
if (ret == -1) {
ret = -ofi_sockerr();
PP_PRINTERR("setsockopt(SO_REUSEADDR)", ret);
goto fail_close_socket;
}
if (pp_ipv6) {
struct sockaddr_in6 ctrl6_addr = {0};
ctrl6_addr.sin6_family = AF_INET6;
ctrl6_addr.sin6_port = htons(ct->opts.src_port);
ctrl6_addr.sin6_addr = in6addr_any;
ret = bind(listenfd, (struct sockaddr *)&ctrl6_addr,
sizeof(ctrl6_addr));
} else {
struct sockaddr_in ctrl_addr = {0};
ctrl_addr.sin_family = AF_INET;
ctrl_addr.sin_port = htons(ct->opts.src_port);
ctrl_addr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(listenfd, (struct sockaddr *)&ctrl_addr,
sizeof(ctrl_addr));
}
if (ret == -1) {
ret = -ofi_sockerr();
PP_PRINTERR("bind", ret);
goto fail_close_socket;
}
ret = listen(listenfd, 10);
if (ret == -1) {
ret = -ofi_sockerr();
PP_PRINTERR("listen", ret);
goto fail_close_socket;
}
PP_DEBUG("SERVER: waiting for connection\n");
ct->ctrl_connfd = accept(listenfd, NULL, NULL);
if (ct->ctrl_connfd == -1) {
ret = -ofi_sockerr();
PP_PRINTERR("accept", ret);
goto fail_close_socket;
}
ofi_close_socket(listenfd);
PP_DEBUG("SERVER: connected\n");
return ret;
fail_close_socket:
if (ct->ctrl_connfd != -1) {
ofi_close_socket(ct->ctrl_connfd);
ct->ctrl_connfd = -1;
}
if (listenfd != -1)
ofi_close_socket(listenfd);
return ret;
}
static int pp_ctrl_init(struct ct_pingpong *ct)
{
const uint32_t default_ctrl = 47592;
struct timeval tv = {
.tv_sec = 5
};
int ret;
PP_DEBUG("Initializing control messages\n");
if (ct->opts.dst_addr) {
if (ct->opts.dst_port == 0)
ct->opts.dst_port = default_ctrl;
ret = pp_ctrl_init_client(ct);
} else {
if (ct->opts.src_port == 0)
ct->opts.src_port = default_ctrl;
ret = pp_ctrl_init_server(ct);
}
if (ret)
return ret;
ret = setsockopt(ct->ctrl_connfd, SOL_SOCKET, SO_RCVTIMEO,
(const char *)&tv, sizeof(struct timeval));
if (ret == -1) {
ret = -ofi_sockerr();
PP_PRINTERR("setsockopt(SO_RCVTIMEO)", ret);
return ret;
}
PP_DEBUG("Control messages initialized\n");
return ret;
}
static int pp_ctrl_send(struct ct_pingpong *ct, char *buf, size_t size)
{
int ret, err;
ret = ofi_send_socket(ct->ctrl_connfd, buf, size, 0);
if (ret < 0) {
err = -ofi_sockerr();
PP_PRINTERR("ctrl/send", err);
return err;
}
if (ret == 0) {
err = -ECONNABORTED;
PP_ERR("ctrl/read: no data or remote connection closed");
return err;
}
return ret;
}
static int pp_ctrl_recv(struct ct_pingpong *ct, char *buf, size_t size)
{
int ret, err;
do {
PP_DEBUG("receiving\n");
ret = ofi_read_socket(ct->ctrl_connfd, buf, size);
} while (ret == -1 && OFI_SOCK_TRY_SND_RCV_AGAIN(ofi_sockerr()));
if (ret < 0) {
err = -ofi_sockerr();
PP_PRINTERR("ctrl/read", err);
return err;
}
if (ret == 0) {
err = -ECONNABORTED;
PP_ERR("ctrl/read: no data or remote connection closed");
return err;
}
return ret;
}
static int pp_ctrl_recv_str(struct ct_pingpong *ct, char *buf, size_t size)
{
int ret;
ret = pp_ctrl_recv(ct, buf, size);
buf[size - 1] = '\0';
return ret;
}
static int pp_send_name(struct ct_pingpong *ct, struct fid *endpoint)
{
size_t addrlen = 0;
uint32_t len;
int ret;
PP_DEBUG("Fetching local address\n");
ct->local_name = NULL;
ret = fi_getname(endpoint, ct->local_name, &addrlen);
if ((ret != -FI_ETOOSMALL) || (addrlen <= 0)) {
PP_ERR("fi_getname didn't return length\n");
return -EMSGSIZE;
}
ct->local_name = calloc(1, addrlen);
if (!ct->local_name) {
PP_ERR("Failed to allocate memory for the address\n");
return -ENOMEM;
}
ret = fi_getname(endpoint, ct->local_name, &addrlen);
if (ret) {
PP_PRINTERR("fi_getname", ret);
goto fn;
}
PP_DEBUG("Sending name length\n");
len = htonl((uint32_t) addrlen);
ret = pp_ctrl_send(ct, (char *) &len, sizeof(len));
if (ret < 0)
goto fn;
PP_DEBUG("Sending address format\n");
if (ct->fi) {
ret = pp_ctrl_send(ct, (char *) &ct->fi->addr_format,
sizeof(ct->fi->addr_format));
} else {
ret = pp_ctrl_send(ct, (char *) &ct->fi_pep->addr_format,
sizeof(ct->fi_pep->addr_format));
}
if (ret < 0)
goto fn;
PP_DEBUG("Sending name\n");
ret = pp_ctrl_send(ct, ct->local_name, addrlen);
PP_DEBUG("Sent name\n");
fn:
return ret;
}
static int pp_recv_name(struct ct_pingpong *ct)
{
uint32_t len;
int ret;
PP_DEBUG("Receiving name length\n");
ret = pp_ctrl_recv(ct, (char *) &len, sizeof(len));
if (ret < 0)
return ret;
len = ntohl(len);
if (len > PP_MAX_ADDRLEN)
return -EINVAL;
ct->rem_name = calloc(1, len);
if (!ct->rem_name) {
PP_ERR("Failed to allocate memory for the address\n");
return -ENOMEM;
}
PP_DEBUG("Receiving address format\n");
ret = pp_ctrl_recv(ct, (char *) &ct->hints->addr_format,
sizeof(ct->hints->addr_format));
if (ret < 0)
return ret;
PP_DEBUG("Receiving name\n");
ret = pp_ctrl_recv(ct, ct->rem_name, len);
if (ret < 0)
return ret;
PP_DEBUG("Received name\n");
ct->hints->dest_addr = calloc(1, len);
if (!ct->hints->dest_addr) {
PP_DEBUG("Failed to allocate memory for destination address\n");
return -ENOMEM;
}
/* fi_freeinfo will free the dest_addr field. */
memcpy(ct->hints->dest_addr, ct->rem_name, len);
ct->hints->dest_addrlen = len;
return 0;
}
static int pp_ctrl_finish(struct ct_pingpong *ct)
{
if (ct->ctrl_connfd != -1) {
ofi_close_socket(ct->ctrl_connfd);
ct->ctrl_connfd = -1;
}
return 0;
}
static int pp_ctrl_sync(struct ct_pingpong *ct)
{
int ret;
PP_DEBUG("Syncing nodes\n");
if (ct->opts.dst_addr) {
snprintf(ct->ctrl_buf, sizeof(PP_MSG_SYNC_Q), "%s",
PP_MSG_SYNC_Q);
PP_DEBUG("CLIENT: syncing\n");
ret = pp_ctrl_send(ct, ct->ctrl_buf, sizeof(PP_MSG_SYNC_Q));
PP_DEBUG("CLIENT: after send / ret=%d\n", ret);
if (ret < 0)
return ret;
if (ret < sizeof(PP_MSG_SYNC_Q)) {
PP_ERR("CLIENT: bad length of sent data (len=%d/%zu)",
ret, sizeof(PP_MSG_SYNC_Q));
return -EBADMSG;
}
PP_DEBUG("CLIENT: syncing now\n");
ret = pp_ctrl_recv_str(ct, ct->ctrl_buf, sizeof(PP_MSG_SYNC_A));
PP_DEBUG("CLIENT: after recv / ret=%d\n", ret);
if (ret < 0)
return ret;
if (strcmp(ct->ctrl_buf, PP_MSG_SYNC_A)) {
PP_DEBUG("CLIENT: sync error while acking A: <%s> "
"(len=%zu)\n",
ct->ctrl_buf, strlen(ct->ctrl_buf));
return -EBADMSG;
}
PP_DEBUG("CLIENT: synced\n");
} else {
PP_DEBUG("SERVER: syncing\n");
ret = pp_ctrl_recv_str(ct, ct->ctrl_buf, sizeof(PP_MSG_SYNC_Q));
PP_DEBUG("SERVER: after recv / ret=%d\n", ret);
if (ret < 0)
return ret;
if (strcmp(ct->ctrl_buf, PP_MSG_SYNC_Q)) {
PP_DEBUG("SERVER: sync error while acking Q: <%s> "
"(len=%zu)\n",
ct->ctrl_buf, strlen(ct->ctrl_buf));
return -EBADMSG;
}
PP_DEBUG("SERVER: syncing now\n");
snprintf(ct->ctrl_buf, sizeof(PP_MSG_SYNC_A), "%s",
PP_MSG_SYNC_A);
ret = pp_ctrl_send(ct, ct->ctrl_buf, sizeof(PP_MSG_SYNC_A));
PP_DEBUG("SERVER: after send / ret=%d\n", ret);
if (ret < 0)
return ret;
if (ret < sizeof(PP_MSG_SYNC_A)) {
PP_ERR("SERVER: bad length of sent data (len=%d/%zu)",
ret, sizeof(PP_MSG_SYNC_A));
return -EBADMSG;
}
PP_DEBUG("SERVER: synced\n");
}
PP_DEBUG("Nodes synced\n");
return 0;
}
static int pp_ctrl_txrx_msg_count(struct ct_pingpong *ct)
{
int ret;
PP_DEBUG("Exchanging ack count\n");
if (ct->opts.dst_addr) {
memset(&ct->ctrl_buf, '\0', PP_MSG_LEN_CNT + 1);
snprintf(ct->ctrl_buf, PP_MSG_LEN_CNT + 1, "%ld",
ct->cnt_ack_msg);
PP_DEBUG("CLIENT: sending count = <%s> (len=%zu)\n",
ct->ctrl_buf, strlen(ct->ctrl_buf));
ret = pp_ctrl_send(ct, ct->ctrl_buf, PP_MSG_LEN_CNT);
if (ret < 0)
return ret;
if (ret < PP_MSG_LEN_CNT) {
PP_ERR("CLIENT: bad length of sent data (len=%d/%d)",
ret, PP_MSG_LEN_CNT);
return -EBADMSG;
}
PP_DEBUG("CLIENT: sent count\n");
ret = pp_ctrl_recv_str(ct, ct->ctrl_buf,
sizeof(PP_MSG_CHECK_CNT_OK));
if (ret < 0)
return ret;
if (ret < sizeof(PP_MSG_CHECK_CNT_OK)) {
PP_ERR(
"CLIENT: bad length of received data (len=%d/%zu)",
ret, sizeof(PP_MSG_CHECK_CNT_OK));
return -EBADMSG;
}
if (strcmp(ct->ctrl_buf, PP_MSG_CHECK_CNT_OK)) {
PP_DEBUG("CLIENT: error while server acking the count: "
"<%s> (len=%zu)\n",
ct->ctrl_buf, strlen(ct->ctrl_buf));
return ret;
}
PP_DEBUG("CLIENT: count acked by server\n");
} else {
memset(&ct->ctrl_buf, '\0', PP_MSG_LEN_CNT + 1);
PP_DEBUG("SERVER: receiving count\n");
ret = pp_ctrl_recv(ct, ct->ctrl_buf, PP_MSG_LEN_CNT);
if (ret < 0)
return ret;
if (ret < PP_MSG_LEN_CNT) {
PP_ERR(
"SERVER: bad length of received data (len=%d/%d)",
ret, PP_MSG_LEN_CNT);
return -EBADMSG;
}
ct->cnt_ack_msg = parse_ulong(ct->ctrl_buf, -1);
if (ct->cnt_ack_msg < 0)
return ret;
PP_DEBUG("SERVER: received count = <%ld> (len=%zu)\n",
ct->cnt_ack_msg, strlen(ct->ctrl_buf));
snprintf(ct->ctrl_buf, sizeof(PP_MSG_CHECK_CNT_OK), "%s",
PP_MSG_CHECK_CNT_OK);
ret =
pp_ctrl_send(ct, ct->ctrl_buf, sizeof(PP_MSG_CHECK_CNT_OK));
if (ret < 0)
return ret;
if (ret < sizeof(PP_MSG_CHECK_CNT_OK)) {
PP_ERR(
"CLIENT: bad length of received data (len=%d/%zu)",
ret, sizeof(PP_MSG_CHECK_CNT_OK));
return -EBADMSG;
}
PP_DEBUG("SERVER: acked count to client\n");
}
PP_DEBUG("Ack count exchanged\n");
return 0;
}
/*******************************************************************************
* Options
******************************************************************************/
static inline void pp_start(struct ct_pingpong *ct)
{
PP_DEBUG("Starting test chrono\n");
ct->opts.options |= PP_OPT_ACTIVE;
ct->start = pp_gettime_us();
}
static inline void pp_stop(struct ct_pingpong *ct)
{
ct->end = pp_gettime_us();
ct->opts.options &= ~PP_OPT_ACTIVE;
PP_DEBUG("Stopped test chrono\n");
}
static inline int pp_check_opts(struct ct_pingpong *ct, uint64_t flags)
{
return (ct->opts.options & flags) == flags;
}
/*******************************************************************************
* Data Verification
******************************************************************************/
static void pp_fill_buf(void *buf, int size)
{
char *msg_buf;
int msg_index;
static unsigned int iter;
int i;
msg_index = ((iter++) * INTEG_SEED) % integ_alphabet_length;
msg_buf = (char *)buf;
for (i = 0; i < size; i++) {
PP_DEBUG("index=%d msg_index=%d\n", i, msg_index);
msg_buf[i] = integ_alphabet[msg_index++];
if (msg_index >= integ_alphabet_length)
msg_index = 0;
}
}
static int pp_check_buf(void *buf, int size)
{
char *recv_data;
char c;
static unsigned int iter;
int msg_index;
int i;
PP_DEBUG("Verifying buffer content\n");
msg_index = ((iter++) * INTEG_SEED) % integ_alphabet_length;
recv_data = (char *)buf;
for (i = 0; i < size; i++) {
c = integ_alphabet[msg_index++];
if (msg_index >= integ_alphabet_length)
msg_index = 0;
if (c != recv_data[i]) {
PP_DEBUG("index=%d msg_index=%d expected=%d got=%d\n",
i, msg_index, c, recv_data[i]);
break;
}
}
if (i != size) {
PP_DEBUG("Finished veryfing buffer: content is corrupted\n");
printf("Error at iteration=%d size=%d byte=%d\n", iter, size,
i);
return 1;
}
PP_DEBUG("Buffer verified\n");
return 0;
}
/*******************************************************************************
* Error handling
******************************************************************************/
static void eq_readerr(struct fid_eq *eq)
{
struct fi_eq_err_entry eq_err = { 0 };
ssize_t rd;
rd = fi_eq_readerr(eq, &eq_err, 0);
if ((size_t) rd != sizeof(eq_err)) {
PP_PRINTERR("fi_eq_readerr", rd);
} else {
PP_ERR("eq_readerr: %s",
fi_eq_strerror(eq, eq_err.prov_errno, eq_err.err_data,
NULL, 0));
}
}
static void pp_process_eq_err(ssize_t rd, struct fid_eq *eq, const char *fn)
{
if (rd == -FI_EAVAIL)
eq_readerr(eq);
else
PP_PRINTERR(fn, rd);
}
/*******************************************************************************
* Test sizes
******************************************************************************/
static int generate_test_sizes(struct pp_opts *opts, size_t tx_size, int **sizes_)
{
int defaults[] = {64, 256, 1024, 4096, 65536, 1048576};
int power_of_two;
int half_up;
int n = 0;
int i;
int *sizes = NULL;
PP_DEBUG("Generating test sizes\n");
sizes = calloc(64, sizeof(*sizes));
if (sizes == NULL)
return 0;
*sizes_ = sizes;
if (opts->options & PP_OPT_SIZE) {
if (opts->transfer_size > tx_size)
return 0;
sizes[0] = opts->transfer_size;
n = 1;
} else if (opts->sizes_enabled != PP_ENABLE_ALL) {
for (i = 0; i < (sizeof(defaults) / sizeof(defaults[0])); i++) {
if (defaults[i] > tx_size)
break;
sizes[i] = defaults[i];
n++;
}
} else {
for (i = 0;; i++) {
power_of_two = (i == 0) ? 0 : (1 << i);
half_up =
(i == 0) ? 1 : power_of_two + (power_of_two / 2);
if (power_of_two > tx_size)
break;
sizes[i * 2] = power_of_two;
n++;
if (half_up > tx_size)
break;
sizes[(i * 2) + 1] = half_up;
n++;
}
}
PP_DEBUG("Generated %d test sizes\n", n);
return n;
}
/*******************************************************************************
* Performance output
******************************************************************************/
/* str must be an allocated buffer of PP_STR_LEN bytes */
static char *size_str(char *str, uint64_t size)
{
uint64_t base, fraction = 0;
char mag;
memset(str, '\0', PP_STR_LEN);
if (size >= (1 << 30)) {
base = 1 << 30;
mag = 'g';
} else if (size >= (1 << 20)) {
base = 1 << 20;
mag = 'm';
} else if (size >= (1 << 10)) {
base = 1 << 10;
mag = 'k';
} else {
base = 1;
mag = '\0';
}
if (size / base < 10)
fraction = (size % base) * 10 / base;
if (fraction)
snprintf(str, PP_STR_LEN, "%" PRIu64 ".%" PRIu64 "%c",
size / base, fraction, mag);
else
snprintf(str, PP_STR_LEN, "%" PRIu64 "%c", size / base, mag);
return str;
}
/* str must be an allocated buffer of PP_STR_LEN bytes */
static char *cnt_str(char *str, size_t size, uint64_t cnt)
{
if (cnt >= 1000000000)
snprintf(str, size, "%" PRIu64 "b", cnt / 1000000000);
else if (cnt >= 1000000)
snprintf(str, size, "%" PRIu64 "m", cnt / 1000000);
else if (cnt >= 1000)
snprintf(str, size, "%" PRIu64 "k", cnt / 1000);
else
snprintf(str, size, "%" PRIu64, cnt);
return str;
}
static void show_perf(char *name, int tsize, int sent, int acked,
uint64_t start, uint64_t end, int xfers_per_iter)
{
static int header = 1;
char str[PP_STR_LEN];
int64_t elapsed = end - start;
uint64_t bytes = (uint64_t)sent * tsize * xfers_per_iter;
float usec_per_xfer;
if (sent == 0)
return;
if (name) {
if (header) {
printf("%-50s%-8s%-8s%-9s%-8s%8s %10s%13s%13s\n",
"name", "bytes", "#sent", "#ack", "total",
"time", "MB/sec", "usec/xfer", "Mxfers/sec");
header = 0;
}
printf("%-50s", name);
} else {
if (header) {
printf("%-8s%-8s%-9s%-8s%8s %10s%13s%13s\n", "bytes",
"#sent", "#ack", "total", "time", "MB/sec",
"usec/xfer", "Mxfers/sec");
header = 0;
}
}
printf("%-8s", size_str(str, tsize));
printf("%-8s", cnt_str(str, sizeof(str), sent));
if (sent == acked)
printf("=%-8s", cnt_str(str, sizeof(str), acked));
else if (sent < acked)
printf("-%-8s", cnt_str(str, sizeof(str), acked - sent));
else
printf("+%-8s", cnt_str(str, sizeof(str), sent - acked));
printf("%-8s", size_str(str, bytes));
usec_per_xfer = ((float)elapsed / sent / xfers_per_iter);
printf("%8.2fs%10.2f%11.2f%11.2f\n", elapsed / 1000000.0,
bytes / (1.0 * elapsed), usec_per_xfer, 1.0 / usec_per_xfer);
}
/*******************************************************************************
* Data Messaging
******************************************************************************/
static int pp_cq_readerr(struct fid_cq *cq)
{
struct fi_cq_err_entry cq_err = { 0 };
int ret;
ret = fi_cq_readerr(cq, &cq_err, 0);
if (ret < 0) {
PP_PRINTERR("fi_cq_readerr", ret);
} else {
PP_ERR("cq_readerr: %s",
fi_cq_strerror(cq, cq_err.prov_errno, cq_err.err_data,
NULL, 0));
ret = -cq_err.err;
}
return ret;
}
static int pp_get_cq_comp(struct fid_cq *cq, uint64_t *cur, uint64_t total,
int timeout_sec)
{
struct fi_cq_err_entry comp;
uint64_t a = 0, b = 0;
int ret = 0;
if (timeout_sec >= 0)
a = pp_gettime_us();
do {
ret = fi_cq_read(cq, &comp, 1);
if (ret > 0) {
if (timeout_sec >= 0)
a = pp_gettime_us();
(*cur)++;
} else if (ret < 0 && ret != -FI_EAGAIN) {
if (ret == -FI_EAVAIL) {
ret = pp_cq_readerr(cq);
(*cur)++;
} else {
PP_PRINTERR("pp_get_cq_comp", ret);
}
return ret;
} else if (timeout_sec >= 0) {
b = pp_gettime_us();
if ((b - a) / 1000000 > timeout_sec) {
fprintf(stderr, "%ds timeout expired\n",
timeout_sec);
return -FI_ENODATA;
}
}
} while (total - *cur > 0);
return 0;
}
static int pp_get_rx_comp(struct ct_pingpong *ct, uint64_t total)
{
int ret = FI_SUCCESS;
if (ct->rxcq) {
ret = pp_get_cq_comp(ct->rxcq, &(ct->rx_cq_cntr), total,
ct->timeout_sec);
} else {
PP_ERR(
"Trying to get a RX completion when no RX CQ was opened");
ret = -FI_EOTHER;
}
return ret;
}
static int pp_get_tx_comp(struct ct_pingpong *ct, uint64_t total)
{
int ret;
if (ct->txcq) {
ret = pp_get_cq_comp(ct->txcq, &(ct->tx_cq_cntr), total, -1);
} else {
PP_ERR(
"Trying to get a TX completion when no TX CQ was opened");
ret = -FI_EOTHER;
}
return ret;
}
#define PP_POST(post_fn, comp_fn, seq, op_str, ...) \
do { \
int timeout_sec_save; \
int ret, rc; \
\
while (1) { \
ret = (int) post_fn(__VA_ARGS__); \
if (!ret) \
break; \
\
if (ret != -FI_EAGAIN) { \
PP_PRINTERR(op_str, ret); \
return ret; \
} \
\
timeout_sec_save = ct->timeout_sec; \
ct->timeout_sec = 0; \
rc = comp_fn(ct, seq); \
ct->timeout_sec = timeout_sec_save; \
if (rc && rc != -FI_EAGAIN) { \
PP_ERR("Failed to get " op_str " completion"); \
return rc; \
} \
} \
seq++; \
} while (0)
static ssize_t pp_post_tx(struct ct_pingpong *ct, struct fid_ep *ep, size_t size,
void *ctx)
{
if (!(ct->fi->caps & FI_TAGGED))
PP_POST(fi_send, pp_get_tx_comp, ct->tx_seq, "transmit", ep,
ct->tx_buf, size, fi_mr_desc(ct->mr),
ct->remote_fi_addr, ctx);
else
PP_POST(fi_tsend, pp_get_tx_comp, ct->tx_seq, "t-transmit", ep,
ct->tx_buf, size, fi_mr_desc(ct->mr),
ct->remote_fi_addr, TAG, ctx);
return 0;
}
static ssize_t pp_tx(struct ct_pingpong *ct, struct fid_ep *ep, size_t size)
{
ssize_t ret;
if (pp_check_opts(ct, PP_OPT_VERIFY_DATA | PP_OPT_ACTIVE))
pp_fill_buf((char *)ct->tx_buf + ct->tx_prefix_size, (int)size);
ret = pp_post_tx(ct, ep, size + ct->tx_prefix_size, ct->tx_ctx_ptr);
if (ret)
return ret;
ret = pp_get_tx_comp(ct, ct->tx_seq);
return ret;
}
static ssize_t pp_post_inject(struct ct_pingpong *ct, struct fid_ep *ep,
size_t size)
{
if (!(ct->fi->caps & FI_TAGGED))
PP_POST(fi_inject, pp_get_tx_comp, ct->tx_seq, "inject", ep,
ct->tx_buf, size, ct->remote_fi_addr);
else
PP_POST(fi_tinject, pp_get_tx_comp, ct->tx_seq, "tinject", ep,
ct->tx_buf, size, ct->remote_fi_addr, TAG);
ct->tx_cq_cntr++;
return 0;
}
static ssize_t pp_inject(struct ct_pingpong *ct, struct fid_ep *ep, size_t size)
{
ssize_t ret;
if (pp_check_opts(ct, PP_OPT_VERIFY_DATA | PP_OPT_ACTIVE))
pp_fill_buf((char *)ct->tx_buf + ct->tx_prefix_size, (int)size);
ret = pp_post_inject(ct, ep, size + ct->tx_prefix_size);
if (ret)
return ret;
return ret;
}
static ssize_t pp_post_rx(struct ct_pingpong *ct, struct fid_ep *ep,
size_t size, void *ctx)
{
if (!(ct->fi->caps & FI_TAGGED))
PP_POST(fi_recv, pp_get_rx_comp, ct->rx_seq, "receive", ep,
ct->rx_buf, size, fi_mr_desc(ct->mr), 0, ctx);
else
PP_POST(fi_trecv, pp_get_rx_comp, ct->rx_seq, "t-receive", ep,
ct->rx_buf, size, fi_mr_desc(ct->mr), 0, TAG, 0, ctx);
return 0;
}
static ssize_t pp_rx(struct ct_pingpong *ct, struct fid_ep *ep, size_t size)
{
ssize_t ret;
ret = pp_get_rx_comp(ct, ct->rx_seq);
if (ret)
return ret;
if (pp_check_opts(ct, PP_OPT_VERIFY_DATA | PP_OPT_ACTIVE)) {
ret = pp_check_buf((char *)ct->rx_buf + ct->rx_prefix_size,
(int)size);
if (ret)
return ret;
}
/* TODO: verify CQ data, if available */
/* Ignore the size arg. Post a buffer large enough to handle all message
* sizes. pp_sync() makes use of pp_rx() and gets called in tests just
* before message size is updated. The recvs posted are always for the
* next incoming message.
*/
ret = pp_post_rx(ct, ct->ep, MAX(ct->rx_size , PP_MAX_CTRL_MSG) +
ct->rx_prefix_size, ct->rx_ctx_ptr);
if (!ret)
ct->cnt_ack_msg++;
return ret;
}
/*******************************************************************************
* Initialization and allocations
******************************************************************************/
static void init_test(struct ct_pingpong *ct, struct pp_opts *opts)
{
char sstr[PP_STR_LEN];
size_str(sstr, opts->transfer_size);
ct->cnt_ack_msg = 0;
}
static uint64_t pp_init_cq_data(struct fi_info *info)
{
if (info->domain_attr->cq_data_size >= sizeof(uint64_t)) {
return 0x0123456789abcdefULL;
} else {
return 0x0123456789abcdefULL &
((0x1ULL << (info->domain_attr->cq_data_size * 8)) - 1);
}
}
static int pp_alloc_msgs(struct ct_pingpong *ct)
{
int ret;
long alignment = 1;
ct->tx_size = ct->opts.options & PP_OPT_SIZE ? ct->opts.transfer_size
: PP_MAX_DATA_MSG;
if (ct->tx_size > ct->fi->ep_attr->max_msg_size)
ct->tx_size = ct->fi->ep_attr->max_msg_size;
ct->rx_size = ct->tx_size;
ct->buf_size = MAX(ct->tx_size, PP_MAX_CTRL_MSG) +
MAX(ct->rx_size, PP_MAX_CTRL_MSG) +
ct->tx_prefix_size + ct->rx_prefix_size;
alignment = ofi_get_page_size();
if (alignment < 0) {
PP_PRINTERR("ofi_get_page_size", alignment);
return alignment;
}
/* Extra alignment for the second part of the buffer */
ct->buf_size += alignment;
ret = ofi_memalign(&(ct->buf), (size_t)alignment, ct->buf_size);
if (ret) {
PP_PRINTERR("ofi_memalign", ret);
return ret;
}
memset(ct->buf, 0, ct->buf_size);
ct->rx_buf = ct->buf;
ct->tx_buf = (char *)ct->buf +
MAX(ct->rx_size, PP_MAX_CTRL_MSG) +
ct->tx_prefix_size;
ct->tx_buf = (void *)(((uintptr_t)ct->tx_buf + alignment - 1) &
~(alignment - 1));
ct->remote_cq_data = pp_init_cq_data(ct->fi);
if (ct->fi->domain_attr->mr_mode & FI_MR_LOCAL) {
ret = fi_mr_reg(ct->domain, ct->buf, ct->buf_size,
FI_SEND | FI_RECV, 0, PP_MR_KEY, 0, &(ct->mr),
NULL);
if (ret) {
PP_PRINTERR("fi_mr_reg", ret);
return ret;
}
} else {
ct->mr = &(ct->no_mr);
}
return 0;
}
static int pp_open_fabric_res(struct ct_pingpong *ct)
{
int ret;
PP_DEBUG("Opening fabric resources: fabric, eq & domain\n");
ret = fi_fabric(ct->fi->fabric_attr, &(ct->fabric), NULL);
if (ret) {
PP_PRINTERR("fi_fabric", ret);
return ret;
}
ret = fi_eq_open(ct->fabric, &(ct->eq_attr), &(ct->eq), NULL);
if (ret) {
PP_PRINTERR("fi_eq_open", ret);
return ret;
}
ret = fi_domain(ct->fabric, ct->fi, &(ct->domain), NULL);
if (ret) {
PP_PRINTERR("fi_domain", ret);
return ret;
}
PP_DEBUG("Fabric resources opened\n");
return 0;
}
static int pp_alloc_active_res(struct ct_pingpong *ct, struct fi_info *fi)
{
int ret;
if (fi->tx_attr->mode & FI_MSG_PREFIX)
ct->tx_prefix_size = fi->ep_attr->msg_prefix_size;
if (fi->rx_attr->mode & FI_MSG_PREFIX)
ct->rx_prefix_size = fi->ep_attr->msg_prefix_size;
ret = pp_alloc_msgs(ct);
if (ret)
return ret;
if (ct->cq_attr.format == FI_CQ_FORMAT_UNSPEC)
ct->cq_attr.format = FI_CQ_FORMAT_CONTEXT;
ct->cq_attr.wait_obj = FI_WAIT_NONE;
ct->cq_attr.size = fi->tx_attr->size;
ret = fi_cq_open(ct->domain, &(ct->cq_attr), &(ct->txcq), &(ct->txcq));
if (ret) {
PP_PRINTERR("fi_cq_open", ret);
return ret;
}
ct->cq_attr.size = fi->rx_attr->size;
ret = fi_cq_open(ct->domain, &(ct->cq_attr), &(ct->rxcq), &(ct->rxcq));
if (ret) {
PP_PRINTERR("fi_cq_open", ret);
return ret;
}
if (fi->ep_attr->type == FI_EP_RDM ||
fi->ep_attr->type == FI_EP_DGRAM) {
if (fi->domain_attr->av_type != FI_AV_UNSPEC)
ct->av_attr.type = fi->domain_attr->av_type;
ret = fi_av_open(ct->domain, &(ct->av_attr), &(ct->av), NULL);
if (ret) {
PP_PRINTERR("fi_av_open", ret);
return ret;
}
}
ret = fi_endpoint(ct->domain, fi, &(ct->ep), NULL);
if (ret) {
PP_PRINTERR("fi_endpoint", ret);
return ret;
}
return 0;
}
static int pp_getinfo(struct ct_pingpong *ct, struct fi_info *hints,
struct fi_info **info)
{
uint64_t flags = 0;
int ret;
if (!hints->ep_attr->type)
hints->ep_attr->type = FI_EP_DGRAM;
ret = fi_getinfo(FI_VERSION(FI_MAJOR_VERSION, FI_MINOR_VERSION),
NULL, NULL, flags, hints, info);
if (ret) {
PP_PRINTERR("fi_getinfo", ret);
return ret;
}
if (((*info)->tx_attr->mode & FI_CONTEXT2) != 0) {
ct->tx_ctx_ptr = &(ct->tx_ctx[0]);
} else if (((*info)->tx_attr->mode & FI_CONTEXT) != 0) {
ct->tx_ctx_ptr = &(ct->tx_ctx[1]);
} else if (((*info)->mode & FI_CONTEXT2) != 0) {
ct->tx_ctx_ptr = &(ct->tx_ctx[0]);
} else if (((*info)->mode & FI_CONTEXT) != 0) {
ct->tx_ctx_ptr = &(ct->tx_ctx[1]);
} else {
ct->tx_ctx_ptr = NULL;
}
if (((*info)->rx_attr->mode & FI_CONTEXT2) != 0) {
ct->rx_ctx_ptr = &(ct->rx_ctx[0]);
} else if (((*info)->rx_attr->mode & FI_CONTEXT) != 0) {
ct->rx_ctx_ptr = &(ct->rx_ctx[1]);
} else if (((*info)->mode & FI_CONTEXT2) != 0) {
ct->rx_ctx_ptr = &(ct->rx_ctx[0]);
} else if (((*info)->mode & FI_CONTEXT) != 0) {
ct->rx_ctx_ptr = &(ct->rx_ctx[1]);
} else {
ct->rx_ctx_ptr = NULL;
}
if ((hints->caps & FI_DIRECTED_RECV) == 0) {
(*info)->caps &= ~FI_DIRECTED_RECV;
(*info)->rx_attr->caps &= ~FI_DIRECTED_RECV;
}
return 0;
}
#define PP_EP_BIND(ep, fd, flags) \
do { \
int ret; \
if ((fd)) { \
ret = fi_ep_bind((ep), &(fd)->fid, (flags)); \
if (ret) { \
PP_PRINTERR("fi_ep_bind", ret); \
return ret; \
} \
} \
} while (0)
static int pp_init_ep(struct ct_pingpong *ct)
{
int ret;
PP_DEBUG("Initializing endpoint\n");
if (ct->fi->ep_attr->type == FI_EP_MSG)
PP_EP_BIND(ct->ep, ct->eq, 0);
PP_EP_BIND(ct->ep, ct->av, 0);
PP_EP_BIND(ct->ep, ct->txcq, FI_TRANSMIT);
PP_EP_BIND(ct->ep, ct->rxcq, FI_RECV);
ret = fi_enable(ct->ep);
if (ret) {
PP_PRINTERR("fi_enable", ret);
return ret;
}
ret = pp_post_rx(ct, ct->ep, MAX(ct->rx_size, PP_MAX_CTRL_MSG) +
ct->rx_prefix_size, ct->rx_ctx_ptr);
if (ret)
return ret;
PP_DEBUG("Endpoint initialized\n");
return 0;
}
static int pp_av_insert(struct fid_av *av, void *addr, size_t count,
fi_addr_t *fi_addr, uint64_t flags, void *context)
{
int ret;
PP_DEBUG("Connection-less endpoint: inserting new address in vector\n");
ret = fi_av_insert(av, addr, count, fi_addr, flags, context);
if (ret < 0) {
PP_PRINTERR("fi_av_insert", ret);
return ret;
} else if (ret != count) {
PP_ERR("fi_av_insert: number of addresses inserted = %d;"
" number of addresses given = %zd\n",
ret, count);
return -EXIT_FAILURE;
}
PP_DEBUG("Connection-less endpoint: new address inserted in vector\n");
return 0;
}
static int pp_exchange_names_connected(struct ct_pingpong *ct)
{
int ret;
PP_DEBUG("Connection-based endpoint: setting up connection\n");
if (ct->opts.dst_addr) {
ret = pp_recv_name(ct);
if (ret < 0)
return ret;
ret = pp_getinfo(ct, ct->hints, &(ct->fi));
if (ret)
return ret;
} else {
ret = pp_send_name(ct, &ct->pep->fid);
if (ret < 0)
return ret;
}
return 0;
}
static int pp_start_server(struct ct_pingpong *ct)
{
int ret;
PP_DEBUG("Connected endpoint: starting server\n");
ret = pp_getinfo(ct, ct->hints, &(ct->fi_pep));
if (ret)
return ret;
ret = fi_fabric(ct->fi_pep->fabric_attr, &(ct->fabric), NULL);
if (ret) {
PP_PRINTERR("fi_fabric", ret);
return ret;
}
ret = fi_eq_open(ct->fabric, &(ct->eq_attr), &(ct->eq), NULL);
if (ret) {
PP_PRINTERR("fi_eq_open", ret);
return ret;
}
ret = fi_passive_ep(ct->fabric, ct->fi_pep, &(ct->pep), NULL);
if (ret) {
PP_PRINTERR("fi_passive_ep", ret);
return ret;
}
ret = fi_pep_bind(ct->pep, &(ct->eq->fid), 0);
if (ret) {
PP_PRINTERR("fi_pep_bind", ret);
return ret;
}
ret = fi_listen(ct->pep);
if (ret) {
PP_PRINTERR("fi_listen", ret);
return ret;
}
PP_DEBUG("Connected endpoint: server started\n");
return 0;
}
static int pp_server_connect(struct ct_pingpong *ct)
{
struct fi_eq_cm_entry entry;
uint32_t event;
ssize_t rd;
int ret;
PP_DEBUG("Connected endpoint: connecting server\n");
ret = pp_exchange_names_connected(ct);
if (ret)
return ret;
/* Listen */
rd = fi_eq_sread(ct->eq, &event, &entry, sizeof(entry), -1, 0);
if (rd != sizeof(entry)) {
pp_process_eq_err(rd, ct->eq, "fi_eq_sread");
return (int) rd;
}
if (event != FI_CONNREQ) {
fprintf(stderr, "Unexpected CM event %d\n", event);
return -FI_EOTHER;
}
ct->fi = entry.info;
ret = fi_domain(ct->fabric, ct->fi, &(ct->domain), NULL);
if (ret) {
PP_PRINTERR("fi_domain", ret);
goto err;
}
ret = pp_alloc_active_res(ct, ct->fi);
if (ret)
goto err;
ret = pp_init_ep(ct);
if (ret)
goto err;
PP_DEBUG("accepting\n");
ret = fi_accept(ct->ep, NULL, 0);
if (ret) {
PP_PRINTERR("fi_accept", ret);
goto err;
}
/* Accept */
rd = fi_eq_sread(ct->eq, &event, &entry, sizeof(entry), -1, 0);
if (rd != sizeof(entry)) {
pp_process_eq_err(rd, ct->eq, "fi_eq_sread");
ret = (int)rd;
goto err;
}
if (event != FI_CONNECTED || entry.fid != &(ct->ep->fid)) {
fprintf(stderr, "Unexpected CM event %d fid %p (ep %p)\n",
event, entry.fid, ct->ep);
ret = -FI_EOTHER;
goto err;
}
PP_DEBUG("Connected endpoint: server connected\n");
return 0;
err:
fi_reject(ct->pep, ct->fi->handle, NULL, 0);
return ret;
}
static int pp_client_connect(struct ct_pingpong *ct)
{
struct fi_eq_cm_entry entry;
uint32_t event;
ssize_t rd;
int ret;
ret = pp_exchange_names_connected(ct);
if (ret)
return ret;
ret = pp_open_fabric_res(ct);
if (ret)
return ret;
ret = pp_alloc_active_res(ct, ct->fi);
if (ret)
return ret;
ret = pp_init_ep(ct);
if (ret)
return ret;
ret = fi_connect(ct->ep, ct->rem_name, NULL, 0);
if (ret) {
PP_PRINTERR("fi_connect", ret);
return ret;
}
/* Connect */
rd = fi_eq_sread(ct->eq, &event, &entry, sizeof(entry), -1, 0);
if (rd != sizeof(entry)) {
pp_process_eq_err(rd, ct->eq, "fi_eq_sread");
ret = (int)rd;
return ret;
}
if (event != FI_CONNECTED || entry.fid != &(ct->ep->fid)) {
fprintf(stderr, "Unexpected CM event %d fid %p (ep %p)\n",
event, entry.fid, ct->ep);
ret = -FI_EOTHER;
return ret;
}
return 0;
}
static int pp_init_fabric(struct ct_pingpong *ct)
{
int ret;
ret = pp_ctrl_init(ct);
if (ret)
return ret;
PP_DEBUG("Initializing fabric\n");
PP_DEBUG("Connection-less endpoint: initializing address vector\n");
if (ct->opts.dst_addr) {
ret = pp_recv_name(ct);
if (ret < 0)
return ret;
ret = pp_getinfo(ct, ct->hints, &(ct->fi));
if (ret)
return ret;
ret = pp_open_fabric_res(ct);
if (ret)
return ret;
ret = pp_alloc_active_res(ct, ct->fi);
if (ret)
return ret;
ret = pp_init_ep(ct);
if (ret)
return ret;
ret = pp_send_name(ct, &ct->ep->fid);
} else {
PP_DEBUG("SERVER: getinfo\n");
ret = pp_getinfo(ct, ct->hints, &(ct->fi));
if (ret)
return ret;
PP_DEBUG("SERVER: open fabric resources\n");
ret = pp_open_fabric_res(ct);
if (ret)
return ret;
PP_DEBUG("SERVER: allocate active resource\n");
ret = pp_alloc_active_res(ct, ct->fi);
if (ret)
return ret;
PP_DEBUG("SERVER: initialize endpoint\n");
ret = pp_init_ep(ct);
if (ret)
return ret;
ret = pp_send_name(ct, &ct->ep->fid);
if (ret < 0)
return ret;
ret = pp_recv_name(ct);
}
if (ret < 0)
return ret;
if (ct->opts.dst_addr) {
/* Set */
ret = pp_av_insert(ct->av, ct->rem_name, 1, &(ct->remote_fi_addr), 0,
NULL);
if (ret)
return ret;
if (ct->fi->domain_attr->caps & FI_LOCAL_COMM)
ret = pp_av_insert(ct->av, ct->local_name, 1,
&(ct->local_fi_addr), 0, NULL);
} else {
if (ct->fi->domain_attr->caps & FI_LOCAL_COMM) {
ret = pp_av_insert(ct->av, ct->local_name, 1,
&(ct->local_fi_addr), 0, NULL);
if (ret)
return ret;
}
ret = pp_av_insert(ct->av, ct->rem_name, 1, &(ct->remote_fi_addr), 0,
NULL);
}
if (ret)
return ret;
PP_DEBUG("Connection-less endpoint: address vector initialized\n");
PP_DEBUG("Fabric Initialized\n");
return 0;
}
/*******************************************************************************
* Deallocations and Final
******************************************************************************/
static void pp_free_res(struct ct_pingpong *ct)
{
PP_DEBUG("Freeing resources of test suite\n");
if (ct->mr != &(ct->no_mr))
PP_CLOSE_FID(ct->mr);
PP_CLOSE_FID(ct->ep);
PP_CLOSE_FID(ct->pep);
PP_CLOSE_FID(ct->rxcq);
PP_CLOSE_FID(ct->txcq);
PP_CLOSE_FID(ct->av);
PP_CLOSE_FID(ct->eq);
PP_CLOSE_FID(ct->domain);
PP_CLOSE_FID(ct->fabric);
free(ct->rem_name);
free(ct->local_name);
if (ct->buf) {
ofi_freealign(ct->buf);
ct->buf = ct->rx_buf = ct->tx_buf = NULL;
ct->buf_size = ct->rx_size = ct->tx_size = 0;
}
if (ct->fi_pep) {
fi_freeinfo(ct->fi_pep);
ct->fi_pep = NULL;
}
if (ct->fi) {
fi_freeinfo(ct->fi);
ct->fi = NULL;
}
if (ct->hints) {
fi_freeinfo(ct->hints);
ct->hints = NULL;
}
PP_DEBUG("Resources of test suite freed\n");
}
static int pp_finalize(struct ct_pingpong *ct)
{
struct iovec iov;
int ret;
struct fi_context ctx[2];
void *mem_desc[1] = { fi_mr_desc(ct->mr) };
const char *fin_buf = "fin";
const size_t fin_buf_size = strlen(fin_buf) + 1;
PP_DEBUG("Terminating test\n");
snprintf(ct->tx_buf, fin_buf_size, "%s", fin_buf);
iov.iov_base = ct->tx_buf;
iov.iov_len = fin_buf_size + ct->tx_prefix_size;
if (!(ct->fi->caps & FI_TAGGED)) {
struct fi_msg msg = {
.msg_iov = &iov,
.iov_count = 1,
.desc = mem_desc,
.addr = ct->remote_fi_addr,
.context = ctx,
};
ret = fi_sendmsg(ct->ep, &msg, FI_TRANSMIT_COMPLETE);
if (ret) {
PP_PRINTERR("transmit", ret);
return ret;
}
} else {
struct fi_msg_tagged tmsg = {
.msg_iov = &iov,
.iov_count = 1,
.desc = mem_desc,
.addr = ct->remote_fi_addr,
.context = ctx,
.tag = TAG,
};
ret = fi_tsendmsg(ct->ep, &tmsg, FI_TRANSMIT_COMPLETE);
if (ret) {
PP_PRINTERR("t-transmit", ret);
return ret;
}
}
ret = pp_get_tx_comp(ct, ++ct->tx_seq);
if (ret)
return ret;
ret = pp_get_rx_comp(ct, ct->rx_seq);
if (ret)
return ret;
if (pp_check_opts(ct, PP_OPT_VERIFY_DATA | PP_OPT_ACTIVE)) {
if (strncmp((char *)ct->rx_buf, fin_buf, fin_buf_size))
return 1;
PP_DEBUG("Buffer verified\n");
}
ret = pp_ctrl_finish(ct);
if (ret)
return ret;
PP_DEBUG("Test terminated\n");
return 0;
}
/*******************************************************************************
* CLI: Usage and Options parsing
******************************************************************************/
static void pp_pingpong_usage(struct ct_pingpong *ct, char *name, char *desc)
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s [OPTIONS]\t\tstart server\n", name);
fprintf(stderr, " %s [OPTIONS] <srv_addr>\tconnect to server\n", name);
if (desc)
fprintf(stderr, "\n%s\n", desc);
fprintf(stderr, "\nOptions:\n");
fprintf(stderr, " %-20s %s\n", "-B <src_port>",
"source control port number (server: 47592, client: auto)");
fprintf(stderr, " %-20s %s\n", "-P <dst_port>",
"destination control port number (client: 47592)");
fprintf(stderr, " %-20s %s\n", "-d <domain>", "domain name");
fprintf(stderr, " %-20s %s\n", "-p <provider>",
"specific provider name eg sockets, verbs");
fprintf(stderr, " %-20s %s\n", "-e <ep_type>",
"endpoint type: msg|rdm|dgram (dgram)");
fprintf(stderr, " %-20s %s (%d)\n", "-I <number>",
"number of iterations", ct->opts.iterations);
fprintf(stderr, " %-20s %s\n", "-S <size>",
"specific transfer size or 'all' (all)");
fprintf(stderr, " %-20s %s\n", "-c", "enables data_integrity checks");
fprintf(stderr, " %-20s %s\n", "-m <transmit mode>",
"transmit mode type: msg|tagged (msg)");
fprintf(stderr, " %-20s %s\n", "-h", "display this help output");
fprintf(stderr, " %-20s %s\n", "-v", "enable debugging output");
fprintf(stderr, " %-20s %s\n", "-6", "use IPv6 address");
}
static void pp_parse_opts(struct ct_pingpong *ct, int op, char *optarg)
{
switch (op) {
/* Domain */
case 'd':
ct->hints->domain_attr->name = strdup(optarg);
break;
/* Provider */
case 'p':
/* The provider name will be checked during the fabric
* initialization.
*/
ct->hints->fabric_attr->prov_name = strdup(optarg);
break;
/* Endpoint */
case 'e':
if (!strncasecmp("msg", optarg, 3) && (strlen(optarg) == 3)) {
ct->hints->ep_attr->type = FI_EP_MSG;
} else if (!strncasecmp("rdm", optarg, 3) &&
(strlen(optarg) == 3)) {
ct->hints->ep_attr->type = FI_EP_RDM;
} else if (!strncasecmp("dgram", optarg, 5) &&
(strlen(optarg) == 5)) {
ct->hints->ep_attr->type = FI_EP_DGRAM;
} else {
fprintf(stderr, "Unknown endpoint : %s\n", optarg);
exit(EXIT_FAILURE);
}
break;
/* Iterations */
case 'I':
ct->opts.options |= PP_OPT_ITER;
ct->opts.iterations = (int)parse_ulong(optarg, INT_MAX);
if (ct->opts.iterations < 0)
ct->opts.iterations = 0;
break;
/* Message Size */
case 'S':
if (!strncasecmp("all", optarg, 3) && (strlen(optarg) == 3)) {
ct->opts.sizes_enabled = PP_ENABLE_ALL;
} else {
ct->opts.options |= PP_OPT_SIZE;
ct->opts.transfer_size =
(int)parse_ulong(optarg, INT_MAX);
}
break;
/* Check data */
case 'c':
ct->opts.options |= PP_OPT_VERIFY_DATA;
break;
/* Source Port */
case 'B':
ct->opts.src_port = (uint16_t) parse_ulong(optarg, UINT16_MAX);
break;
/* Destination Port */
case 'P':
ct->opts.dst_port = (uint16_t) parse_ulong(optarg, UINT16_MAX);
break;
case 'm':
if (strncasecmp("msg", optarg, 4)) {
ct->hints->caps &= ~FI_MSG;
ct->hints->caps |= FI_TAGGED;
}
break;
/* Debug */
case 'v':
pp_debug = 1;
break;
/* IPV6 */
case '6':
pp_ipv6 = 1;
break;
default:
/* let getopt handle unknown opts*/
break;
}
}
/*******************************************************************************
* PingPong core and implemenations for endpoints
******************************************************************************/
static int pingpong(struct ct_pingpong *ct)
{
int ret, i;
ret = pp_ctrl_sync(ct);
if (ret)
return ret;
pp_start(ct);
if (ct->opts.dst_addr) {
for (i = 0; i < ct->opts.iterations; i++) {
if (ct->opts.transfer_size <
ct->fi->tx_attr->inject_size)
ret = pp_inject(ct, ct->ep,
ct->opts.transfer_size);
else
ret = pp_tx(ct, ct->ep, ct->opts.transfer_size);
if (ret)
return ret;
ret = pp_rx(ct, ct->ep, ct->opts.transfer_size);
if (ret)
return ret;
}
} else {
for (i = 0; i < ct->opts.iterations; i++) {
ret = pp_rx(ct, ct->ep, ct->opts.transfer_size);
if (ret)
return ret;
if (ct->opts.transfer_size <
ct->fi->tx_attr->inject_size)
ret = pp_inject(ct, ct->ep,
ct->opts.transfer_size);
else
ret = pp_tx(ct, ct->ep, ct->opts.transfer_size);
if (ret)
return ret;
}
}
pp_stop(ct);
ret = pp_ctrl_txrx_msg_count(ct);
if (ret)
return ret;
PP_DEBUG("Results:\n");
show_perf(NULL, ct->opts.transfer_size, ct->opts.iterations,
ct->cnt_ack_msg, ct->start, ct->end, 2);
return 0;
}
static int run_suite_pingpong(struct ct_pingpong *ct)
{
int i, sizes_cnt;
int ret = 0;
int *sizes = NULL;
pp_banner_fabric_info(ct);
sizes_cnt = generate_test_sizes(&ct->opts, ct->tx_size, &sizes);
PP_DEBUG("Count of sizes to test: %d\n", sizes_cnt);
for (i = 0; i < sizes_cnt; i++) {
ct->opts.transfer_size = sizes[i];
init_test(ct, &(ct->opts));
ret = pingpong(ct);
if (ret)
goto out;
}
out:
free(sizes);
return ret;
}
static int run_pingpong_dgram(struct ct_pingpong *ct)
{
int ret;
PP_DEBUG("Selected endpoint: DGRAM\n");
ret = pp_init_fabric(ct);
if (ret)
return ret;
/* Post an extra receive to avoid lacking a posted receive in the
* finalize.
*/
ret = fi_recv(ct->ep, ct->rx_buf,
MAX(ct->rx_size, PP_MAX_CTRL_MSG) + ct->rx_prefix_size,
fi_mr_desc(ct->mr), 0,
ct->rx_ctx_ptr);
if (ret)
return ret;
ret = run_suite_pingpong(ct);
if (ret)
return ret;
return pp_finalize(ct);
}
static int run_pingpong_rdm(struct ct_pingpong *ct)
{
int ret;
PP_DEBUG("Selected endpoint: RDM\n");
ret = pp_init_fabric(ct);
if (ret)
return ret;
ret = run_suite_pingpong(ct);
if (ret)
return ret;
return pp_finalize(ct);
}
static int run_pingpong_msg(struct ct_pingpong *ct)
{
int ret;
PP_DEBUG("Selected endpoint: MSG\n");
ret = pp_ctrl_init(ct);
if (ret)
return ret;
if (!ct->opts.dst_addr) {
ret = pp_start_server(ct);
if (ret)
return ret;
}
if (ct->opts.dst_addr) {
ret = pp_client_connect(ct);
PP_DEBUG("CLIENT: client_connect=%s\n", ret ? "KO" : "OK");
} else {
ret = pp_server_connect(ct);
PP_DEBUG("SERVER: server_connect=%s\n", ret ? "KO" : "OK");
}
if (ret)
return ret;
ret = run_suite_pingpong(ct);
if (ret)
goto out;
ret = pp_finalize(ct);
out:
fi_shutdown(ct->ep, 0);
return ret;
}
int main(int argc, char **argv)
{
int op, ret = EXIT_SUCCESS;
struct ct_pingpong ct = {
.timeout_sec = -1,
.ctrl_connfd = -1,
.opts = {
.iterations = 10,
.transfer_size = 64,
.sizes_enabled = PP_DEFAULT_SIZE
},
.eq_attr.wait_obj = FI_WAIT_UNSPEC,
};
ct.hints = fi_allocinfo();
if (!ct.hints)
return EXIT_FAILURE;
ct.hints->ep_attr->type = FI_EP_DGRAM;
ct.hints->caps = FI_MSG;
ct.hints->mode = FI_CONTEXT | FI_CONTEXT2 | FI_MSG_PREFIX;
ct.hints->domain_attr->mr_mode = FI_MR_LOCAL | OFI_MR_BASIC_MAP;
ofi_osd_init();
while ((op = getopt(argc, argv, "hvd:p:e:I:S:B:P:cm:6")) != -1) {
switch (op) {
default:
pp_parse_opts(&ct, op, optarg);
break;
case '?':
case 'h':
pp_pingpong_usage(&ct, argv[0],
"Ping pong client and server");
return EXIT_FAILURE;
}
}
if (optind < argc)
ct.opts.dst_addr = argv[optind];
pp_banner_options(&ct);
switch (ct.hints->ep_attr->type) {
case FI_EP_DGRAM:
if (ct.opts.options & PP_OPT_SIZE)
ct.hints->ep_attr->max_msg_size = ct.opts.transfer_size;
ret = run_pingpong_dgram(&ct);
break;
case FI_EP_RDM:
ret = run_pingpong_rdm(&ct);
break;
case FI_EP_MSG:
ret = run_pingpong_msg(&ct);
break;
default:
fprintf(stderr, "Endpoint unsupported: %d\n",
ct.hints->ep_attr->type);
ret = EXIT_FAILURE;
}
pp_free_res(&ct);
return -ret;
}
|