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
|
/* Copyright (C) CZ.NIC, z.s.p.o. <knot-resolver@labs.nic.cz>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "kresconfig.h"
#include <ucw/lib.h>
#include <sys/socket.h>
#if ENABLE_XDP
#include <libknot/xdp/xdp.h>
#endif
#include "lib/log.h"
#include "lib/utils.h"
#include "daemon/io.h"
#include "daemon/udp_queue.h"
#include "daemon/worker.h"
#include "daemon/defer.h"
#include "daemon/proxyv2.h"
#include "daemon/session2.h"
#define VERBOSE_LOG(session, fmt, ...) do {\
if (kr_log_is_debug(PROTOLAYER, NULL)) {\
const char *sess_dir = (session)->outgoing ? "out" : "in";\
kr_log_debug(PROTOLAYER, "[%08X] (%s) " fmt, \
(session)->log_id, sess_dir, __VA_ARGS__);\
}\
} while (0);\
static uint32_t next_log_id = 1;
struct protolayer_globals protolayer_globals[PROTOLAYER_TYPE_COUNT] = {{0}};
static const enum protolayer_type protolayer_grp_udp53[] = {
PROTOLAYER_TYPE_UDP,
PROTOLAYER_TYPE_PROXYV2_DGRAM,
PROTOLAYER_TYPE_DEFER,
PROTOLAYER_TYPE_DNS_DGRAM,
};
static const enum protolayer_type protolayer_grp_tcp53[] = {
PROTOLAYER_TYPE_TCP,
PROTOLAYER_TYPE_PROXYV2_STREAM,
PROTOLAYER_TYPE_DEFER,
PROTOLAYER_TYPE_DNS_MULTI_STREAM,
};
static const enum protolayer_type protolayer_grp_dot[] = {
PROTOLAYER_TYPE_TCP,
PROTOLAYER_TYPE_PROXYV2_STREAM,
PROTOLAYER_TYPE_DEFER,
PROTOLAYER_TYPE_TLS,
PROTOLAYER_TYPE_DNS_MULTI_STREAM,
};
static const enum protolayer_type protolayer_grp_doh[] = {
PROTOLAYER_TYPE_TCP,
PROTOLAYER_TYPE_PROXYV2_STREAM,
PROTOLAYER_TYPE_DEFER,
PROTOLAYER_TYPE_TLS,
PROTOLAYER_TYPE_HTTP,
PROTOLAYER_TYPE_DNS_UNSIZED_STREAM,
};
static const enum protolayer_type protolayer_grp_doq[] = {
// not yet used
PROTOLAYER_TYPE_NULL,
};
struct protolayer_grp {
const enum protolayer_type *layers;
size_t num_layers;
};
#define PROTOLAYER_GRP(p_array) { \
.layers = (p_array), \
.num_layers = sizeof((p_array)) / sizeof((p_array)[0]), \
}
/** Sequences of layers, or groups, mapped by `enum kr_proto`.
*
* Each group represents a sequence of layers in the unwrap direction (wrap
* direction being the opposite). The sequence dictates the order in which
* individual layers are processed. This macro is used to generate global data
* about groups.
*
* To define a new group, add a new entry in the `KR_PROTO_MAP()` macro and
* create a new static `protolayer_grp_*` array above, similarly to the already
* existing ones. Each array must end with `PROTOLAYER_TYPE_NULL`, to
* indicate the end of the list of protocol layers. The array name's suffix must
* be the one defined as *Variable name* (2nd parameter) in the
* `KR_PROTO_MAP` macro. */
static const struct protolayer_grp protolayer_grps[KR_PROTO_COUNT] = {
#define XX(cid, vid, name) [KR_PROTO_##cid] = PROTOLAYER_GRP(protolayer_grp_##vid),
KR_PROTO_MAP(XX)
#undef XX
};
const char *protolayer_layer_name(enum protolayer_type p)
{
switch (p) {
case PROTOLAYER_TYPE_NULL:
return "(null)";
#define XX(cid) case PROTOLAYER_TYPE_ ## cid: \
return #cid;
PROTOLAYER_TYPE_MAP(XX)
#undef XX
default:
return "(invalid)";
}
}
const char *protolayer_event_name(enum protolayer_event_type e)
{
switch (e) {
case PROTOLAYER_EVENT_NULL:
return "(null)";
#define XX(cid) case PROTOLAYER_EVENT_ ## cid: \
return #cid;
PROTOLAYER_EVENT_MAP(XX)
#undef XX
default:
return "(invalid)";
}
}
const char *protolayer_payload_name(enum protolayer_payload_type p)
{
switch (p) {
case PROTOLAYER_PAYLOAD_NULL:
return "(null)";
#define XX(cid, name) case PROTOLAYER_PAYLOAD_ ## cid: \
return (name);
PROTOLAYER_PAYLOAD_MAP(XX)
#undef XX
default:
return "(invalid)";
}
}
/* Forward decls. */
static int session2_transport_pushv(struct session2 *s,
struct iovec *iov, int iovcnt,
bool iov_short_lived,
const struct comm_info *comm,
protolayer_finished_cb cb, void *baton);
static inline int session2_transport_push(struct session2 *s,
char *buf, size_t buf_len,
bool buf_short_lived,
const struct comm_info *comm,
protolayer_finished_cb cb, void *baton);
static int session2_transport_event(struct session2 *s,
enum protolayer_event_type event,
void *baton);
static size_t iovecs_size(const struct iovec *iov, int cnt)
{
size_t sum = 0;
for (int i = 0; i < cnt; i++) {
sum += iov[i].iov_len;
}
return sum;
}
static size_t iovecs_copy(void *dest, const struct iovec *iov, int cnt,
size_t max_len)
{
const size_t pld_size = iovecs_size(iov, cnt);
const size_t copy_size = MIN(max_len, pld_size);
char *cur = dest;
size_t remaining = copy_size;
for (int i = 0; i < cnt && remaining; i++) {
size_t l = iov[i].iov_len;
size_t to_copy = MIN(l, remaining);
memcpy(cur, iov[i].iov_base, to_copy);
remaining -= l;
cur += l;
}
kr_assert(remaining == 0 && (cur - (char *)dest) == copy_size);
return copy_size;
}
size_t protolayer_payload_size(const struct protolayer_payload *payload)
{
switch (payload->type) {
case PROTOLAYER_PAYLOAD_BUFFER:
return payload->buffer.len;
case PROTOLAYER_PAYLOAD_IOVEC:
return iovecs_size(payload->iovec.iov, payload->iovec.cnt);
case PROTOLAYER_PAYLOAD_WIRE_BUF:
return wire_buf_data_length(payload->wire_buf);
case PROTOLAYER_PAYLOAD_NULL:
return 0;
default:
kr_assert(false && "Invalid payload type");
return 0;
}
}
size_t protolayer_payload_copy(void *dest,
const struct protolayer_payload *payload,
size_t max_len)
{
const size_t pld_size = protolayer_payload_size(payload);
const size_t copy_size = MIN(max_len, pld_size);
if (payload->type == PROTOLAYER_PAYLOAD_BUFFER) {
memcpy(dest, payload->buffer.buf, copy_size);
return copy_size;
} else if (payload->type == PROTOLAYER_PAYLOAD_IOVEC) {
char *cur = dest;
size_t remaining = copy_size;
for (int i = 0; i < payload->iovec.cnt && remaining; i++) {
size_t l = payload->iovec.iov[i].iov_len;
size_t to_copy = MIN(l, remaining);
memcpy(cur, payload->iovec.iov[i].iov_base, to_copy);
remaining -= l;
cur += l;
}
kr_assert(remaining == 0 && (cur - (char *)dest) == copy_size);
return copy_size;
} else if (payload->type == PROTOLAYER_PAYLOAD_WIRE_BUF) {
memcpy(dest, wire_buf_data(payload->wire_buf), copy_size);
return copy_size;
} else if(!payload->type) {
return 0;
} else {
kr_assert(false && "Invalid payload type");
return 0;
}
}
struct protolayer_payload protolayer_payload_as_buffer(
const struct protolayer_payload *payload)
{
if (payload->type == PROTOLAYER_PAYLOAD_BUFFER)
return *payload;
if (payload->type == PROTOLAYER_PAYLOAD_WIRE_BUF) {
struct protolayer_payload new_payload = {
.type = PROTOLAYER_PAYLOAD_BUFFER,
.short_lived = payload->short_lived,
.ttl = payload->ttl,
.buffer = {
.buf = wire_buf_data(payload->wire_buf),
.len = wire_buf_data_length(payload->wire_buf)
}
};
wire_buf_reset(payload->wire_buf);
return new_payload;
}
kr_assert(false && "Unsupported payload type.");
return (struct protolayer_payload){
.type = PROTOLAYER_PAYLOAD_NULL
};
}
size_t protolayer_queue_count_payload(const protolayer_iter_ctx_queue_t *queue)
{
if (!queue || queue_len(*queue) == 0)
return 0;
size_t sum = 0;
/* We're only reading from the queue, but we need to discard the
* `const` so that `queue_it_begin()` accepts it. As long as
* `queue_it_` operations do not write into the queue (which they do
* not, checked at the time of writing), we should be safely in the
* defined behavior territory. */
queue_it_t(struct protolayer_iter_ctx *) it =
queue_it_begin(*(protolayer_iter_ctx_queue_t *)queue);
for (; !queue_it_finished(it); queue_it_next(it)) {
struct protolayer_iter_ctx *ctx = queue_it_val(it);
sum += protolayer_payload_size(&ctx->payload);
}
return sum;
}
bool protolayer_queue_has_payload(const protolayer_iter_ctx_queue_t *queue)
{
if (!queue || queue_len(*queue) == 0)
return false;
/* We're only reading from the queue, but we need to discard the
* `const` so that `queue_it_begin()` accepts it. As long as
* `queue_it_` operations do not write into the queue (which they do
* not, checked at the time of writing), we should be safely in the
* defined behavior territory. */
queue_it_t(struct protolayer_iter_ctx *) it =
queue_it_begin(*(protolayer_iter_ctx_queue_t *)queue);
for (; !queue_it_finished(it); queue_it_next(it)) {
struct protolayer_iter_ctx *ctx = queue_it_val(it);
if (protolayer_payload_size(&ctx->payload))
return true;
}
return false;
}
/** Gets layer-specific session data for the layer with the specified index
* from the manager. */
static inline struct protolayer_data *protolayer_sess_data_get(
struct session2 *s, size_t layer_ix)
{
const struct protolayer_grp *grp = &protolayer_grps[s->proto];
if (kr_fails_assert(layer_ix < grp->num_layers))
return NULL;
/* See doc comment of `struct session2::layer_data` */
const ssize_t *offsets = (ssize_t *)s->layer_data;
char *pl_data_beg = &s->layer_data[2 * grp->num_layers * sizeof(*offsets)];
ssize_t offset = offsets[layer_ix];
if (offset < 0) /* No session data for this layer */
return NULL;
return (struct protolayer_data *)(pl_data_beg + offset);
}
void *protolayer_sess_data_get_current(struct protolayer_iter_ctx *ctx)
{
return protolayer_sess_data_get(ctx->session, ctx->layer_ix);
}
/** Gets layer-specific iteration data for the layer with the specified index
* from the context. */
static inline struct protolayer_data *protolayer_iter_data_get(
struct protolayer_iter_ctx *ctx, size_t layer_ix)
{
struct session2 *s = ctx->session;
const struct protolayer_grp *grp = &protolayer_grps[s->proto];
if (kr_fails_assert(layer_ix < grp->num_layers))
return NULL;
/* See doc comment of `struct session2::layer_data` */
const ssize_t *offsets = (ssize_t *)&s->layer_data[grp->num_layers * sizeof(*offsets)];
ssize_t offset = offsets[layer_ix];
if (offset < 0) /* No iteration data for this layer */
return NULL;
return (struct protolayer_data *)(ctx->data + offset);
}
void *protolayer_iter_data_get_current(struct protolayer_iter_ctx *ctx)
{
return protolayer_iter_data_get(ctx, ctx->layer_ix);
}
static inline ssize_t session2_get_protocol(
struct session2 *s, enum protolayer_type protocol)
{
const struct protolayer_grp *grp = &protolayer_grps[s->proto];
for (ssize_t i = 0; i < grp->num_layers; i++) {
enum protolayer_type found = grp->layers[i];
if (protocol == found)
return i;
}
return -1;
}
static inline bool protolayer_iter_ctx_is_last(struct protolayer_iter_ctx *ctx)
{
unsigned int last_ix = (ctx->direction == PROTOLAYER_UNWRAP)
? protolayer_grps[ctx->session->proto].num_layers - 1
: 0;
return ctx->layer_ix == last_ix;
}
static inline void protolayer_iter_ctx_next(struct protolayer_iter_ctx *ctx)
{
if (ctx->direction == PROTOLAYER_UNWRAP)
ctx->layer_ix++;
else
ctx->layer_ix--;
}
static inline const char *layer_name(enum kr_proto grp, ssize_t layer_ix)
{
if (grp >= KR_PROTO_COUNT)
return "(invalid)";
enum protolayer_type p = protolayer_grps[grp].layers[layer_ix];
return protolayer_layer_name(p);
}
static inline const char *layer_name_ctx(struct protolayer_iter_ctx *ctx)
{
return layer_name(ctx->session->proto, ctx->layer_ix);
}
static int protolayer_iter_ctx_finish(struct protolayer_iter_ctx *ctx, int ret)
{
struct session2 *s = ctx->session;
const struct protolayer_globals *globals = &protolayer_globals[s->proto];
const struct protolayer_grp *grp = &protolayer_grps[s->proto];
for (size_t i = 0; i < grp->num_layers; i++) {
struct protolayer_data *d = protolayer_iter_data_get(ctx, i);
if (globals->iter_deinit)
globals->iter_deinit(ctx, d);
}
if (ret) {
VERBOSE_LOG(s, "layer context of group '%s' (on %u: %s) ended with return code %d\n",
kr_proto_name(s->proto),
ctx->layer_ix, layer_name_ctx(ctx), ret);
}
if (ctx->status) {
VERBOSE_LOG(s, "iteration of group '%s' (on %u: %s) ended with status '%s (%d)'\n",
kr_proto_name(s->proto),
ctx->layer_ix, layer_name_ctx(ctx),
kr_strerror(ctx->status), ctx->status);
}
if (ctx->finished_cb)
ctx->finished_cb(ret, s, ctx->comm, ctx->finished_cb_baton);
mm_ctx_delete(&ctx->pool);
free(ctx);
session2_unhandle(s);
return ret;
}
static void protolayer_push_finished(int status, struct session2 *s, const struct comm_info *comm, void *baton)
{
struct protolayer_iter_ctx *ctx = baton;
ctx->status = status;
protolayer_iter_ctx_finish(ctx, PROTOLAYER_RET_NORMAL);
}
/** Pushes the specified protocol layer's payload to the session's transport. */
static int protolayer_push(struct protolayer_iter_ctx *ctx)
{
struct session2 *session = ctx->session;
if (ctx->payload.type == PROTOLAYER_PAYLOAD_WIRE_BUF) {
ctx->payload = protolayer_payload_as_buffer(&ctx->payload);
}
if (kr_log_is_debug(PROTOLAYER, NULL)) {
VERBOSE_LOG(session, "Pushing %s\n",
protolayer_payload_name(ctx->payload.type));
}
if (ctx->payload.type == PROTOLAYER_PAYLOAD_BUFFER) {
session2_transport_push(session,
ctx->payload.buffer.buf, ctx->payload.buffer.len,
ctx->payload.short_lived,
ctx->comm, protolayer_push_finished, ctx);
} else if (ctx->payload.type == PROTOLAYER_PAYLOAD_IOVEC) {
session2_transport_pushv(session,
ctx->payload.iovec.iov, ctx->payload.iovec.cnt,
ctx->payload.short_lived,
ctx->comm, protolayer_push_finished, ctx);
} else {
kr_assert(false && "Invalid payload type");
return kr_error(EINVAL);
}
return PROTOLAYER_RET_ASYNC;
}
static void protolayer_payload_ensure_long_lived(struct protolayer_iter_ctx *ctx)
{
if (!ctx->payload.short_lived)
return;
size_t buf_len = protolayer_payload_size(&ctx->payload);
if (kr_fails_assert(buf_len))
return;
void *buf = mm_alloc(&ctx->pool, buf_len);
kr_require(buf);
protolayer_payload_copy(buf, &ctx->payload, buf_len);
ctx->payload = protolayer_payload_buffer(buf, buf_len, false);
}
/** Processes as many layers as possible synchronously, returning when either
* a layer has gone asynchronous, or when the whole sequence has finished.
*
* May be called multiple times on the same `ctx` to continue processing after
* an asynchronous operation - user code will do this via *layer sequence return
* functions*. */
static int protolayer_step(struct protolayer_iter_ctx *ctx)
{
while (true) {
if (kr_fails_assert(ctx->session->proto < KR_PROTO_COUNT))
return kr_error(EFAULT);
enum protolayer_type protocol = protolayer_grps[ctx->session->proto].layers[ctx->layer_ix];
struct protolayer_globals *globals = &protolayer_globals[protocol];
bool was_async = ctx->async_mode;
ctx->async_mode = false;
/* Basically if we went asynchronous, we want to "resume" from
* underneath this `if` block. */
if (!was_async) {
ctx->status = 0;
ctx->action = PROTOLAYER_ITER_ACTION_NULL;
protolayer_iter_cb cb = (ctx->direction == PROTOLAYER_UNWRAP)
? globals->unwrap : globals->wrap;
if (ctx->session->closing) {
return protolayer_iter_ctx_finish(
ctx, kr_error(ECANCELED));
}
if (cb) {
struct protolayer_data *sess_data = protolayer_sess_data_get(
ctx->session, ctx->layer_ix);
struct protolayer_data *iter_data = protolayer_iter_data_get(
ctx, ctx->layer_ix);
enum protolayer_iter_cb_result result = cb(sess_data, iter_data, ctx);
if (kr_fails_assert(result == PROTOLAYER_ITER_CB_RESULT_MAGIC)) {
/* Callback did not use a *layer
* sequence return function* (see
* glossary). */
return protolayer_iter_ctx_finish(ctx, kr_error(EINVAL));
}
} else {
ctx->action = PROTOLAYER_ITER_ACTION_CONTINUE;
}
if (!ctx->action) {
/* We're going asynchronous - the next step is
* probably going to be from some sort of a
* callback and we will "resume" from underneath
* this `if` block. */
ctx->async_mode = true;
protolayer_payload_ensure_long_lived(ctx);
return PROTOLAYER_RET_ASYNC;
}
}
if (kr_fails_assert(ctx->action)) {
return protolayer_iter_ctx_finish(ctx, kr_error(EINVAL));
}
if (ctx->action == PROTOLAYER_ITER_ACTION_BREAK) {
return protolayer_iter_ctx_finish(
ctx, PROTOLAYER_RET_NORMAL);
}
if (kr_fails_assert(ctx->status == 0)) {
/* Status should be zero without a BREAK. */
return protolayer_iter_ctx_finish(ctx, kr_error(EINVAL));
}
if (ctx->action == PROTOLAYER_ITER_ACTION_CONTINUE) {
if (protolayer_iter_ctx_is_last(ctx)) {
if (ctx->direction == PROTOLAYER_WRAP)
return protolayer_push(ctx);
return protolayer_iter_ctx_finish(
ctx, PROTOLAYER_RET_NORMAL);
}
protolayer_iter_ctx_next(ctx);
continue;
}
/* Should never get here */
kr_assert(false && "Invalid layer callback action");
return protolayer_iter_ctx_finish(ctx, kr_error(EINVAL));
}
}
/** Submits the specified buffer to the sequence of layers represented by the
* specified protolayer manager. The sequence will be processed in the
* specified `direction`, starting by the layer specified by `layer_ix`.
*
* Returns PROTOLAYER_RET_NORMAL when all layers have finished,
* PROTOLAYER_RET_ASYNC when some layers are asynchronous and waiting for
* continuation, or a negative number for errors (kr_error). */
static int session2_submit(
struct session2 *session,
enum protolayer_direction direction, size_t layer_ix,
struct protolayer_payload payload, const struct comm_info *comm,
protolayer_finished_cb cb, void *baton)
{
if (session->closing)
return kr_error(ECANCELED);
if (session->ref_count >= INT_MAX)
return kr_error(ETOOMANYREFS);
if (kr_fails_assert(session->proto < KR_PROTO_COUNT))
return kr_error(EFAULT);
bool had_comm_param = (comm != NULL);
if (!had_comm_param)
comm = &session->comm_storage;
// DEFER: at this point we might start doing nontrivial work,
// but we may not know the client's IP yet.
// Note two cases: incoming session (new request)
// vs. outgoing session (resuming work on some request)
if ((direction == PROTOLAYER_UNWRAP) && (layer_ix == 0))
defer_sample_start();
struct protolayer_iter_ctx *ctx = malloc(session->iter_ctx_size);
kr_require(ctx);
VERBOSE_LOG(session,
"%s submitted to grp '%s' in %s direction (%zu: %s)\n",
protolayer_payload_name(payload.type),
kr_proto_name(session->proto),
(direction == PROTOLAYER_UNWRAP) ? "unwrap" : "wrap",
layer_ix, layer_name(session->proto, layer_ix));
*ctx = (struct protolayer_iter_ctx) {
.payload = payload,
.direction = direction,
.layer_ix = layer_ix,
.session = session,
.finished_cb = cb,
.finished_cb_baton = baton
};
session->ref_count++;
if (had_comm_param) {
struct comm_addr_storage *addrst = &ctx->comm_addr_storage;
if (comm->src_addr) {
int len = kr_sockaddr_len(comm->src_addr);
kr_require(len > 0 && len <= sizeof(union kr_sockaddr));
memcpy(&addrst->src_addr, comm->src_addr, len);
ctx->comm_storage.src_addr = &addrst->src_addr.ip;
}
if (comm->comm_addr) {
int len = kr_sockaddr_len(comm->comm_addr);
kr_require(len > 0 && len <= sizeof(union kr_sockaddr));
memcpy(&addrst->comm_addr, comm->comm_addr, len);
ctx->comm_storage.comm_addr = &addrst->comm_addr.ip;
}
if (comm->dst_addr) {
int len = kr_sockaddr_len(comm->dst_addr);
kr_require(len > 0 && len <= sizeof(union kr_sockaddr));
memcpy(&addrst->dst_addr, comm->dst_addr, len);
ctx->comm_storage.dst_addr = &addrst->dst_addr.ip;
}
ctx->comm = &ctx->comm_storage;
} else {
ctx->comm = &session->comm_storage;
}
mm_ctx_mempool(&ctx->pool, CPU_PAGE_SIZE);
const struct protolayer_grp *grp = &protolayer_grps[session->proto];
for (size_t i = 0; i < grp->num_layers; i++) {
struct protolayer_globals *globals = &protolayer_globals[grp->layers[i]];
struct protolayer_data *iter_data = protolayer_iter_data_get(ctx, i);
if (iter_data) {
memset(iter_data, 0, globals->iter_size);
iter_data->session = session;
}
if (globals->iter_init)
globals->iter_init(ctx, iter_data);
}
int ret = protolayer_step(ctx);
if ((direction == PROTOLAYER_UNWRAP) && (layer_ix == 0))
defer_sample_stop();
return ret;
}
static void *get_init_param(enum protolayer_type p,
struct protolayer_data_param *layer_param,
size_t layer_param_count)
{
if (!layer_param || !layer_param_count)
return NULL;
for (size_t i = 0; i < layer_param_count; i++) {
if (layer_param[i].protocol == p)
return layer_param[i].param;
}
return NULL;
}
/** Called by *Layer sequence return functions* to proceed with protolayer
* processing. If the */
static inline void maybe_async_do_step(struct protolayer_iter_ctx *ctx)
{
if (ctx->async_mode)
protolayer_step(ctx);
}
enum protolayer_iter_cb_result protolayer_continue(struct protolayer_iter_ctx *ctx)
{
ctx->action = PROTOLAYER_ITER_ACTION_CONTINUE;
maybe_async_do_step(ctx);
return PROTOLAYER_ITER_CB_RESULT_MAGIC;
}
enum protolayer_iter_cb_result protolayer_break(struct protolayer_iter_ctx *ctx, int status)
{
ctx->status = status;
ctx->action = PROTOLAYER_ITER_ACTION_BREAK;
maybe_async_do_step(ctx);
return PROTOLAYER_ITER_CB_RESULT_MAGIC;
}
int wire_buf_init(struct wire_buf *wb, size_t initial_size)
{
char *buf = malloc(initial_size);
kr_require(buf);
*wb = (struct wire_buf){
.buf = buf,
.size = initial_size
};
return kr_ok();
}
void wire_buf_deinit(struct wire_buf *wb)
{
free(wb->buf);
}
int wire_buf_reserve(struct wire_buf *wb, size_t size)
{
if (wb->buf && wb->size >= size)
return kr_ok();
char *newbuf = realloc(wb->buf, size);
kr_require(newbuf);
wb->buf = newbuf;
wb->size = size;
return kr_ok();
}
int wire_buf_consume(struct wire_buf *wb, size_t length)
{
size_t ne = wb->end + length;
if (kr_fails_assert(wb->buf && ne <= wb->size))
return kr_error(EINVAL);
wb->end = ne;
return kr_ok();
}
int wire_buf_trim(struct wire_buf *wb, size_t length)
{
size_t ns = wb->start + length;
if (kr_fails_assert(ns <= wb->end))
return kr_error(EINVAL);
wb->start = ns;
return kr_ok();
}
int wire_buf_movestart(struct wire_buf *wb)
{
if (kr_fails_assert(wb->buf))
return kr_error(EINVAL);
if (wb->start == 0)
return kr_ok();
size_t len = wire_buf_data_length(wb);
if (len) {
if (wb->start < len)
memmove(wb->buf, wire_buf_data(wb), len);
else
memcpy(wb->buf, wire_buf_data(wb), len);
}
wb->start = 0;
wb->end = len;
return kr_ok();
}
int wire_buf_reset(struct wire_buf *wb)
{
wb->start = 0;
wb->end = 0;
return kr_ok();
}
struct session2 *session2_new(enum session2_transport_type transport_type,
enum kr_proto proto,
struct protolayer_data_param *layer_param,
size_t layer_param_count,
bool outgoing)
{
kr_require(transport_type && proto);
size_t session_size = sizeof(struct session2);
size_t iter_ctx_size = sizeof(struct protolayer_iter_ctx);
const struct protolayer_grp *grp = &protolayer_grps[proto];
if (kr_fails_assert(grp->num_layers))
return NULL;
size_t wire_buf_length = 0;
ssize_t offsets[2 * grp->num_layers];
session_size += sizeof(offsets);
ssize_t *sess_offsets = offsets;
ssize_t *iter_offsets = &offsets[grp->num_layers];
/* Space for layer-specific data, guaranteeing alignment */
size_t total_sess_data_size = 0;
size_t total_iter_data_size = 0;
for (size_t i = 0; i < grp->num_layers; i++) {
const struct protolayer_globals *g = &protolayer_globals[grp->layers[i]];
sess_offsets[i] = g->sess_size ? total_sess_data_size : -1;
total_sess_data_size += ALIGN_TO(g->sess_size, CPU_STRUCT_ALIGN);
iter_offsets[i] = g->iter_size ? total_iter_data_size : -1;
total_iter_data_size += ALIGN_TO(g->iter_size, CPU_STRUCT_ALIGN);
size_t wire_buf_overhead = (g->wire_buf_overhead_cb)
? g->wire_buf_overhead_cb(outgoing)
: g->wire_buf_overhead;
wire_buf_length += wire_buf_overhead;
}
session_size += total_sess_data_size;
iter_ctx_size += total_iter_data_size;
struct session2 *s = malloc(session_size);
kr_require(s);
*s = (struct session2) {
.transport = {
.type = transport_type,
},
.log_id = next_log_id++,
.outgoing = outgoing,
.tasks = trie_create(NULL),
.proto = proto,
.iter_ctx_size = iter_ctx_size,
};
memcpy(&s->layer_data, offsets, sizeof(offsets));
queue_init(s->waiting);
int ret = wire_buf_init(&s->wire_buf, wire_buf_length);
kr_require(!ret);
ret = uv_timer_init(uv_default_loop(), &s->timer);
kr_require(!ret);
s->timer.data = s;
s->ref_count++; /* Session owns the timer */
/* Initialize the layer's session data */
for (size_t i = 0; i < grp->num_layers; i++) {
struct protolayer_globals *globals = &protolayer_globals[grp->layers[i]];
struct protolayer_data *sess_data = protolayer_sess_data_get(s, i);
if (sess_data) {
memset(sess_data, 0, globals->sess_size);
sess_data->session = s;
}
void *param = get_init_param(grp->layers[i], layer_param, layer_param_count);
if (globals->sess_init)
globals->sess_init(s, sess_data, param);
}
session2_touch(s);
return s;
}
/** De-allocates the session. Must only be called once the underlying IO handle
* and timer are already closed, otherwise may leak resources. */
static void session2_free(struct session2 *s)
{
const struct protolayer_grp *grp = &protolayer_grps[s->proto];
for (size_t i = 0; i < grp->num_layers; i++) {
struct protolayer_globals *globals = &protolayer_globals[grp->layers[i]];
if (globals->sess_deinit) {
struct protolayer_data *sess_data = protolayer_sess_data_get(s, i);
globals->sess_deinit(s, sess_data);
}
}
wire_buf_deinit(&s->wire_buf);
trie_free(s->tasks);
queue_deinit(s->waiting);
free(s);
}
void session2_unhandle(struct session2 *s)
{
if (kr_fails_assert(s->ref_count > 0)) {
session2_free(s);
return;
}
s->ref_count--;
if (s->ref_count <= 0)
session2_free(s);
}
int session2_start_read(struct session2 *session)
{
if (session->transport.type == SESSION2_TRANSPORT_IO)
return io_start_read(session->transport.io.handle);
/* TODO - probably just some event for this */
kr_assert(false && "Parent start_read unsupported");
return kr_error(EINVAL);
}
int session2_stop_read(struct session2 *session)
{
if (session->transport.type == SESSION2_TRANSPORT_IO)
return io_stop_read(session->transport.io.handle);
/* TODO - probably just some event for this */
kr_assert(false && "Parent stop_read unsupported");
return kr_error(EINVAL);
}
struct sockaddr *session2_get_peer(struct session2 *s)
{
while (s && s->transport.type == SESSION2_TRANSPORT_PARENT)
s = s->transport.parent;
return (s && s->transport.type == SESSION2_TRANSPORT_IO)
? &s->transport.io.peer.ip
: NULL;
}
struct sockaddr *session2_get_sockname(struct session2 *s)
{
while (s && s->transport.type == SESSION2_TRANSPORT_PARENT)
s = s->transport.parent;
return (s && s->transport.type == SESSION2_TRANSPORT_IO)
? &s->transport.io.sockname.ip
: NULL;
}
uv_handle_t *session2_get_handle(struct session2 *s)
{
while (s && s->transport.type == SESSION2_TRANSPORT_PARENT)
s = s->transport.parent;
return (s && s->transport.type == SESSION2_TRANSPORT_IO)
? s->transport.io.handle
: NULL;
}
static void session2_on_timeout(uv_timer_t *timer)
{
defer_sample_start();
struct session2 *s = timer->data;
session2_event(s, s->timer_event, NULL);
defer_sample_stop();
}
int session2_timer_start(struct session2 *s, enum protolayer_event_type event, uint64_t timeout, uint64_t repeat)
{
s->timer_event = event;
return uv_timer_start(&s->timer, session2_on_timeout, timeout, repeat);
}
int session2_timer_restart(struct session2 *s)
{
return uv_timer_again(&s->timer);
}
int session2_timer_stop(struct session2 *s)
{
return uv_timer_stop(&s->timer);
}
int session2_tasklist_add(struct session2 *session, struct qr_task *task)
{
trie_t *t = session->tasks;
uint16_t task_msg_id = 0;
const char *key = NULL;
size_t key_len = 0;
if (session->outgoing) {
knot_pkt_t *pktbuf = worker_task_get_pktbuf(task);
task_msg_id = knot_wire_get_id(pktbuf->wire);
key = (const char *)&task_msg_id;
key_len = sizeof(task_msg_id);
} else {
key = (const char *)&task;
key_len = sizeof(char *);
}
trie_val_t *v = trie_get_ins(t, key, key_len);
if (kr_fails_assert(v))
return kr_error(ENOMEM);
if (*v == NULL) {
*v = task;
worker_task_ref(task);
} else if (kr_fails_assert(*v == task)) {
return kr_error(EINVAL);
}
return kr_ok();
}
int session2_tasklist_del(struct session2 *session, struct qr_task *task)
{
trie_t *t = session->tasks;
uint16_t task_msg_id = 0;
const char *key = NULL;
size_t key_len = 0;
trie_val_t val;
if (session->outgoing) {
knot_pkt_t *pktbuf = worker_task_get_pktbuf(task);
task_msg_id = knot_wire_get_id(pktbuf->wire);
key = (const char *)&task_msg_id;
key_len = sizeof(task_msg_id);
} else {
key = (const char *)&task;
key_len = sizeof(char *);
}
int ret = trie_del(t, key, key_len, &val);
if (ret == KNOT_EOK) {
kr_require(val == task);
worker_task_unref(val);
}
return ret;
}
struct qr_task *session2_tasklist_get_first(struct session2 *session)
{
trie_val_t *val = trie_get_first(session->tasks, NULL, NULL);
return val ? (struct qr_task *) *val : NULL;
}
struct qr_task *session2_tasklist_del_first(struct session2 *session, bool deref)
{
trie_val_t val = NULL;
int res = trie_del_first(session->tasks, NULL, NULL, &val);
if (res != KNOT_EOK) {
val = NULL;
} else if (deref) {
worker_task_unref(val);
}
return (struct qr_task *)val;
}
struct qr_task *session2_tasklist_find_msgid(const struct session2 *session, uint16_t msg_id)
{
if (kr_fails_assert(session->outgoing))
return NULL;
trie_t *t = session->tasks;
struct qr_task *ret = NULL;
trie_val_t *val = trie_get_try(t, (char *)&msg_id, sizeof(msg_id));
if (val) {
ret = *val;
}
return ret;
}
struct qr_task *session2_tasklist_del_msgid(const struct session2 *session, uint16_t msg_id)
{
if (kr_fails_assert(session->outgoing))
return NULL;
trie_t *t = session->tasks;
struct qr_task *ret = NULL;
const char *key = (const char *)&msg_id;
size_t key_len = sizeof(msg_id);
trie_val_t val;
int res = trie_del(t, key, key_len, &val);
if (res == KNOT_EOK) {
if (worker_task_numrefs(val) > 1) {
ret = val;
}
worker_task_unref(val);
}
return ret;
}
void session2_tasklist_finalize(struct session2 *session, int status)
{
while (session2_tasklist_get_len(session) > 0) {
struct qr_task *t = session2_tasklist_del_first(session, false);
kr_require(worker_task_numrefs(t) > 0);
worker_task_finalize(t, status);
worker_task_unref(t);
}
}
int session2_tasklist_finalize_expired(struct session2 *session)
{
int ret = 0;
queue_t(struct qr_task *) q;
uint64_t now = kr_now();
trie_t *t = session->tasks;
trie_it_t *it;
queue_init(q);
for (it = trie_it_begin(t); !trie_it_finished(it); trie_it_next(it)) {
trie_val_t *v = trie_it_val(it);
struct qr_task *task = (struct qr_task *)*v;
if ((now - worker_task_creation_time(task)) >= KR_RESOLVE_TIME_LIMIT) {
struct kr_request *req = worker_task_request(task);
if (!kr_fails_assert(req))
kr_query_inform_timeout(req, req->current_query);
queue_push(q, task);
worker_task_ref(task);
}
}
trie_it_free(it);
struct qr_task *task = NULL;
uint16_t msg_id = 0;
char *key = (char *)&task;
int32_t keylen = sizeof(struct qr_task *);
if (session->outgoing) {
key = (char *)&msg_id;
keylen = sizeof(msg_id);
}
while (queue_len(q) > 0) {
task = queue_head(q);
if (session->outgoing) {
knot_pkt_t *pktbuf = worker_task_get_pktbuf(task);
msg_id = knot_wire_get_id(pktbuf->wire);
}
int res = trie_del(t, key, keylen, NULL);
if (!worker_task_finished(task)) {
/* task->pending_count must be zero,
* but there are can be followers,
* so run worker_task_subreq_finalize() to ensure retrying
* for all the followers. */
worker_task_subreq_finalize(task);
worker_task_finalize(task, KR_STATE_FAIL);
}
if (res == KNOT_EOK) {
worker_task_unref(task);
}
queue_pop(q);
worker_task_unref(task);
++ret;
}
queue_deinit(q);
return ret;
}
int session2_waitinglist_push(struct session2 *session, struct qr_task *task)
{
queue_push(session->waiting, task);
worker_task_ref(task);
return kr_ok();
}
struct qr_task *session2_waitinglist_get(const struct session2 *session)
{
return (queue_len(session->waiting) > 0) ? (queue_head(session->waiting)) : NULL;
}
struct qr_task *session2_waitinglist_pop(struct session2 *session, bool deref)
{
struct qr_task *t = session2_waitinglist_get(session);
queue_pop(session->waiting);
if (deref) {
worker_task_unref(t);
}
return t;
}
void session2_waitinglist_retry(struct session2 *session, bool increase_timeout_cnt)
{
while (!session2_waitinglist_is_empty(session)) {
struct qr_task *task = session2_waitinglist_pop(session, false);
if (increase_timeout_cnt) {
worker_task_timeout_inc(task);
}
worker_task_step(task, session2_get_peer(session), NULL);
worker_task_unref(task);
}
}
void session2_waitinglist_finalize(struct session2 *session, int status)
{
while (!session2_waitinglist_is_empty(session)) {
struct qr_task *t = session2_waitinglist_pop(session, false);
worker_task_finalize(t, status);
worker_task_unref(t);
}
}
void session2_penalize(struct session2 *session)
{
if (session->was_useful || !session->outgoing)
return;
/* We want to penalize the IP address, if a task is asking a query.
* It might not be the right task, but that doesn't matter so much
* for attributing the useless session to the IP address. */
struct qr_task *t = session2_tasklist_get_first(session);
struct kr_query *qry = NULL;
if (t) {
struct kr_request *req = worker_task_request(t);
qry = array_tail(req->rplan.pending);
}
if (qry) /* We reuse the error for connection, as it's quite similar. */
qry->server_selection.error(qry, worker_task_get_transport(t),
KR_SELECTION_TCP_CONNECT_FAILED);
}
int session2_unwrap(struct session2 *s, struct protolayer_payload payload,
const struct comm_info *comm, protolayer_finished_cb cb,
void *baton)
{
return session2_submit(s, PROTOLAYER_UNWRAP,
0, payload, comm, cb, baton);
}
int session2_unwrap_after(struct session2 *s, enum protolayer_type protocol,
struct protolayer_payload payload,
const struct comm_info *comm,
protolayer_finished_cb cb, void *baton)
{
ssize_t layer_ix = session2_get_protocol(s, protocol);
bool ok = layer_ix >= 0 && layer_ix + 1 < protolayer_grps[s->proto].num_layers;
if (kr_fails_assert(ok)) // not found or "last layer"
return kr_error(EINVAL);
return session2_submit(s, PROTOLAYER_UNWRAP,
layer_ix + 1, payload, comm, cb, baton);
}
int session2_wrap(struct session2 *s, struct protolayer_payload payload,
const struct comm_info *comm, protolayer_finished_cb cb,
void *baton)
{
return session2_submit(s, PROTOLAYER_WRAP,
protolayer_grps[s->proto].num_layers - 1,
payload, comm, cb, baton);
}
int session2_wrap_after(struct session2 *s, enum protolayer_type protocol,
struct protolayer_payload payload,
const struct comm_info *comm,
protolayer_finished_cb cb, void *baton)
{
ssize_t layer_ix = session2_get_protocol(s, protocol);
if (kr_fails_assert(layer_ix > 0)) // not found or "last layer"
return kr_error(EINVAL);
return session2_submit(s, PROTOLAYER_WRAP, layer_ix - 1,
payload, comm, cb, baton);
}
static void session2_event_wrap(struct session2 *s, enum protolayer_event_type event, void *baton)
{
bool cont;
const struct protolayer_grp *grp = &protolayer_grps[s->proto];
for (ssize_t i = grp->num_layers - 1; i >= 0; i--) {
struct protolayer_globals *globals = &protolayer_globals[grp->layers[i]];
if (globals->event_wrap) {
struct protolayer_data *sess_data = protolayer_sess_data_get(s, i);
cont = globals->event_wrap(event, &baton, s, sess_data);
} else {
cont = true;
}
if (!cont)
return;
}
session2_transport_event(s, event, baton);
}
static void session2_event_unwrap(struct session2 *s, ssize_t start_ix, enum protolayer_event_type event, void *baton)
{
bool cont;
const struct protolayer_grp *grp = &protolayer_grps[s->proto];
for (ssize_t i = start_ix; i < grp->num_layers; i++) {
struct protolayer_globals *globals = &protolayer_globals[grp->layers[i]];
if (globals->event_unwrap) {
struct protolayer_data *sess_data = protolayer_sess_data_get(s, i);
cont = globals->event_unwrap(event, &baton, s, sess_data);
} else {
cont = true;
}
if (!cont)
return;
}
/* Immediately bounce back in the `wrap` direction.
*
* TODO: This might be undesirable for cases with sub-sessions - the
* current idea is for the layers managing sub-sessions to just return
* `PROTOLAYER_EVENT_CONSUME` on `event_unwrap`, but a more "automatic"
* mechanism may be added when this is relevant, to make it less
* error-prone. */
session2_event_wrap(s, event, baton);
}
void session2_event(struct session2 *s, enum protolayer_event_type event, void *baton)
{
session2_event_unwrap(s, 0, event, baton);
}
void session2_event_after(struct session2 *s, enum protolayer_type protocol,
enum protolayer_event_type event, void *baton)
{
ssize_t start_ix = session2_get_protocol(s, protocol);
if (kr_fails_assert(start_ix >= 0))
return;
session2_event_unwrap(s, start_ix + 1, event, baton);
}
void session2_init_request(struct session2 *s, struct kr_request *req)
{
const struct protolayer_grp *grp = &protolayer_grps[s->proto];
for (ssize_t i = 0; i < grp->num_layers; i++) {
struct protolayer_globals *globals = &protolayer_globals[grp->layers[i]];
if (globals->request_init) {
struct protolayer_data *sess_data = protolayer_sess_data_get(s, i);
globals->request_init(s, req, sess_data);
}
}
}
struct session2_pushv_ctx {
struct session2 *session;
protolayer_finished_cb cb;
const struct comm_info *comm;
void *baton;
char *async_buf;
};
static void session2_transport_parent_pushv_finished(int status,
struct session2 *session,
const struct comm_info *comm,
void *baton)
{
struct session2_pushv_ctx *ctx = baton;
if (ctx->cb)
ctx->cb(status, ctx->session, comm, ctx->baton);
free(ctx->async_buf);
free(ctx);
}
static void session2_transport_pushv_finished(int status, struct session2_pushv_ctx *ctx)
{
if (ctx->cb)
ctx->cb(status, ctx->session, ctx->comm, ctx->baton);
free(ctx->async_buf);
free(ctx);
}
static void session2_transport_udp_queue_pushv_finished(int status, void *baton)
{
session2_transport_pushv_finished(status, baton);
}
static void session2_transport_udp_pushv_finished(uv_udp_send_t *req, int status)
{
session2_transport_pushv_finished(status, req->data);
free(req);
}
static void session2_transport_stream_pushv_finished(uv_write_t *req, int status)
{
session2_transport_pushv_finished(status, req->data);
free(req);
}
#if ENABLE_XDP
static void xdp_tx_waker(uv_idle_t *handle)
{
xdp_handle_data_t *xhd = handle->data;
int ret = knot_xdp_send_finish(xhd->socket);
if (ret != KNOT_EAGAIN && ret != KNOT_EOK)
kr_log_error(XDP, "check: ret = %d, %s\n", ret, knot_strerror(ret));
/* Apparently some drivers need many explicit wake-up calls
* even if we push no additional packets (in case they accumulated a lot) */
if (ret != KNOT_EAGAIN)
uv_idle_stop(handle);
knot_xdp_send_prepare(xhd->socket);
/* LATER(opt.): it _might_ be better for performance to do these two steps
* at different points in time */
while (queue_len(xhd->tx_waker_queue)) {
struct session2_pushv_ctx *ctx = queue_head(xhd->tx_waker_queue);
if (ctx->cb)
ctx->cb(kr_ok(), ctx->session, ctx->comm, ctx->baton);
free(ctx);
queue_pop(xhd->tx_waker_queue);
}
}
#endif
static void session2_transport_pushv_ensure_long_lived(
struct iovec **iov, int *iovcnt, bool iov_short_lived,
struct iovec *out_iovecmem, struct session2_pushv_ctx *ctx)
{
if (!iov_short_lived)
return;
size_t iovsize = iovecs_size(*iov, *iovcnt);
if (kr_fails_assert(iovsize))
return;
void *buf = malloc(iovsize);
kr_require(buf);
iovecs_copy(buf, *iov, *iovcnt, iovsize);
ctx->async_buf = buf;
out_iovecmem->iov_base = buf;
out_iovecmem->iov_len = iovsize;
*iov = out_iovecmem;
*iovcnt = 1;
}
/// Count the total size of an iovec[] in bytes.
static inline size_t iovec_sum(const struct iovec iov[], const int iovcnt)
{
size_t result = 0;
for (int i = 0; i < iovcnt; ++i)
result += iov[i].iov_len;
return result;
}
static int session2_transport_pushv(struct session2 *s,
struct iovec *iov, int iovcnt,
bool iov_short_lived,
const struct comm_info *comm,
protolayer_finished_cb cb, void *baton)
{
struct iovec iovecmem;
if (kr_fails_assert(s))
return kr_error(EINVAL);
struct session2_pushv_ctx *ctx = malloc(sizeof(*ctx));
kr_require(ctx);
*ctx = (struct session2_pushv_ctx){
.session = s,
.cb = cb,
.baton = baton,
.comm = comm
};
int err_ret = kr_ok();
switch (s->transport.type) {
case SESSION2_TRANSPORT_IO:;
uv_handle_t *handle = s->transport.io.handle;
if (kr_fails_assert(handle)) {
err_ret = kr_error(EINVAL);
goto exit_err;
}
if (handle->type == UV_UDP) {
if (ENABLE_SENDMMSG && !s->outgoing) {
int fd;
int ret = uv_fileno(handle, &fd);
if (kr_fails_assert(!ret)) {
err_ret = kr_error(EIO);
goto exit_err;
}
/* TODO: support multiple iovecs properly? */
if (kr_fails_assert(iovcnt == 1)) {
err_ret = kr_error(EINVAL);
goto exit_err;
}
session2_transport_pushv_ensure_long_lived(
&iov, &iovcnt, iov_short_lived,
&iovecmem, ctx);
udp_queue_push(fd, comm->comm_addr, iov->iov_base, iov->iov_len,
session2_transport_udp_queue_pushv_finished,
ctx);
return kr_ok();
} else {
int ret = uv_udp_try_send((uv_udp_t*)handle, (uv_buf_t *)iov, iovcnt,
the_network->enable_connect_udp ? NULL : comm->comm_addr);
if (ret > 0) // equals buffer size, only confuses us
ret = 0;
if (ret == UV_EAGAIN) {
ret = kr_error(ENOBUFS);
session2_event(s, PROTOLAYER_EVENT_OS_BUFFER_FULL, NULL);
}
if (false && ret == UV_EAGAIN) { // XXX: see uv_try_write() below
uv_udp_send_t *req = malloc(sizeof(*req));
req->data = ctx;
session2_transport_pushv_ensure_long_lived(
&iov, &iovcnt, iov_short_lived,
&iovecmem, ctx);
ret = uv_udp_send(req, (uv_udp_t *)handle,
(uv_buf_t *)iov, iovcnt, comm->comm_addr,
session2_transport_udp_pushv_finished);
if (ret)
session2_transport_udp_pushv_finished(req, ret);
return ret;
}
session2_transport_pushv_finished(ret, ctx);
return ret;
}
} else if (handle->type == UV_TCP) {
int ret = uv_try_write((uv_stream_t *)handle, (uv_buf_t *)iov, iovcnt);
// XXX: queueing disabled for now if the OS can't accept the data.
// Typically that happens when OS buffers are full.
// We were missing any handling of partial write success, too.
if (ret == UV_EAGAIN || (ret >= 0 && ret != iovec_sum(iov, iovcnt))) {
ret = kr_error(ENOBUFS);
session2_event(s, PROTOLAYER_EVENT_OS_BUFFER_FULL, NULL);
}
else if (ret > 0) // iovec_sum was checked, let's not get confused anymore
ret = 0;
if (false && ret == UV_EAGAIN) {
uv_write_t *req = malloc(sizeof(*req));
req->data = ctx;
session2_transport_pushv_ensure_long_lived(
&iov, &iovcnt, iov_short_lived,
&iovecmem, ctx);
ret = uv_write(req, (uv_stream_t *)handle, (uv_buf_t *)iov, iovcnt,
session2_transport_stream_pushv_finished);
if (ret)
session2_transport_stream_pushv_finished(req, ret);
return ret;
}
session2_transport_pushv_finished(ret, ctx);
return ret;
#if ENABLE_XDP
} else if (handle->type == UV_POLL) {
xdp_handle_data_t *xhd = handle->data;
if (kr_fails_assert(xhd && xhd->socket)) {
err_ret = kr_error(EIO);
goto exit_err;
}
/* TODO: support multiple iovecs properly? */
if (kr_fails_assert(iovcnt == 1)) {
err_ret = kr_error(EINVAL);
goto exit_err;
}
session2_transport_pushv_ensure_long_lived(
&iov, &iovcnt, iov_short_lived,
&iovecmem, ctx);
knot_xdp_msg_t msg;
/* We don't have a nice way of preserving the _msg_t from frame allocation,
* so we manually redo all other parts of knot_xdp_send_alloc() */
memset(&msg, 0, sizeof(msg));
bool ipv6 = comm->comm_addr->sa_family == AF_INET6;
msg.flags = ipv6 ? KNOT_XDP_MSG_IPV6 : 0;
memcpy(msg.eth_from, comm->eth_from, sizeof(comm->eth_from));
memcpy(msg.eth_to, comm->eth_to, sizeof(comm->eth_to));
const struct sockaddr *ip_from = comm->dst_addr;
const struct sockaddr *ip_to = comm->comm_addr;
memcpy(&msg.ip_from, ip_from, kr_sockaddr_len(ip_from));
memcpy(&msg.ip_to, ip_to, kr_sockaddr_len(ip_to));
msg.payload = *iov;
uint32_t sent;
int ret = knot_xdp_send(xhd->socket, &msg, 1, &sent);
queue_push(xhd->tx_waker_queue, ctx);
uv_idle_start(&xhd->tx_waker, xdp_tx_waker);
kr_log_debug(XDP, "pushed a packet, ret = %d\n", ret);
return kr_ok();
#endif
} else {
kr_assert(false && "Unsupported handle");
err_ret = kr_error(EINVAL);
goto exit_err;
}
case SESSION2_TRANSPORT_PARENT:;
struct session2 *parent = s->transport.parent;
if (kr_fails_assert(parent)) {
err_ret = kr_error(EINVAL);
goto exit_err;
}
int ret = session2_wrap(parent,
protolayer_payload_iovec(iov, iovcnt, iov_short_lived),
comm, session2_transport_parent_pushv_finished,
ctx);
return (ret < 0) ? ret : kr_ok();
default:
kr_assert(false && "Invalid transport");
err_ret = kr_error(EINVAL);
goto exit_err;
}
exit_err:
session2_transport_pushv_finished(err_ret, ctx);
return err_ret;
}
struct push_ctx {
struct iovec iov;
protolayer_finished_cb cb;
void *baton;
};
static void session2_transport_single_push_finished(int status,
struct session2 *s,
const struct comm_info *comm,
void *baton)
{
struct push_ctx *ctx = baton;
if (ctx->cb)
ctx->cb(status, s, comm, ctx->baton);
free(ctx);
}
static inline int session2_transport_push(struct session2 *s,
char *buf, size_t buf_len,
bool buf_short_lived,
const struct comm_info *comm,
protolayer_finished_cb cb, void *baton)
{
struct push_ctx *ctx = malloc(sizeof(*ctx));
kr_require(ctx);
*ctx = (struct push_ctx){
.iov = {
.iov_base = buf,
.iov_len = buf_len
},
.cb = cb,
.baton = baton
};
return session2_transport_pushv(s, &ctx->iov, 1, buf_short_lived, comm,
session2_transport_single_push_finished, ctx);
}
static void on_session2_handle_close(uv_handle_t *handle)
{
struct session2 *session = handle->data;
kr_require(session->transport.type == SESSION2_TRANSPORT_IO &&
session->transport.io.handle == handle);
io_free(handle);
}
static void on_session2_timer_close(uv_handle_t *handle)
{
session2_unhandle(handle->data);
}
static int session2_handle_close(struct session2 *s)
{
if (kr_fails_assert(s->transport.type == SESSION2_TRANSPORT_IO))
return kr_error(EINVAL);
uv_handle_t *handle = s->transport.io.handle;
if (!handle->loop) {
/* This happens when kresd is stopping and the libUV loop has
* been ended. We do not `uv_close` the handles, we just free
* up the memory. */
session2_unhandle(s); /* For timer handle */
io_free(handle); /* This will unhandle the transport handle */
return kr_ok();
}
io_stop_read(handle);
uv_close((uv_handle_t *)&s->timer, on_session2_timer_close);
uv_close(handle, on_session2_handle_close);
return kr_ok();
}
static int session2_transport_event(struct session2 *s,
enum protolayer_event_type event,
void *baton)
{
if (s->closing)
return kr_ok();
bool is_close_event = (event == PROTOLAYER_EVENT_CLOSE ||
event == PROTOLAYER_EVENT_FORCE_CLOSE);
if (is_close_event) {
kr_require(session2_is_empty(s));
session2_timer_stop(s);
s->closing = true;
}
switch (s->transport.type) {
case SESSION2_TRANSPORT_IO:;
if (kr_fails_assert(s->transport.io.handle)) {
return kr_error(EINVAL);
}
if (is_close_event)
return session2_handle_close(s);
return kr_ok();
case SESSION2_TRANSPORT_PARENT:;
session2_event_wrap(s, event, baton);
return kr_ok();
default:
kr_assert(false && "Invalid transport");
return kr_error(EINVAL);
}
}
void session2_kill_ioreq(struct session2 *session, struct qr_task *task)
{
if (!session || session->closing)
return;
if (kr_fails_assert(session->outgoing
&& session->transport.type == SESSION2_TRANSPORT_IO
&& session->transport.io.handle))
return;
session2_tasklist_del(session, task);
if (session->transport.io.handle->type == UV_UDP)
session2_close(session);
}
|