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
|
/*
* Copyright 2013-2021 Fabian Groffen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/resource.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "relay.h"
#include "router.h"
#include "server.h"
#include "collector.h"
#include "dispatcher.h"
#include "receptor.h"
#ifdef HAVE_GZIP
#include <zlib.h>
#endif
#ifdef HAVE_LZ4
#include <lz4.h>
#include <lz4frame.h>
#endif
#ifdef HAVE_SNAPPY
#include <snappy-c.h>
#endif
#ifdef HAVE_SSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif
#if defined(HAVE_DISPATCH_DISPATCH_H)
# include <dispatch/dispatch.h>
#elif defined(HAVE_SEMAPHORE_H)
# include <semaphore.h>
#else
# error "found no implementation for semaphores for your platform!"
#endif
enum conntype {
LISTENER,
CONNECTION
};
typedef struct _z_strm {
ssize_t (*strmread)(struct _z_strm *, void *, size_t); /* read func */
/* read from buffer only func, on error set errno to ENOMEM, EMSGSIZE or EBADMSG */
ssize_t (*strmreadbuf)(struct _z_strm *, void *, size_t, int, int);
int (*strmclose)(struct _z_strm *);
union {
#ifdef HAVE_GZIP
struct gz {
z_stream z;
int inflatemode;
} gz;
#endif
#ifdef HAVE_LZ4
struct lz4 {
LZ4F_decompressionContext_t lz;
size_t iloc; /* location for unprocessed input */
} lz4;
#endif
#ifdef HAVE_SSL
SSL *ssl;
#endif
int sock;
/* udp variant (in order to receive info about sender) */
struct udp_strm {
int sock;
struct sockaddr_in6 saddr;
char *srcaddr;
size_t srcaddrlen;
} udp;
} hdl;
#if defined(HAVE_GZIP) || defined(HAVE_LZ4) || defined(HAVE_SNAPPY)
char *ibuf;
size_t ipos;
size_t isize;
#endif
struct _z_strm *nextstrm;
} z_strm;
#define SOCKGROWSZ 32768
#define CONNGROWSZ 1024
#define MAX_LISTENERS 32 /* hopefully enough */
#define POLL_TIMEOUT 100
#define IDLE_DISCONNECT_TIME (10 * 60 * 1000 * 1000) /* 10 minutes */
/* connection takenby */
#define C_SETUP -2 /* being setup */
#define C_FREE -1 /* free */
#define C_IN 0 /* not taken */
/* > 0 taken by worker with id */
typedef struct _connection {
int sock;
z_strm *strm;
char takenby;
char srcaddr[24]; /* string representation of source address */
char buf[METRIC_BUFSIZ];
int buflen;
char needmore:1;
char noexpire:1;
char isaggr:1;
char isudp:1;
char datawaiting; /* full byte for atomic access */
char metric[METRIC_BUFSIZ];
destination dests[CONN_DESTS_SIZE];
size_t destlen;
struct timeval lastwork;
unsigned int maxsenddelay;
} connection;
struct _dispatcher {
pthread_t tid;
enum conntype type;
size_t metrics;
size_t blackholes;
size_t discards;
size_t ticks;
size_t sleeps;
size_t prevmetrics;
size_t prevblackholes;
size_t prevdiscards;
size_t prevticks;
size_t prevsleeps;
char id;
char keep_running; /* these all use a full byte for atomic access */
char route_refresh_pending;
char hold;
char tags_supported;
router *rtr;
router *pending_rtr;
char *allowed_chars;
int maxinplen;
int maxmetriclen;
};
static listener **listeners = NULL;
static connection *connections = NULL;
static size_t connectionslen = 0;
pthread_rwlock_t listenerslock = PTHREAD_RWLOCK_INITIALIZER;
pthread_rwlock_t connectionslock = PTHREAD_RWLOCK_INITIALIZER;
static size_t acceptedconnections = 0;
static size_t closedconnections = 0;
static unsigned int sockbufsize = 0;
/* connection specific readers and closers */
/* ordinary socket */
static inline ssize_t
sockread(z_strm *strm, void *buf, size_t sze)
{
return read(strm->hdl.sock, buf, sze);
}
static inline int
sockclose(z_strm *strm)
{
int ret = close(strm->hdl.sock);
free(strm);
return ret;
}
/* udp socket */
static inline ssize_t
udpsockread(z_strm *strm, void *buf, size_t sze)
{
ssize_t ret;
struct udp_strm *s = &strm->hdl.udp;
socklen_t slen = sizeof(s->saddr);
ret = recvfrom(s->sock, buf, sze, 0, (struct sockaddr *)&s->saddr, &slen);
if (ret <= 0)
return ret;
/* figure out who's calling */
s->srcaddr[0] = '\0';
switch (s->saddr.sin6_family) {
case PF_INET:
inet_ntop(s->saddr.sin6_family,
&((struct sockaddr_in *)&s->saddr)->sin_addr,
s->srcaddr, s->srcaddrlen);
break;
case PF_INET6:
inet_ntop(s->saddr.sin6_family, &s->saddr.sin6_addr,
s->srcaddr, s->srcaddrlen);
break;
}
return ret;
}
static inline int
udpsockclose(z_strm *strm)
{
int ret = close(strm->hdl.udp.sock);
free(strm);
return ret;
}
#ifdef HAVE_GZIP
/* gzip wrapped socket */
static inline ssize_t
gzipreadbuf(z_strm *strm, void *buf, size_t sze, int last_ret, int err);
static inline ssize_t
gzipread(z_strm *strm, void *buf, size_t sze)
{
z_stream *zstrm = &(strm->hdl.gz.z);
int ret;
if (zstrm->avail_in + 1 >= strm->isize) {
logerr("buffer overflow during read of gzip stream\n");
errno = EBADMSG;
return -1;
}
/* update ibuf */
if ((Bytef *)strm->ibuf != zstrm->next_in) {
memmove(strm->ibuf, zstrm->next_in, zstrm->avail_in);
zstrm->next_in = (Bytef *)strm->ibuf;
strm->ipos = zstrm->avail_in;
}
/* read any available data, if it fits */
ret = strm->nextstrm->strmread(strm->nextstrm,
strm->ibuf + strm->ipos,
strm->isize - strm->ipos);
if (ret > 0) {
zstrm->avail_in += ret;
strm->ipos += ret;
} else if (ret < 0) {
if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)
strm->hdl.gz.inflatemode = Z_FINISH;
} else {
/* ret == 0: EOF, which means we didn't read anything here, so
* calling inflate should be to flush whatever is in the zlib
* buffers, much like a read error.
* data in buffers may be exist, read with gzipreadbuf */
strm->hdl.gz.inflatemode = Z_FINISH;
return 0;
}
return gzipreadbuf(strm, buf, sze, ret, errno);
}
/* read data from buffer */
static inline ssize_t
gzipreadbuf(z_strm *strm, void *buf, size_t sze, int rval, int err)
{
z_stream *zstrm = &(strm->hdl.gz.z);
int iret;
zstrm->next_out = (Bytef *)buf;
zstrm->avail_out = (uInt)sze;
iret = inflate(zstrm, strm->hdl.gz.inflatemode);
switch (iret) {
case Z_OK: /* progress has been made */
/* calculate the "returned" bytes */
iret = sze - zstrm->avail_out;
break;
case Z_STREAM_END: /* everything uncompressed, nothing pending */
iret = sze - zstrm->avail_out;
break;
case Z_DATA_ERROR: /* corrupt input */
inflateSync(zstrm);
/* return isn't much of interest, we will call inflate next
* time and sync again if it still fails */
iret = -1;
break;
case Z_MEM_ERROR: /* out of memory */
logerr("out of memory during read of gzip stream\n");
errno = ENOMEM;
return -1;
break;
case Z_BUF_ERROR: /* output buffer full or nothing to read */
errno = EAGAIN;
break;
default:
iret = -1;
}
if (iret < 1) {
if (strm->ipos == strm->isize) {
logerr("buffer overflow during read of gzip stream\n");
errno = EBADMSG;
} else if (rval < 0)
errno = err ? err : EAGAIN;
}
return (ssize_t)iret;
}
static inline int
gzipclose(z_strm *strm)
{
int ret = strm->nextstrm->strmclose(strm->nextstrm);
inflateEnd(&(strm->hdl.gz.z));
free(strm->ibuf);
free(strm);
return ret;
}
#endif
#ifdef HAVE_LZ4
/* lz4 wrapped socket */
static inline ssize_t
lzreadbuf(z_strm *strm, void *buf, size_t sze, int rval, int err);
static inline ssize_t
lzreadbuf(z_strm *strm, void *buf, size_t sze, int rval, int err);
static inline ssize_t
lzread(z_strm *strm, void *buf, size_t sze)
{
int ret;
/* update ibuf */
if (strm->hdl.lz4.iloc > 0) {
memmove(strm->ibuf, strm->ibuf + strm->hdl.lz4.iloc, strm->ipos - strm->hdl.lz4.iloc);
strm->ipos -= strm->hdl.lz4.iloc;
strm->hdl.lz4.iloc = 0;
} else if (strm->ipos == strm->isize) {
logerr("buffer overflow during read of lz4 stream\n");
errno = EMSGSIZE;
return -1;
}
/* read any available data, if it fits */
ret = strm->nextstrm->strmread(strm->nextstrm,
strm->ibuf + strm->ipos, strm->isize - strm->ipos);
/* if EOF(0) or no-data(-1) then only get out now if the input
* buffer is empty because start of next frame may be waiting for us */
if (ret > 0) {
strm->ipos += ret;
} else if (ret < 0) {
if (strm->ipos == 0)
return -1;
} else {
/* ret == 0, a.k.a. EOF */
if (strm->ipos == 0)
return 0;
}
return lzreadbuf(strm, buf, sze, ret, errno);
}
static inline ssize_t
lzreadbuf(z_strm *strm, void *buf, size_t sze, int rval, int err)
{
/* attempt to decompress something from the (partial) frame that's
* arrived so far. srcsize is updated to the number of bytes
* consumed. likewise for destsize and bytes written */
size_t ret;
size_t srcsize;
size_t destsize;
srcsize = strm->ipos - strm->hdl.lz4.iloc;
if (srcsize == 0) /* input buffer decompressed */
return 0;
destsize = sze;
ret = LZ4F_decompress(strm->hdl.lz4.lz, buf, &destsize, strm->ibuf + strm->hdl.lz4.iloc, &srcsize, NULL);
/* check for error before doing anything else */
if (LZ4F_isError(ret)) {
/* need reset */
LZ4F_resetDecompressionContext(strm->hdl.lz4.lz);
/* liblz4 doesn't allow access to the error constants so have to
* return a generic code */
if (strm->hdl.lz4.iloc == 0 && strm->ipos == strm->isize) {
logerr("Error %s reading LZ4 compressed data, input buffer overflow\n", LZ4F_getErrorName(ret));
errno = EBADMSG;
} else if (rval == 0 ||
(rval == -1 && errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)) {
logerr("Error %s reading LZ4 compressed data, lost %lu bytes in input buffer\n",
LZ4F_getErrorName(ret), strm->ipos - strm->hdl.lz4.iloc);
errno = EBADMSG;
} else
errno = err ? err : EAGAIN;
return -1;
}
/* if we decompressed something, update our ibuf */
if (destsize > 0) {
strm->hdl.lz4.iloc += srcsize;
} else if (destsize == 0) {
tracef("No LZ4 data was produced\n");
errno = err ? err : EAGAIN;
return -1;
}
#ifdef ENABLE_TRACE
/* debug logging */
if (ret == 0)
tracef("LZ4 frame fully decoded\n");
#endif
return (ssize_t)destsize;
}
static inline int
lzclose(z_strm *strm)
{
int ret = strm->nextstrm->strmclose(strm->nextstrm);
LZ4F_freeDecompressionContext(strm->hdl.lz4.lz);
free(strm->ibuf);
free(strm);
return ret;
}
#endif
#ifdef HAVE_SNAPPY
/* snappy wrapped socket */
static inline ssize_t
snappyreadbuf(z_strm *strm, void *buf, size_t sze, int rval, int err);
static inline ssize_t
snappyreadbuf(z_strm *strm, void *buf, size_t sze, int rval, int err);
static inline ssize_t
snappyread(z_strm *strm, void *buf, size_t sze)
{
char *ibuf = strm->ibuf;
int ret;
size_t buflen = sze;
/* read any available data, if it fits */
ret = strm->nextstrm->strmread(strm->nextstrm,
ibuf + strm->ipos,
strm->isize - strm->ipos);
if (ret > 0) {
strm->ipos += ret;
} else if (ret < 0) {
return -1;
} else {
/* ret == 0, a.k.a. EOF */
return 0;
}
ret = snappy_uncompress(ibuf, strm->ipos, buf, &buflen);
/* if we decompressed something, update our ibuf */
if (ret == SNAPPY_OK) {
strm->ipos = strm->ipos - buflen;
memmove(ibuf, ibuf + buflen, strm->ipos);
} else if (ret == SNAPPY_BUFFER_TOO_SMALL) {
logerr("discarding snappy buffer: "
"the uncompressed block is too large\n");
strm->ipos = 0;
}
return (ssize_t)(ret == SNAPPY_OK ? buflen : -1);
}
static inline ssize_t
snappyreadbuf(z_strm *strm, void *buf, size_t sze, int rval, int err)
{
return 0;
}
static inline int
snappyclose(z_strm *strm)
{
int ret = strm->nextstrm->strmclose(strm->nextstrm);
free(strm->ibuf);
free(strm);
return ret;
}
#endif
#ifdef HAVE_SSL
/* (Open|Libre)SSL wrapped socket */
static inline ssize_t
sslread(z_strm *strm, void *buf, size_t sze)
{
return (ssize_t)SSL_read(strm->hdl.ssl, buf, (int)sze);
}
static inline int
sslclose(z_strm *strm)
{
int sock = SSL_get_fd(strm->hdl.ssl);
SSL_free(strm->hdl.ssl);
free(strm);
return close(sock);
}
#endif
/**
* Helper function to try and be helpful to the user. If errno
* indicates no new fds could be made, checks what the current max open
* files limit is, and if it's close to what we have in use now, write
* an informative message to stderr.
*/
void
dispatch_check_rlimit_and_warn(void)
{
if (errno == EISCONN || errno == EMFILE) {
struct rlimit ofiles;
/* rlimit can be changed for the running process (at least on
* Linux 2.6+) so refetch this value every time, should only
* occur on errors anyway */
if (getrlimit(RLIMIT_NOFILE, &ofiles) < 0)
ofiles.rlim_max = 0;
if (ofiles.rlim_max != RLIM_INFINITY && ofiles.rlim_max > 0)
logerr("process configured maximum connections = %d, "
"consider raising max open files/max descriptor limit\n",
(int)ofiles.rlim_max);
}
}
#define MAX_LISTENERS 32 /* hopefully enough */
/**
* Adds an (initial) listener socket to the chain of connections.
* Listener sockets are those which need to be accept()-ed on.
*/
int
dispatch_addlistener(listener *lsnr)
{
int c;
int *socks;
if (lsnr->ctype == CON_UDP) {
/* Adds a pseudo-listener for datagram (UDP) sockets, which is
* pseudo, for in fact it adds a new connection, but makes sure
* that connection won't be closed after being idle, and won't
* count that connection as an incoming connection either. */
for (socks = lsnr->socks; *socks != -1; socks++) {
c = dispatch_addconnection(*socks, lsnr);
if (c == -1)
return 1;
connections[c].noexpire = 1;
connections[c].isudp = 1;
acceptedconnections--;
}
return 0;
}
pthread_rwlock_wrlock(&listenerslock);
for (c = 0; c < MAX_LISTENERS; c++) {
if (listeners[c] == NULL) {
listeners[c] = lsnr;
for (socks = lsnr->socks; *socks != -1; socks++)
(void) fcntl(*socks, F_SETFL, O_NONBLOCK);
break;
}
}
if (c == MAX_LISTENERS) {
logerr("cannot add new listener: "
"no more free listener slots (max = %d)\n",
MAX_LISTENERS);
pthread_rwlock_unlock(&listenerslock);
return 1;
}
pthread_rwlock_unlock(&listenerslock);
return 0;
}
/**
* Remove listener from the listeners list. Each removal will incur a
* global lock. Frequent usage of this function is not anticipated.
*/
void
dispatch_removelistener(listener *lsnr)
{
int c;
int *socks;
if (lsnr->ctype != CON_UDP) {
pthread_rwlock_wrlock(&listenerslock);
/* find connection */
for (c = 0; c < MAX_LISTENERS; c++)
if (listeners[c] != NULL && listeners[c] == lsnr)
break;
if (c == MAX_LISTENERS) {
/* not found?!? */
logerr("dispatch: cannot find listener to remove!\n");
pthread_rwlock_unlock(&listenerslock);
return;
}
listeners[c] = NULL;
pthread_rwlock_unlock(&listenerslock);
}
/* acquire a write lock on connections, which is a bit wrong, but it
* ensures all dispatchers are stopped while we close the sockets,
* which avoids a race on the reading thereof if this is a UDP
* connection */
pthread_rwlock_wrlock(&connectionslock);
shutdownclose(lsnr);
pthread_rwlock_unlock(&connectionslock);
if (lsnr->saddrs) {
freeaddrinfo(lsnr->saddrs);
lsnr->saddrs = NULL;
}
}
/**
* Copy over all state related things from olsnr to nlsnr and ensure
* olsnr can be discarded (that is, thrown away without calling
* dispatch_removelistener).
*/
void
dispatch_transplantlistener(listener *olsnr, listener *nlsnr, router *r)
{
int c;
pthread_rwlock_wrlock(&listenerslock);
for (c = 0; c < MAX_LISTENERS; c++) {
if (listeners[c] == olsnr) {
router_transplant_listener_socks(r, olsnr, nlsnr);
#ifdef HAVE_SSL
if ((nlsnr->transport & ~0xFFFF) == W_SSL)
nlsnr->ctx = olsnr->ctx;
#endif
if (olsnr->saddrs) {
freeaddrinfo(olsnr->saddrs);
olsnr->saddrs = NULL;
}
listeners[c] = nlsnr;
break; /* found and done */
}
}
pthread_rwlock_unlock(&listenerslock);
}
#define CONNGROWSZ 1024
/**
* Adds a connection socket to the chain of connections.
* Connection sockets are those which need to be read from.
* Returns the connection id, or -1 if a failure occurred.
*/
int
dispatch_addconnection(int sock, listener *lsnr)
{
size_t c;
struct sockaddr_in6 saddr;
socklen_t saddr_len = sizeof(saddr);
#if defined(HAVE_GZIP) || defined(HAVE_LZ4) || defined(HAVE_SNAPPY)
int compress_type;
char *ibuf;
#endif
pthread_rwlock_rdlock(&connectionslock);
for (c = 0; c < connectionslen; c++)
if (__sync_bool_compare_and_swap(&(connections[c].takenby),
C_FREE, C_SETUP))
break;
pthread_rwlock_unlock(&connectionslock);
if (c == connectionslen) {
connection *newlst;
pthread_rwlock_wrlock(&connectionslock);
if (connectionslen > c) {
/* another dispatcher just extended the list */
pthread_rwlock_unlock(&connectionslock);
return dispatch_addconnection(sock, lsnr);
}
newlst = realloc(connections,
sizeof(connection) * (connectionslen + CONNGROWSZ));
if (newlst == NULL) {
logerr("cannot add new connection: "
"out of memory allocating more slots (max = %zu)\n",
connectionslen);
pthread_rwlock_unlock(&connectionslock);
return -1;
} else if (newlst != connections) {
/* reset srcaddr after realloc due to issue 346 */
for (c = 0; c < connectionslen; c++) {
if (newlst[c].isudp) {
newlst[c].strm->hdl.udp.srcaddr = newlst[c].srcaddr;
newlst[c].strm->hdl.udp.srcaddrlen =
sizeof(newlst[c].srcaddr);
}
}
}
for (c = connectionslen; c < connectionslen + CONNGROWSZ; c++) {
memset(&newlst[c], '\0', sizeof(connection));
newlst[c].takenby = C_FREE;
}
connections = newlst;
c = connectionslen; /* for the setup code below */
newlst[c].takenby = C_SETUP;
connectionslen += CONNGROWSZ;
pthread_rwlock_unlock(&connectionslock);
}
/* figure out who's calling */
if (getpeername(sock, (struct sockaddr *)&saddr, &saddr_len) == 0) {
snprintf(connections[c].srcaddr, sizeof(connections[c].srcaddr),
"(unknown)");
switch (saddr.sin6_family) {
case PF_INET:
inet_ntop(saddr.sin6_family,
&((struct sockaddr_in *)&saddr)->sin_addr,
connections[c].srcaddr, sizeof(connections[c].srcaddr));
break;
case PF_INET6:
inet_ntop(saddr.sin6_family, &saddr.sin6_addr,
connections[c].srcaddr, sizeof(connections[c].srcaddr));
break;
}
}
(void) fcntl(sock, F_SETFL, O_NONBLOCK);
if (sockbufsize > 0) {
if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
&sockbufsize, sizeof(sockbufsize)) != 0)
;
}
connections[c].sock = sock;
connections[c].strm = malloc(sizeof(z_strm));
if (connections[c].strm == NULL) {
logerr("cannot add new connection: "
"out of memory allocating stream\n");
__sync_bool_compare_and_swap(&(connections[c].takenby),
C_SETUP, C_FREE);
return -1;
}
/* set socket or SSL connection */
connections[c].strm->nextstrm = NULL;
connections[c].strm->strmreadbuf = NULL;
if (lsnr == NULL || (lsnr->transport & ~0xFFFF) != W_SSL) {
if (lsnr == NULL || lsnr->ctype != CON_UDP) {
connections[c].strm->hdl.sock = sock;
connections[c].strm->strmread = &sockread;
connections[c].strm->strmclose = &sockclose;
} else {
connections[c].strm->hdl.udp.sock = sock;
connections[c].strm->hdl.udp.srcaddr =
connections[c].srcaddr;
connections[c].strm->hdl.udp.srcaddrlen =
sizeof(connections[c].srcaddr);
connections[c].strm->strmread = &udpsockread;
connections[c].strm->strmclose = &udpsockclose;
}
#ifdef HAVE_SSL
} else {
if ((connections[c].strm->hdl.ssl = SSL_new(lsnr->ctx)) == NULL) {
logerr("cannot add new connection: %s\n",
ERR_reason_error_string(ERR_get_error()));
free(connections[c].strm);
__sync_bool_compare_and_swap(&(connections[c].takenby),
C_SETUP, C_FREE);
return -1;
}
SSL_set_fd(connections[c].strm->hdl.ssl, sock);
SSL_set_accept_state(connections[c].strm->hdl.ssl);
connections[c].strm->strmread = &sslread;
connections[c].strm->strmclose = &sslclose;
#endif
}
#if defined(HAVE_GZIP) || defined(HAVE_LZ4) || defined(HAVE_SNAPPY)
if (lsnr == NULL)
compress_type = 0;
else
compress_type = lsnr->transport & 0xFFFF;
/* allocate input buffer */
if (compress_type == W_GZIP || compress_type == W_LZ4 || compress_type == W_SNAPPY) {
ibuf = malloc(METRIC_BUFSIZ);
if (ibuf == NULL) {
logerr("cannot add new connection: "
"out of memory allocating stream ibuf\n");
free(connections[c].strm);
__sync_bool_compare_and_swap(&(connections[c].takenby),
C_SETUP, C_FREE);
return -1;
}
} else
ibuf = NULL;
#endif
/* setup decompressor */
if (lsnr == NULL) {
/* do nothing, catch case only */
}
#ifdef HAVE_GZIP
else if (compress_type == W_GZIP) {
z_strm *zstrm = malloc(sizeof(z_strm));
if (zstrm == NULL) {
logerr("cannot add new connection: "
"out of memory allocating gzip stream\n");
free(ibuf);
free(connections[c].strm);
__sync_bool_compare_and_swap(&(connections[c].takenby),
C_SETUP, C_FREE);
return -1;
}
zstrm->ipos = 0;
zstrm->ibuf = ibuf;
zstrm->isize = METRIC_BUFSIZ;
memset(&zstrm->hdl.gz.z, 0, sizeof(zstrm->hdl.gz.z));
zstrm->hdl.gz.z.next_in = (Bytef *)zstrm->ibuf;
zstrm->hdl.gz.z.avail_in = 0;
zstrm->hdl.gz.z.zalloc = Z_NULL;
zstrm->hdl.gz.z.zfree = Z_NULL;
zstrm->hdl.gz.z.opaque = Z_NULL;
if (inflateInit2(&zstrm->hdl.gz.z, 15 + 16) != Z_OK)
{
logerr("cannot init gzip connection\n");
free(ibuf);
free(connections[c].strm);
free(zstrm);
__sync_bool_compare_and_swap(&(connections[c].takenby),
C_SETUP, C_FREE);
return -1;
}
zstrm->strmread = &gzipread;
zstrm->strmreadbuf = &gzipreadbuf;
zstrm->strmclose = &gzipclose;
zstrm->hdl.gz.inflatemode = Z_SYNC_FLUSH;
zstrm->nextstrm = connections[c].strm;
connections[c].strm = zstrm;
}
#endif
#ifdef HAVE_LZ4
else if (compress_type == W_LZ4) {
z_strm *lzstrm = malloc(sizeof(z_strm));
if (lzstrm == NULL) {
logerr("cannot add new connection: "
"out of memory allocating lz4 stream\n");
free(ibuf);
free(connections[c].strm);
__sync_bool_compare_and_swap(&(connections[c].takenby),
C_SETUP, C_FREE);
return -1;
}
if (LZ4F_isError(LZ4F_createDecompressionContext(
&lzstrm->hdl.lz4.lz, LZ4F_VERSION)))
{
logerr("Failed to create LZ4 decompression context\n");
free(ibuf);
free(connections[c].strm);
free(lzstrm);
__sync_bool_compare_and_swap(&(connections[c].takenby),
C_SETUP, C_FREE);
return -1;
}
lzstrm->ibuf = ibuf;
lzstrm->isize = METRIC_BUFSIZ;
lzstrm->ipos = 0;
lzstrm->hdl.lz4.iloc = 0;
lzstrm->strmread = &lzread;
lzstrm->strmreadbuf = &lzreadbuf;
lzstrm->strmclose = &lzclose;
lzstrm->nextstrm = connections[c].strm;
connections[c].strm = lzstrm;
}
#endif
#ifdef HAVE_SNAPPY
else if (compress_type == W_SNAPPY) {
z_strm *lzstrm = malloc(sizeof(z_strm));
if (lzstrm == NULL) {
logerr("cannot add new connection: "
"out of memory allocating snappy stream\n");
__sync_bool_compare_and_swap(&(connections[c].takenby),
C_SETUP, C_FREE);
free(ibuf);
free(connections[c].strm);
__sync_bool_compare_and_swap(&(connections[c].takenby),
C_SETUP, C_FREE);
return -1;
}
lzstrm->ibuf = ibuf;
lzstrm->isize = METRIC_BUFSIZ;
lzstrm->ipos = 0;
lzstrm->strmread = &snappyread;
lzstrm->strmreadbuf = &snappyreadbuf;
lzstrm->strmclose = &snappyclose;
lzstrm->nextstrm = connections[c].strm;
connections[c].strm = lzstrm;
}
#endif
connections[c].buflen = 0;
connections[c].needmore = 0;
connections[c].noexpire = noexpire;
connections[c].isaggr = 0;
connections[c].isudp = 0;
connections[c].destlen = 0;
gettimeofday(&connections[c].lastwork, NULL);
connections[c].datawaiting = 0;
/* after this dispatchers will pick this connection up */
__sync_bool_compare_and_swap(&(connections[c].takenby), C_SETUP, C_IN);
__sync_add_and_fetch(&acceptedconnections, 1);
return c;
}
/**
* Adds a connection which we know is from an aggregator, so direct
* pipe. This is different from normal connections that we don't want
* to count them, never expire them, and want to recognise them when
* we're doing reloads.
*/
int
dispatch_addconnection_aggr(int sock)
{
int conn = dispatch_addconnection(sock, NULL);
if (conn == -1)
return 1;
connections[conn].noexpire = 1;
connections[conn].isaggr = 1;
acceptedconnections--;
return 0;
}
inline static char
dispatch_process_dests(connection *conn, dispatcher *self, struct timeval now)
{
int i;
char force;
if (conn->destlen > 0) {
if (conn->maxsenddelay == 0)
conn->maxsenddelay = ((rand() % 750) + 250) * 1000;
/* force when aggr (don't stall it) or after timeout */
force = conn->isaggr ? 1 :
timediff(conn->lastwork, now) > conn->maxsenddelay;
for (i = 0; i < conn->destlen; i++) {
tracef("dispatcher %d, connfd %d, metric %s, queueing to %s:%d\n",
self->id, conn->sock, conn->dests[i].metric,
server_ip(conn->dests[i].dest),
server_port(conn->dests[i].dest));
if (server_send(conn->dests[i].dest,
conn->dests[i].metric, force) == 0)
break;
}
if (i < conn->destlen) {
conn->destlen -= i;
memmove(&conn->dests[0], &conn->dests[i],
(sizeof(destination) * conn->destlen));
return 0;
} else {
/* finally "complete" this metric */
conn->destlen = 0;
conn->lastwork = now;
}
}
return 1;
}
/* Extract received metrics from buffer */
static void
dispatch_received_metrics(connection *conn, dispatcher *self)
{
/* Metrics look like this: metric_path value timestamp\n
* due to various messups we need to sanitise the
* metrics_path here, to ensure we can calculate the metric
* name off the filesystem path (and actually retrieve it in
* the web interface).
* Since tag support, metrics can look like this:
* metric_path[;tag=value ...] value timestamp\n
* where the tag=value part can be repeated. It should not be
* sanitised, however. */
char *p, *q, *firstspace, *lastnl;
char search_tags;
q = conn->metric;
firstspace = NULL;
lastnl = NULL;
search_tags = self->tags_supported;
for (p = conn->buf; p - conn->buf < conn->buflen; p++) {
if (*p == '\n' || *p == '\r') {
/* end of metric */
lastnl = p;
/* just a newline on it's own? some random garbage?
* do we exceed the set limits? drop */
if (q == conn->metric || firstspace == NULL ||
q - conn->metric > self->maxinplen - 1 ||
firstspace - conn->metric > self->maxmetriclen)
{
__sync_add_and_fetch(&(self->discards), 1);
q = conn->metric;
firstspace = NULL;
continue;
}
__sync_add_and_fetch(&(self->metrics), 1);
/* add newline and terminate the string, we can do this
* because we substract one from buf and we always store
* a full metric in buf before we copy it to metric
* (which is of the same size) */
*q++ = '\n';
*q = '\0';
/* perform routing of this metric */
tracef("dispatcher %d, connfd %d, metric %s",
self->id, conn->sock, conn->metric);
__sync_add_and_fetch(&(self->blackholes),
router_route(self->rtr,
conn->dests, &conn->destlen, CONN_DESTS_SIZE,
conn->srcaddr,
conn->metric, firstspace, self->id - 1));
tracef("dispatcher %d, connfd %d, destinations %zd\n",
self->id, conn->sock, conn->destlen);
/* restart building new one from the start */
q = conn->metric;
firstspace = NULL;
search_tags = self->tags_supported;
gettimeofday(&conn->lastwork, NULL);
conn->maxsenddelay = 0;
/* send the metric to where it is supposed to go */
if (dispatch_process_dests(conn, self, conn->lastwork) == 0)
break;
} else if (*p == ' ' || *p == '\t' || *p == '.') {
/* separator */
if (q == conn->metric) {
/* make sure we skip this on next iteration to
* avoid an infinite loop, issues #8 and #51 */
lastnl = p;
continue;
}
if (*p == '\t')
*p = ' ';
if (*p == ' ' && firstspace == NULL) {
if (*(q - 1) == '.')
q--; /* strip trailing separator */
firstspace = q;
*q++ = ' ';
} else {
/* metric_path separator or space,
* - duplicate elimination
* - don't start with separator/space */
if (*(q - 1) != *p && (q - 1) != firstspace)
*q++ = *p;
}
} else if (search_tags && *p == ';') {
/* copy up to next space */
search_tags = 0;
firstspace = q;
*q++ = *p;
} else if (
*p != '\0' && (
firstspace != NULL ||
(*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') ||
(*p >= '0' && *p <= '9') ||
strchr(self->allowed_chars, *p)
)
)
{
/* copy char */
*q++ = *p;
} else {
/* something barf, replace by underscore */
*q++ = '_';
}
}
conn->needmore = q != conn->metric;
if (lastnl != NULL) {
/* move remaining stuff to the front */
conn->buflen -= lastnl + 1 - conn->buf;
tracef("dispatcher %d, conn->buf: %p, lastnl: %p, diff: %zd, "
"conn->buflen: %d, sizeof(conn->buf): %lu, "
"memmove(%d, %lu, %d)\n",
self->id,
conn->buf, lastnl, lastnl - conn->buf,
conn->buflen, sizeof(conn->buf),
0, lastnl + 1 - conn->buf, conn->buflen + 1);
tracef("dispatcher %d, pre conn->buf: %s\n", self->id, conn->buf);
/* copy last NULL-byte for debug tracing */
memmove(conn->buf, lastnl + 1, conn->buflen + 1);
tracef("dispatcher %d, post conn->buf: %s\n", self->id, conn->buf);
}
}
#define IDLE_DISCONNECT_TIME (10 * 60 * 1000 * 1000) /* 10 minutes */
/**
* Look at conn and see if works needs to be done. If so, do it. This
* function operates on an (exclusive) lock on the connection it serves.
* Schematically, what this function does is like this:
*
* read (partial) data <----
* | |
* v |
* split and clean metrics |
* | |
* v |
* route metrics | feedback loop
* | | (stall client)
* v |
* send 1st attempt |
* \ |
* v* | * this is optional, but if a server's
* retry send (<1s) -------- queue is full, the client is stalled
* block reads
*/
static int
dispatch_connection(connection *conn, dispatcher *self, struct timeval start)
{
ssize_t len;
int err;
/* first try to resume any work being blocked */
if (dispatch_process_dests(conn, self, start) == 0) {
__sync_bool_compare_and_swap(&(conn->takenby), self->id, C_IN);
return 0;
}
/* don't poll (read) when the last time we ran nothing happened,
* this is to avoid excessive CPU usage, issue #126 */
if (__sync_bool_compare_and_swap(&(conn->datawaiting), 0, 0)) {
__sync_bool_compare_and_swap(&(conn->takenby), self->id, C_IN);
return 0;
}
len = -2;
/* try to read more data, if that succeeds, or we still have data
* left in the buffer, try to process the buffer */
if (
(!conn->needmore && conn->buflen > 0) ||
(len = conn->strm->strmread(conn->strm,
conn->buf + conn->buflen,
(sizeof(conn->buf) - 1) - conn->buflen)) > 0
)
{
ssize_t ilen;
if (len > 0) {
conn->buflen += len;
tracef("dispatcher %d, connfd %d, read %zd bytes from socket\n",
self->id, conn->sock, len);
#ifdef ENABLE_TRACE
conn->buf[conn->buflen] = '\0';
#endif
}
err = errno;
dispatch_received_metrics(conn, self);
if (conn->strm->strmreadbuf != NULL) {
while ((ilen = conn->strm->strmreadbuf(conn->strm,
conn->buf + conn->buflen,
(sizeof(conn->buf) - 1) - conn->buflen,
len, err)) > 0)
{
conn->buflen += ilen;
tracef("dispatcher %d, connfd %d, read %zd bytes from socket\n",
self->id, conn->sock, ilen);
#ifdef ENABLE_TRACE
conn->buf[conn->buflen] = '\0';
#endif
dispatch_received_metrics(conn, self);
}
if (ilen < 0 && errno == ENOMEM) /* input buffer overflow */
len = -1;
}
}
if (len == -1 && (errno == EINTR ||
errno == EAGAIN ||
errno == EWOULDBLOCK))
{
/* nothing available/no work done */
struct timeval stop;
gettimeofday(&stop, NULL);
if (!conn->noexpire &&
timediff(conn->lastwork, stop) > IDLE_DISCONNECT_TIME)
{
/* force close connection below */
len = 0;
} else {
__sync_bool_compare_and_swap(&(conn->takenby), self->id, C_IN);
return 0;
}
}
if (len == -1 || len == 0 || conn->isudp) { /* error + EOF */
/* we also disconnect the client in this case if our reading
* buffer is full, but we still need more (read returns 0 if the
* size argument is 0) -> this is good, because we can't do much
* with such client */
if (conn->isaggr || conn->isudp) {
/* reset buffer only (UDP/aggregations) and move on */
conn->needmore = 1;
conn->buflen = 0;
__sync_bool_compare_and_swap(&(conn->takenby), self->id, C_IN);
return len > 0;
} else if (conn->destlen == 0) {
tracef("dispatcher: %d, connfd: %d, len: %zd [%s], disconnecting\n",
self->id, conn->sock, len,
len < 0 ? strerror(errno) : "");
__sync_add_and_fetch(&closedconnections, 1);
conn->strm->strmclose(conn->strm);
/* flag this connection as no longer in use, unless there is
* pending metrics to send */
conn->sock = -1; /* ensure a poll won't match */
__sync_bool_compare_and_swap(&(conn->takenby), self->id, C_FREE);
return 0;
}
}
/* "release" this connection again */
__sync_bool_compare_and_swap(&(conn->takenby), self->id, C_IN);
return 1;
}
/**
* pthread compatible routine that handles connections and processes
* whatever comes in on those.
*/
#ifdef HAVE_DISPATCH_DISPATCH_H
static dispatch_semaphore_t *datawaiting = NULL;
static dispatch_semaphore_t _datawaiting;
#define sem_init(X,Y,Z) *(X) = dispatch_semaphore_create(Z)
#define sem_post(X) dispatch_semaphore_signal(*(X))
#define sema_wait(X,T) dispatch_semaphore_wait(*(X), \
dispatch_time(DISPATCH_TIME_NOW, T))
#else
static sem_t *datawaiting = NULL;
static sem_t _datawaiting;
static int sema_wait(sem_t *sem, long int timeout) {
struct timespec wait;
clock_gettime(CLOCK_REALTIME, &wait);
wait.tv_nsec += timeout;
if (wait.tv_nsec >= 1000000000) {
wait.tv_sec++;
wait.tv_nsec -= 1000000000;
}
return sem_timedwait(sem, &wait);
}
#endif
static void *
dispatch_runner(void *arg)
{
dispatcher *self = (dispatcher *)arg;
connection *conn;
int c;
if (self->type == LISTENER) {
struct pollfd *ufds = NULL;
int ufdslen = 0;
int fds;
int cfds;
int f;
int *sock;
while (__sync_bool_compare_and_swap(&(self->keep_running), 1, 1)) {
pthread_rwlock_rdlock(&connectionslock);
if (ufdslen < MAX_LISTENERS + connectionslen) {
ufdslen = MAX_LISTENERS + connectionslen;
ufds = realloc(ufds, sizeof(ufds[0]) * ufdslen);
if (ufds == NULL) {
logerr("dispatch: failed to allocate socket poll "
"space, cannot continue!\n");
pthread_rwlock_unlock(&connectionslock);
kill(getpid(), SIGTERM);
return NULL;
}
}
fds = 0;
for (c = 0; c < connectionslen; c++) {
conn = &(connections[c]);
if (!__sync_bool_compare_and_swap(&(conn->takenby), 0, 0))
continue;
/* connections are only read from if we flagged that
* there is data waiting, so sockets cannot disappear
* for the read code doesn't trigger unless we polled it */
if (__sync_bool_compare_and_swap(&(conn->datawaiting), 0, 0))
{
ufds[fds].fd = conn->sock;
ufds[fds].events = POLLIN;
fds++;
}
}
pthread_rwlock_unlock(&connectionslock);
cfds = fds;
pthread_rwlock_rdlock(&listenerslock);
for (c = 0; c < MAX_LISTENERS; c++) {
if (listeners[c] == NULL)
continue;
for (sock = listeners[c]->socks; *sock != -1; sock++) {
ufds[fds].fd = *sock;
ufds[fds].events = POLLIN;
fds++;
}
}
pthread_rwlock_unlock(&listenerslock);
if (poll(ufds, fds, 1000) <= 0)
continue;
for (f = 0; f < fds; f++) {
if (f < cfds) {
/* connection has data available */
if (ufds[f].revents & (POLLIN | POLLERR | POLLHUP)) {
pthread_rwlock_rdlock(&connectionslock);
for (c = 0; c < connectionslen; c++) {
conn = &(connections[c]);
/* connection may be serviced at this point,
* that's fine */
if ((char)__sync_add_and_fetch(&(conn->takenby), 0)
< 0)
continue;
if (conn->sock == ufds[f].fd) {
__sync_bool_compare_and_swap(
&(conn->datawaiting), 0, 1);
sem_post(datawaiting);
tracef("data waiting on connection %d, "
"src %s\n", conn->sock, conn->srcaddr);
}
}
pthread_rwlock_unlock(&connectionslock);
}
} else {
/* listener has new connection */
if (ufds[f].revents & POLLIN) {
int client;
struct sockaddr addr;
socklen_t addrlen = sizeof(addr);
/* check if this connection belongs to an
* existing listener, this is in particular of
* importance when a listener was removed,
* because thay may have interleaved here, and
* we might get a POLLIN event for a closed
* socket */
pthread_rwlock_rdlock(&listenerslock);
for (c = 0; c < MAX_LISTENERS; c++) {
if (listeners[c] == NULL)
continue;
sock = listeners[c]->socks;
for ( ; *sock != -1; sock++) {
if (ufds[f].fd == *sock)
break;
}
if (*sock != -1)
break;
}
pthread_rwlock_unlock(&listenerslock);
if (c == MAX_LISTENERS) {
/* be silent about this, because this
* happens in tests, and isn't ever possible
* in normal life */
tracef("dispatch: could not find listener for "
"socket, rejecting connection, fd=%d\n",
ufds[f].fd);
continue;
}
if ((client = accept(ufds[f].fd, &addr, &addrlen)) < 0)
{
logerr("dispatch: failed to "
"accept() new connection on %s:%d: %s\n",
listeners[c]->ip, listeners[c]->port,
strerror(errno));
dispatch_check_rlimit_and_warn();
continue;
}
if (dispatch_addconnection(client, listeners[c]) == -1)
{
close(client);
continue;
}
}
}
}
}
if (ufds != NULL)
free(ufds);
} else if (self->type == CONNECTION) {
int work;
struct timeval start;
struct timeval stop;
while (__sync_bool_compare_and_swap(&(self->keep_running), 1, 1)) {
work = 0;
if (__sync_bool_compare_and_swap(&(self->route_refresh_pending),
1, 1))
{
self->rtr = self->pending_rtr;
self->pending_rtr = NULL;
__sync_bool_compare_and_swap(&(self->route_refresh_pending),
1, 0);
__sync_and_and_fetch(&(self->hold), 0);
}
gettimeofday(&start, NULL);
pthread_rwlock_rdlock(&connectionslock);
for (c = 0; c < connectionslen; c++) {
conn = &(connections[c]);
/* atomically try to "claim" this connection */
if (!__sync_bool_compare_and_swap(
&(conn->takenby), C_IN, self->id))
continue;
if (__sync_bool_compare_and_swap(
&(self->hold), 1, 1) && !conn->isaggr)
{
__sync_bool_compare_and_swap(
&(conn->takenby), self->id, C_IN);
continue;
}
work += dispatch_connection(conn, self, start);
}
pthread_rwlock_unlock(&connectionslock);
gettimeofday(&stop, NULL);
__sync_add_and_fetch(&(self->ticks), timediff(start, stop));
/* nothing done, avoid spinlocking */
if (__sync_bool_compare_and_swap(&(self->keep_running), 1, 1) &&
work == 0)
{
gettimeofday(&start, NULL);
/* wait a bit, but immediately spurt into action if
* there's data available */
if (sema_wait(datawaiting, /* 700ms - 999ms */
(700 + (rand() % 300)) * 1000000) == 0)
tracef("dispatcher %d woken up\n", self->id);
gettimeofday(&stop, NULL);
__sync_add_and_fetch(&(self->sleeps), timediff(start, stop));
}
}
} else {
logerr("huh? unknown self type!\n");
}
return NULL;
}
/**
* Starts a new dispatcher for the given type and with the given id.
* Returns its handle.
*/
static dispatcher *
dispatch_new(
unsigned char id,
enum conntype type,
router *r,
char *allowed_chars,
int maxinplen,
int maxmetriclen
)
{
dispatcher *ret = malloc(sizeof(dispatcher));
if (ret == NULL)
return NULL;
if (type == CONNECTION && r == NULL) {
free(ret);
return NULL;
}
if (type == CONNECTION && datawaiting == NULL) {
datawaiting = &_datawaiting;
sem_init(datawaiting, 0, 0);
}
ret->id = id + 1; /* ensure > 0 */
ret->type = type;
ret->keep_running = 1;
ret->rtr = r;
ret->route_refresh_pending = 0;
ret->hold = 0;
ret->allowed_chars = allowed_chars;
ret->tags_supported = 0;
ret->maxinplen = maxinplen;
ret->maxmetriclen = maxmetriclen;
ret->metrics = 0;
ret->blackholes = 0;
ret->discards = 0;
ret->ticks = 0;
ret->sleeps = 0;
ret->prevmetrics = 0;
ret->prevblackholes = 0;
ret->prevticks = 0;
ret->prevsleeps = 0;
/* switch tag support on when the user didn't allow ';' as valid
* character in metrics */
if (allowed_chars != NULL && strchr(allowed_chars, ';') == NULL)
ret->tags_supported = 1;
if (pthread_create(&ret->tid, NULL, dispatch_runner, ret) != 0) {
free(ret);
return NULL;
}
return ret;
}
void
dispatch_set_bufsize(unsigned int nsockbufsize)
{
sockbufsize = nsockbufsize;
}
/**
* Initialise the listeners array. This is a one-time allocation that
* currently never is extended. This code does no locking, as it
* assumes to be run before any access to listeners occur.
*/
char
dispatch_init_listeners()
{
int i;
/* once all-or-nothing allocation */
if ((listeners = malloc(sizeof(listener *) * MAX_LISTENERS)) == NULL)
return 1;
for (i = 0; i < MAX_LISTENERS; i++)
listeners[i] = NULL;
return 0;
}
/**
* Starts a new dispatcher specialised in handling incoming connections
* (and putting them on the queue for handling the connections).
*/
dispatcher *
dispatch_new_listener(unsigned char id)
{
return dispatch_new(id, LISTENER, NULL, NULL, 0, 0);
}
/**
* Starts a new dispatcher specialised in handling incoming data on
* existing connections.
*/
dispatcher *
dispatch_new_connection(unsigned char id, router *r, char *allowed_chars,
int maxinplen, int maxmetriclen)
{
return dispatch_new(id, CONNECTION, r, allowed_chars,
maxinplen, maxmetriclen);
}
/**
* Signals this dispatcher to stop whatever it's doing.
*/
void
dispatch_stop(dispatcher *d)
{
__sync_bool_compare_and_swap(&(d->keep_running), 1, 0);
}
/**
* Shuts down dispatcher d. Returns when the dispatcher has terminated.
*/
void
dispatch_shutdown(dispatcher *d)
{
dispatch_stop(d);
pthread_join(d->tid, NULL);
}
/**
* Free up resources taken by dispatcher d. The caller should make sure
* the dispatcher has been shut down at this point.
*/
void
dispatch_free(dispatcher *d)
{
free(d);
}
/**
* Requests this dispatcher to stop processing connections. As soon as
* schedulereload finishes reloading the routes, this dispatcher will
* un-hold and continue processing connections.
* Returns when the dispatcher is no longer doing work.
*/
inline void
dispatch_hold(dispatcher *d)
{
__sync_bool_compare_and_swap(&(d->hold), 0, 1);
}
/**
* Schedules routes r to be put in place for the current routes. The
* replacement is performed at the next cycle of the dispatcher.
*/
inline void
dispatch_schedulereload(dispatcher *d, router *r)
{
d->pending_rtr = r;
__sync_bool_compare_and_swap(&(d->route_refresh_pending), 0, 1);
}
/**
* Returns true if the routes scheduled to be reloaded by a call to
* dispatch_schedulereload() have been activated.
*/
inline char
dispatch_reloadcomplete(dispatcher *d)
{
return __sync_bool_compare_and_swap(&(d->route_refresh_pending), 0, 0);
}
/**
* Returns the wall-clock time in milliseconds consumed by this dispatcher.
*/
inline size_t
dispatch_get_ticks(dispatcher *self)
{
return __sync_add_and_fetch(&(self->ticks), 0);
}
/**
* Returns the wall-clock time consumed since last call to this
* function.
*/
inline size_t
dispatch_get_ticks_sub(dispatcher *self)
{
size_t d = dispatch_get_ticks(self) - self->prevticks;
self->prevticks += d;
return d;
}
/**
* Returns the wall-clock time in milliseconds consumed while sleeping
* by this dispatcher.
*/
inline size_t
dispatch_get_sleeps(dispatcher *self)
{
return __sync_add_and_fetch(&(self->sleeps), 0);
}
/**
* Returns the wall-clock time consumed while sleeping since last call
* to this function.
*/
inline size_t
dispatch_get_sleeps_sub(dispatcher *self)
{
size_t d = dispatch_get_sleeps(self) - self->prevsleeps;
self->prevsleeps += d;
return d;
}
/**
* Returns the number of metrics dispatched since start.
*/
inline size_t
dispatch_get_metrics(dispatcher *self)
{
return __sync_add_and_fetch(&(self->metrics), 0);
}
/**
* Returns the number of metrics dispatched since last call to this
* function.
*/
inline size_t
dispatch_get_metrics_sub(dispatcher *self)
{
size_t d = dispatch_get_metrics(self) - self->prevmetrics;
self->prevmetrics += d;
return d;
}
/**
* Returns the number of metrics that were explicitly or implicitly
* blackholed since start.
*/
inline size_t
dispatch_get_blackholes(dispatcher *self)
{
return __sync_add_and_fetch(&(self->blackholes), 0);
}
/**
* Returns the number of metrics that were blackholed since last call to
* this function.
*/
inline size_t
dispatch_get_blackholes_sub(dispatcher *self)
{
size_t d = dispatch_get_blackholes(self) - self->prevblackholes;
self->prevblackholes += d;
return d;
}
/**
* Returns the number of metrics that were discarded since start.
*/
inline size_t
dispatch_get_discards(dispatcher *self)
{
return __sync_add_and_fetch(&(self->discards), 0);
}
/**
* Returns the number of metrics that were discarded since last call to
* this function.
*/
inline size_t
dispatch_get_discards_sub(dispatcher *self)
{
size_t d = dispatch_get_discards(self) - self->prevdiscards;
self->prevdiscards += d;
return d;
}
/**
* Returns the number of accepted connections thusfar.
*/
inline size_t
dispatch_get_accepted_connections(void)
{
return __sync_add_and_fetch(&(acceptedconnections), 0);
}
/**
* Returns the number of closed connections thusfar.
*/
inline size_t
dispatch_get_closed_connections(void)
{
return __sync_add_and_fetch(&(closedconnections), 0);
}
|