1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "ProtocolV2.h"
#include <fmt/format.h>
#include <fmt/ranges.h>
#include "include/msgr.h"
#include "include/random.h"
#include "msg/msg_fmt.h"
#include "crimson/auth/AuthClient.h"
#include "crimson/auth/AuthServer.h"
#include "crimson/common/formatter.h"
#include "crimson/common/log.h"
#include "Errors.h"
#include "SocketMessenger.h"
using namespace ceph::msgr::v2;
using crimson::common::local_conf;
namespace {
// TODO: CEPH_MSGR2_FEATURE_COMPRESSION
const uint64_t CRIMSON_MSGR2_SUPPORTED_FEATURES =
(CEPH_MSGR2_FEATURE_REVISION_1 |
// CEPH_MSGR2_FEATURE_COMPRESSION |
UINT64_C(0));
// Log levels in V2 Protocol:
// * error level, something error that cause connection to terminate:
// - fatal errors;
// - bugs;
// * warn level: something unusual that identifies connection fault or replacement:
// - unstable network;
// - incompatible peer;
// - auth failure;
// - connection race;
// - connection reset;
// * info level, something very important to show connection lifecycle,
// which doesn't happen very frequently;
// * debug level, important logs for debugging, including:
// - all the messages sent/received (-->/<==);
// - all the frames exchanged (WRITE/GOT);
// - important fields updated (UPDATE);
// - connection state transitions (TRIGGER);
// * trace level, trivial logs showing:
// - the exact bytes being sent/received (SEND/RECV(bytes));
// - detailed information of sub-frames;
// - integrity checks;
// - etc.
seastar::logger& logger() {
return crimson::get_logger(ceph_subsys_ms);
}
[[noreturn]] void abort_in_fault() {
throw std::system_error(make_error_code(crimson::net::error::negotiation_failure));
}
[[noreturn]] void abort_protocol() {
throw std::system_error(make_error_code(crimson::net::error::protocol_aborted));
}
#define ABORT_IN_CLOSE(is_dispatch_reset) { \
do_close(is_dispatch_reset); \
abort_protocol(); \
}
inline void expect_tag(const Tag& expected,
const Tag& actual,
crimson::net::SocketConnection& conn,
const char *where) {
if (actual != expected) {
logger().warn("{} {} received wrong tag: {}, expected {}",
conn, where,
static_cast<uint32_t>(actual),
static_cast<uint32_t>(expected));
abort_in_fault();
}
}
inline void unexpected_tag(const Tag& unexpected,
crimson::net::SocketConnection& conn,
const char *where) {
logger().warn("{} {} received unexpected tag: {}",
conn, where, static_cast<uint32_t>(unexpected));
abort_in_fault();
}
inline uint64_t generate_client_cookie() {
return ceph::util::generate_random_number<uint64_t>(
1, std::numeric_limits<uint64_t>::max());
}
} // namespace anonymous
namespace crimson::net {
seastar::future<> ProtocolV2::Timer::backoff(double seconds)
{
logger().warn("{} waiting {} seconds ...", conn, seconds);
cancel();
last_dur_ = seconds;
as = seastar::abort_source();
auto dur = std::chrono::duration_cast<seastar::lowres_clock::duration>(
std::chrono::duration<double>(seconds));
return seastar::sleep_abortable(dur, *as
).handle_exception_type([this] (const seastar::sleep_aborted& e) {
logger().debug("{} wait aborted", conn);
abort_protocol();
});
}
ProtocolV2::ProtocolV2(SocketConnection& conn,
IOHandler &io_handler)
: conn{conn},
messenger{conn.messenger},
io_handler{io_handler},
frame_assembler{FrameAssemblerV2::create(conn)},
auth_meta{seastar::make_lw_shared<AuthConnectionMeta>()},
protocol_timer{conn}
{
io_states = io_handler.get_states();
}
ProtocolV2::~ProtocolV2() {}
void ProtocolV2::start_connect(const entity_addr_t& _peer_addr,
const entity_name_t& _peer_name)
{
assert(seastar::this_shard_id() == conn.get_messenger_shard_id());
ceph_assert(state == state_t::NONE);
ceph_assert(!gate.is_closed());
conn.peer_addr = _peer_addr;
conn.target_addr = _peer_addr;
conn.set_peer_name(_peer_name);
conn.policy = messenger.get_policy(_peer_name.type());
client_cookie = generate_client_cookie();
logger().info("{} ProtocolV2::start_connect(): peer_addr={}, peer_name={}, cc={}"
" policy(lossy={}, server={}, standby={}, resetcheck={})",
conn, _peer_addr, _peer_name, client_cookie,
conn.policy.lossy, conn.policy.server,
conn.policy.standby, conn.policy.resetcheck);
messenger.register_conn(
seastar::static_pointer_cast<SocketConnection>(conn.shared_from_this()));
execute_connecting();
}
void ProtocolV2::start_accept(SocketFRef&& new_socket,
const entity_addr_t& _peer_addr)
{
assert(seastar::this_shard_id() == conn.get_messenger_shard_id());
ceph_assert(state == state_t::NONE);
// until we know better
conn.target_addr = _peer_addr;
frame_assembler->set_socket(std::move(new_socket));
has_socket = true;
is_socket_valid = true;
logger().info("{} ProtocolV2::start_accept(): target_addr={}", conn, _peer_addr);
messenger.accept_conn(
seastar::static_pointer_cast<SocketConnection>(conn.shared_from_this()));
auto cc_seq = crosscore.prepare_submit();
gate.dispatch_in_background("set_accepted_sid", conn, [this, cc_seq] {
return io_handler.set_accepted_sid(
cc_seq,
frame_assembler->get_socket_shard_id(),
seastar::make_foreign(conn.shared_from_this()));
});
execute_accepting();
}
void ProtocolV2::trigger_state_phase1(state_t new_state)
{
ceph_assert_always(!gate.is_closed());
if (new_state == state) {
logger().error("{} is not allowed to re-trigger state {}",
conn, get_state_name(state));
ceph_abort();
}
if (state == state_t::CLOSING) {
logger().error("{} CLOSING is not allowed to trigger state {}",
conn, get_state_name(new_state));
ceph_abort();
}
logger().debug("{} TRIGGER {}, was {}",
conn, get_state_name(new_state), get_state_name(state));
if (state == state_t::READY) {
// from READY
ceph_assert_always(!need_exit_io);
ceph_assert_always(!pr_exit_io.has_value());
need_exit_io = true;
pr_exit_io = seastar::shared_promise<>();
}
if (new_state == state_t::STANDBY && !conn.policy.server) {
need_notify_out = true;
} else {
need_notify_out = false;
}
state = new_state;
}
void ProtocolV2::trigger_state_phase2(
state_t new_state, io_state_t new_io_state)
{
ceph_assert_always(new_state == state);
ceph_assert_always(!gate.is_closed());
ceph_assert_always(!pr_switch_io_shard.has_value());
FrameAssemblerV2Ref fa;
if (new_state == state_t::READY) {
assert(new_io_state == io_state_t::open);
assert(io_handler.get_shard_id() ==
frame_assembler->get_socket_shard_id());
frame_assembler->set_shard_id(io_handler.get_shard_id());
fa = std::move(frame_assembler);
} else {
assert(new_io_state != io_state_t::open);
}
auto cc_seq = crosscore.prepare_submit();
logger().debug("{} send {} IOHandler::set_io_state(): new_state={}, new_io_state={}, "
"fa={}, set_notify_out={}",
conn, cc_seq, get_state_name(new_state), new_io_state,
fa ? fmt::format("(sid={})", fa->get_shard_id()) : "N/A",
need_notify_out);
gate.dispatch_in_background(
"set_io_state", conn,
[this, cc_seq, new_io_state, fa=std::move(fa)]() mutable {
return seastar::smp::submit_to(
io_handler.get_shard_id(),
[this, cc_seq, new_io_state,
fa=std::move(fa), set_notify_out=need_notify_out]() mutable {
return io_handler.set_io_state(
cc_seq, new_io_state, std::move(fa), set_notify_out);
});
});
if (need_exit_io) {
// from READY
auto cc_seq = crosscore.prepare_submit();
logger().debug("{} send {} IOHandler::wait_io_exit_dispatching() ...",
conn, cc_seq);
assert(pr_exit_io.has_value());
assert(new_io_state != io_state_t::open);
need_exit_io = false;
gate.dispatch_in_background("exit_io", conn, [this, cc_seq] {
return seastar::smp::submit_to(
io_handler.get_shard_id(), [this, cc_seq] {
return io_handler.wait_io_exit_dispatching(cc_seq);
}).then([this, cc_seq](auto ret) {
logger().debug("{} finish {} IOHandler::wait_io_exit_dispatching(), {}",
conn, cc_seq, ret.io_states);
frame_assembler = std::move(ret.frame_assembler);
assert(seastar::this_shard_id() == conn.get_messenger_shard_id());
ceph_assert_always(
seastar::this_shard_id() == frame_assembler->get_shard_id());
ceph_assert_always(!frame_assembler->is_socket_valid());
assert(!need_exit_io);
io_states = ret.io_states;
pr_exit_io->set_value();
pr_exit_io = std::nullopt;
});
});
}
}
void ProtocolV2::fault(
state_t expected_state,
const char *where,
std::exception_ptr eptr)
{
assert(expected_state == state_t::CONNECTING ||
expected_state == state_t::ESTABLISHING ||
expected_state == state_t::REPLACING ||
expected_state == state_t::READY);
const char *e_what;
try {
std::rethrow_exception(eptr);
} catch (std::exception &e) {
e_what = e.what();
}
if (state != expected_state) {
logger().info("{} protocol {} {} is aborted at inconsistent {} -- {}",
conn,
get_state_name(expected_state),
where,
get_state_name(state),
e_what);
#ifndef NDEBUG
if (expected_state == state_t::REPLACING) {
assert(state == state_t::CLOSING);
} else if (expected_state == state_t::READY) {
assert(state == state_t::CLOSING ||
state == state_t::REPLACING ||
state == state_t::CONNECTING ||
state == state_t::STANDBY);
} else {
assert(state == state_t::CLOSING ||
state == state_t::REPLACING);
}
#endif
return;
}
assert(state == expected_state);
if (state != state_t::CONNECTING && conn.policy.lossy) {
// socket will be shutdown in do_close()
logger().info("{} protocol {} {} fault on lossy channel, going to CLOSING -- {}",
conn, get_state_name(state), where, e_what);
do_close(true);
return;
}
if (likely(has_socket)) {
if (likely(is_socket_valid)) {
ceph_assert_always(state != state_t::READY);
frame_assembler->shutdown_socket<true>(&gate);
is_socket_valid = false;
} else {
ceph_assert_always(state != state_t::ESTABLISHING);
}
} else { // !has_socket
ceph_assert_always(state == state_t::CONNECTING);
assert(!is_socket_valid);
}
if (conn.policy.server ||
(conn.policy.standby && !io_states.is_out_queued_or_sent())) {
if (conn.policy.server) {
logger().info("{} protocol {} {} fault as server, going to STANDBY {} -- {}",
conn,
get_state_name(state),
where,
io_states,
e_what);
} else {
logger().info("{} protocol {} {} fault with nothing to send, going to STANDBY {} -- {}",
conn,
get_state_name(state),
where,
io_states,
e_what);
}
execute_standby();
} else if (state == state_t::CONNECTING ||
state == state_t::REPLACING) {
logger().info("{} protocol {} {} fault, going to WAIT {} -- {}",
conn,
get_state_name(state),
where,
io_states,
e_what);
execute_wait(false);
} else {
assert(state == state_t::READY ||
state == state_t::ESTABLISHING);
logger().info("{} protocol {} {} fault, going to CONNECTING {} -- {}",
conn,
get_state_name(state),
where,
io_states,
e_what);
execute_connecting();
}
}
void ProtocolV2::reset_session(bool full)
{
server_cookie = 0;
connect_seq = 0;
if (full) {
client_cookie = generate_client_cookie();
peer_global_seq = 0;
}
auto cc_seq = crosscore.prepare_submit();
logger().debug("{} send {} IOHandler::reset_session({})",
conn, cc_seq, full);
io_states.reset_session(full);
gate.dispatch_in_background(
"reset_session", conn, [this, cc_seq, full] {
return seastar::smp::submit_to(
io_handler.get_shard_id(), [this, cc_seq, full] {
return io_handler.reset_session(cc_seq, full);
});
});
// user can make changes
}
seastar::future<std::tuple<entity_type_t, entity_addr_t>>
ProtocolV2::banner_exchange(bool is_connect)
{
// 1. prepare and send banner
bufferlist banner_payload;
encode((uint64_t)CRIMSON_MSGR2_SUPPORTED_FEATURES, banner_payload, 0);
encode((uint64_t)CEPH_MSGR2_REQUIRED_FEATURES, banner_payload, 0);
bufferlist bl;
bl.append(CEPH_BANNER_V2_PREFIX, strlen(CEPH_BANNER_V2_PREFIX));
auto len_payload = static_cast<uint16_t>(banner_payload.length());
encode(len_payload, bl, 0);
bl.claim_append(banner_payload);
logger().debug("{} SEND({}) banner: len_payload={}, supported={}, "
"required={}, banner=\"{}\"",
conn, bl.length(), len_payload,
CRIMSON_MSGR2_SUPPORTED_FEATURES,
CEPH_MSGR2_REQUIRED_FEATURES,
CEPH_BANNER_V2_PREFIX);
#ifdef UNIT_TESTS_BUILT
return frame_assembler->intercept_frame(custom_bp_t::BANNER_WRITE, true
).then([this, bl=std::move(bl)]() mutable {
return frame_assembler->write_flush(std::move(bl));
}
#else
return frame_assembler->write_flush(std::move(bl)
#endif
).then([this] {
// 2. read peer banner
unsigned banner_len = strlen(CEPH_BANNER_V2_PREFIX) + sizeof(ceph_le16);
#ifdef UNIT_TESTS_BUILT
return frame_assembler->intercept_frame(custom_bp_t::BANNER_READ, false
).then([this, banner_len] {
return frame_assembler->read_exactly(banner_len);
});
#else
return frame_assembler->read_exactly(banner_len);
#endif
}).then([this](auto bptr) {
// 3. process peer banner and read banner_payload
unsigned banner_prefix_len = strlen(CEPH_BANNER_V2_PREFIX);
logger().debug("{} RECV({}) banner: \"{}\"",
conn, bptr.length(),
std::string(bptr.c_str(), banner_prefix_len));
if (memcmp(bptr.c_str(), CEPH_BANNER_V2_PREFIX, banner_prefix_len) != 0) {
if (memcmp(bptr.c_str(), CEPH_BANNER, strlen(CEPH_BANNER)) == 0) {
logger().warn("{} peer is using V1 protocol", conn);
} else {
logger().warn("{} peer sent bad banner", conn);
}
abort_in_fault();
}
bptr.set_offset(bptr.offset() + banner_prefix_len);
bptr.set_length(bptr.length() - banner_prefix_len);
assert(bptr.length() == sizeof(ceph_le16));
uint16_t payload_len;
bufferlist buf;
buf.append(std::move(bptr));
auto ti = buf.cbegin();
try {
decode(payload_len, ti);
} catch (const buffer::error &e) {
logger().warn("{} decode banner payload len failed", conn);
abort_in_fault();
}
logger().debug("{} GOT banner: payload_len={}", conn, payload_len);
#ifdef UNIT_TESTS_BUILT
return frame_assembler->intercept_frame(
custom_bp_t::BANNER_PAYLOAD_READ, false
).then([this, payload_len] {
return frame_assembler->read(payload_len);
});
#else
return frame_assembler->read(payload_len);
#endif
}).then([this, is_connect] (bufferlist bl) {
// 4. process peer banner_payload and send HelloFrame
auto p = bl.cbegin();
uint64_t _peer_supported_features;
uint64_t _peer_required_features;
try {
decode(_peer_supported_features, p);
decode(_peer_required_features, p);
} catch (const buffer::error &e) {
logger().warn("{} decode banner payload failed", conn);
abort_in_fault();
}
logger().debug("{} RECV({}) banner features: supported={} required={}",
conn, bl.length(),
_peer_supported_features, _peer_required_features);
// Check feature bit compatibility
uint64_t supported_features = CRIMSON_MSGR2_SUPPORTED_FEATURES;
uint64_t required_features = CEPH_MSGR2_REQUIRED_FEATURES;
if ((required_features & _peer_supported_features) != required_features) {
logger().error("{} peer does not support all required features"
" required={} peer_supported={}",
conn, required_features, _peer_supported_features);
ABORT_IN_CLOSE(is_connect);
}
if ((supported_features & _peer_required_features) != _peer_required_features) {
logger().error("{} we do not support all peer required features"
" peer_required={} supported={}",
conn, _peer_required_features, supported_features);
ABORT_IN_CLOSE(is_connect);
}
peer_supported_features = _peer_supported_features;
bool is_rev1 = HAVE_MSGR2_FEATURE(peer_supported_features, REVISION_1);
frame_assembler->set_is_rev1(is_rev1);
auto hello = HelloFrame::Encode(messenger.get_mytype(),
conn.target_addr);
logger().debug("{} WRITE HelloFrame: my_type={}, peer_addr={}",
conn, ceph_entity_type_name(messenger.get_mytype()),
conn.target_addr);
return frame_assembler->write_flush_frame(hello);
}).then([this] {
//5. read peer HelloFrame
return frame_assembler->read_main_preamble();
}).then([this](auto ret) {
expect_tag(Tag::HELLO, ret.tag, conn, "read_hello_frame");
return frame_assembler->read_frame_payload();
}).then([this](auto payload) {
// 6. process peer HelloFrame
auto hello = HelloFrame::Decode(payload->back());
logger().debug("{} GOT HelloFrame: my_type={} peer_addr={}",
conn, ceph_entity_type_name(hello.entity_type()),
hello.peer_addr());
return seastar::make_ready_future<std::tuple<entity_type_t, entity_addr_t>>(
std::make_tuple(hello.entity_type(), hello.peer_addr()));
});
}
// CONNECTING state
seastar::future<> ProtocolV2::handle_auth_reply()
{
return frame_assembler->read_main_preamble(
).then([this](auto ret) {
switch (ret.tag) {
case Tag::AUTH_BAD_METHOD:
return frame_assembler->read_frame_payload(
).then([this](auto payload) {
// handle_auth_bad_method() logic
auto bad_method = AuthBadMethodFrame::Decode(payload->back());
logger().warn("{} GOT AuthBadMethodFrame: method={} result={}, "
"allowed_methods={}, allowed_modes={}",
conn, bad_method.method(), cpp_strerror(bad_method.result()),
bad_method.allowed_methods(), bad_method.allowed_modes());
ceph_assert(messenger.get_auth_client());
int r = messenger.get_auth_client()->handle_auth_bad_method(
conn, *auth_meta,
bad_method.method(), bad_method.result(),
bad_method.allowed_methods(), bad_method.allowed_modes());
if (r < 0) {
logger().warn("{} auth_client handle_auth_bad_method returned {}",
conn, r);
abort_in_fault();
}
return client_auth(bad_method.allowed_methods());
});
case Tag::AUTH_REPLY_MORE:
return frame_assembler->read_frame_payload(
).then([this](auto payload) {
// handle_auth_reply_more() logic
auto auth_more = AuthReplyMoreFrame::Decode(payload->back());
logger().debug("{} GOT AuthReplyMoreFrame: payload_len={}",
conn, auth_more.auth_payload().length());
ceph_assert(messenger.get_auth_client());
// let execute_connecting() take care of the thrown exception
auto reply = messenger.get_auth_client()->handle_auth_reply_more(
conn, *auth_meta, auth_more.auth_payload());
auto more_reply = AuthRequestMoreFrame::Encode(reply);
logger().debug("{} WRITE AuthRequestMoreFrame: payload_len={}",
conn, reply.length());
return frame_assembler->write_flush_frame(more_reply);
}).then([this] {
return handle_auth_reply();
});
case Tag::AUTH_DONE:
return frame_assembler->read_frame_payload(
).then([this](auto payload) {
// handle_auth_done() logic
auto auth_done = AuthDoneFrame::Decode(payload->back());
logger().debug("{} GOT AuthDoneFrame: gid={}, con_mode={}, payload_len={}",
conn, auth_done.global_id(),
ceph_con_mode_name(auth_done.con_mode()),
auth_done.auth_payload().length());
ceph_assert(messenger.get_auth_client());
int r = messenger.get_auth_client()->handle_auth_done(
conn,
*auth_meta,
auth_done.global_id(),
auth_done.con_mode(),
auth_done.auth_payload());
if (r < 0) {
logger().warn("{} auth_client handle_auth_done returned {}", conn, r);
abort_in_fault();
}
auth_meta->con_mode = auth_done.con_mode();
frame_assembler->create_session_stream_handlers(*auth_meta, false);
return finish_auth();
});
default: {
unexpected_tag(ret.tag, conn, "handle_auth_reply");
return seastar::now();
}
}
});
}
seastar::future<> ProtocolV2::client_auth(std::vector<uint32_t> &allowed_methods)
{
// send_auth_request() logic
ceph_assert(messenger.get_auth_client());
try {
auto [auth_method, preferred_modes, bl] =
messenger.get_auth_client()->get_auth_request(conn, *auth_meta);
auth_meta->auth_method = auth_method;
auto frame = AuthRequestFrame::Encode(auth_method, preferred_modes, bl);
logger().debug("{} WRITE AuthRequestFrame: method={},"
" preferred_modes={}, payload_len={}",
conn, auth_method, preferred_modes, bl.length());
return frame_assembler->write_flush_frame(frame
).then([this] {
return handle_auth_reply();
});
} catch (const crimson::auth::error& e) {
logger().error("{} get_initial_auth_request returned {}", conn, e.what());
ABORT_IN_CLOSE(true);
return seastar::now();
}
}
seastar::future<ProtocolV2::next_step_t>
ProtocolV2::process_wait()
{
return frame_assembler->read_frame_payload(
).then([this](auto payload) {
// handle_wait() logic
logger().debug("{} GOT WaitFrame", conn);
WaitFrame::Decode(payload->back());
return next_step_t::wait;
});
}
seastar::future<ProtocolV2::next_step_t>
ProtocolV2::client_connect()
{
// send_client_ident() logic
uint64_t flags = 0;
if (conn.policy.lossy) {
flags |= CEPH_MSG_CONNECT_LOSSY;
}
auto client_ident = ClientIdentFrame::Encode(
messenger.get_myaddrs(),
conn.target_addr,
messenger.get_myname().num(),
global_seq,
conn.policy.features_supported,
conn.policy.features_required | msgr2_required, flags,
client_cookie);
logger().debug("{} WRITE ClientIdentFrame: addrs={}, target={}, gid={},"
" gs={}, features_supported={}, features_required={},"
" flags={}, cookie={}",
conn, messenger.get_myaddrs(), conn.target_addr,
messenger.get_myname().num(), global_seq,
conn.policy.features_supported,
conn.policy.features_required | msgr2_required,
flags, client_cookie);
return frame_assembler->write_flush_frame(client_ident
).then([this] {
return frame_assembler->read_main_preamble();
}).then([this](auto ret) {
switch (ret.tag) {
case Tag::IDENT_MISSING_FEATURES:
return frame_assembler->read_frame_payload(
).then([this](auto payload) {
// handle_ident_missing_features() logic
auto ident_missing = IdentMissingFeaturesFrame::Decode(payload->back());
logger().warn("{} GOT IdentMissingFeaturesFrame: features={}"
" (client does not support all server features)",
conn, ident_missing.features());
abort_in_fault();
return next_step_t::none;
});
case Tag::WAIT:
return process_wait();
case Tag::SERVER_IDENT:
return frame_assembler->read_frame_payload(
).then([this](auto payload) {
if (unlikely(state != state_t::CONNECTING)) {
logger().debug("{} triggered {} at receiving SERVER_IDENT",
conn, get_state_name(state));
abort_protocol();
}
// handle_server_ident() logic
auto cc_seq = crosscore.prepare_submit();
logger().debug("{} send {} IOHandler::requeue_out_sent()",
conn, cc_seq);
io_states.requeue_out_sent();
gate.dispatch_in_background(
"requeue_out_sent", conn, [this, cc_seq] {
return seastar::smp::submit_to(
io_handler.get_shard_id(), [this, cc_seq] {
return io_handler.requeue_out_sent(cc_seq);
});
});
auto server_ident = ServerIdentFrame::Decode(payload->back());
logger().debug("{} GOT ServerIdentFrame:"
" addrs={}, gid={}, gs={},"
" features_supported={}, features_required={},"
" flags={}, cookie={}",
conn,
server_ident.addrs(), server_ident.gid(),
server_ident.global_seq(),
server_ident.supported_features(),
server_ident.required_features(),
server_ident.flags(), server_ident.cookie());
// is this who we intended to talk to?
// be a bit forgiving here, since we may be connecting based on addresses parsed out
// of mon_host or something.
if (!server_ident.addrs().contains(conn.target_addr)) {
logger().warn("{} peer identifies as {}, does not include {}",
conn, server_ident.addrs(), conn.target_addr);
throw std::system_error(
make_error_code(crimson::net::error::bad_peer_address));
}
server_cookie = server_ident.cookie();
// TODO: change peer_addr to entity_addrvec_t
if (server_ident.addrs().front() != conn.peer_addr) {
logger().warn("{} peer advertises as {}, does not match {}",
conn, server_ident.addrs(), conn.peer_addr);
throw std::system_error(
make_error_code(crimson::net::error::bad_peer_address));
}
if (conn.get_peer_id() != entity_name_t::NEW &&
conn.get_peer_id() != server_ident.gid()) {
logger().error("{} connection peer id ({}) does not match "
"what it should be ({}) during connecting, close",
conn, server_ident.gid(), conn.get_peer_id());
ABORT_IN_CLOSE(true);
}
conn.set_peer_id(server_ident.gid());
conn.set_features(server_ident.supported_features() &
conn.policy.features_supported);
logger().debug("{} UPDATE: features={}", conn, conn.get_features());
peer_global_seq = server_ident.global_seq();
bool lossy = server_ident.flags() & CEPH_MSG_CONNECT_LOSSY;
if (lossy != conn.policy.lossy) {
logger().warn("{} UPDATE Policy(lossy={}) from server flags", conn, lossy);
conn.policy.lossy = lossy;
}
if (lossy && (connect_seq != 0 || server_cookie != 0)) {
logger().warn("{} UPDATE cs=0({}) sc=0({}) for lossy policy",
conn, connect_seq, server_cookie);
connect_seq = 0;
server_cookie = 0;
}
return seastar::make_ready_future<next_step_t>(next_step_t::ready);
});
default: {
unexpected_tag(ret.tag, conn, "post_client_connect");
return seastar::make_ready_future<next_step_t>(next_step_t::none);
}
}
});
}
seastar::future<ProtocolV2::next_step_t>
ProtocolV2::client_reconnect()
{
// send_reconnect() logic
auto reconnect = ReconnectFrame::Encode(messenger.get_myaddrs(),
client_cookie,
server_cookie,
global_seq,
connect_seq,
io_states.in_seq);
logger().debug("{} WRITE ReconnectFrame: addrs={}, client_cookie={},"
" server_cookie={}, gs={}, cs={}, in_seq={}",
conn, messenger.get_myaddrs(),
client_cookie, server_cookie,
global_seq, connect_seq, io_states.in_seq);
return frame_assembler->write_flush_frame(reconnect).then([this] {
return frame_assembler->read_main_preamble();
}).then([this](auto ret) {
switch (ret.tag) {
case Tag::SESSION_RETRY_GLOBAL:
return frame_assembler->read_frame_payload(
).then([this](auto payload) {
// handle_session_retry_global() logic
auto retry = RetryGlobalFrame::Decode(payload->back());
logger().warn("{} GOT RetryGlobalFrame: gs={}",
conn, retry.global_seq());
global_seq = messenger.get_global_seq(retry.global_seq());
logger().warn("{} UPDATE: gs={} for retry global", conn, global_seq);
return client_reconnect();
});
case Tag::SESSION_RETRY:
return frame_assembler->read_frame_payload(
).then([this](auto payload) {
// handle_session_retry() logic
auto retry = RetryFrame::Decode(payload->back());
logger().warn("{} GOT RetryFrame: cs={}",
conn, retry.connect_seq());
connect_seq = retry.connect_seq() + 1;
logger().warn("{} UPDATE: cs={}", conn, connect_seq);
return client_reconnect();
});
case Tag::SESSION_RESET:
return frame_assembler->read_frame_payload(
).then([this](auto payload) {
if (unlikely(state != state_t::CONNECTING)) {
logger().debug("{} triggered {} before reset_session()",
conn, get_state_name(state));
abort_protocol();
}
// handle_session_reset() logic
auto reset = ResetFrame::Decode(payload->back());
logger().warn("{} GOT ResetFrame: full={}", conn, reset.full());
reset_session(reset.full());
// user can make changes
return client_connect();
});
case Tag::WAIT:
return process_wait();
case Tag::SESSION_RECONNECT_OK:
return frame_assembler->read_frame_payload(
).then([this](auto payload) {
if (unlikely(state != state_t::CONNECTING)) {
logger().debug("{} triggered {} at receiving RECONNECT_OK",
conn, get_state_name(state));
abort_protocol();
}
// handle_reconnect_ok() logic
auto reconnect_ok = ReconnectOkFrame::Decode(payload->back());
auto cc_seq = crosscore.prepare_submit();
logger().debug("{} GOT ReconnectOkFrame: msg_seq={}, "
"send {} IOHandler::requeue_out_sent_up_to()",
conn, reconnect_ok.msg_seq(), cc_seq);
io_states.requeue_out_sent_up_to();
auto msg_seq = reconnect_ok.msg_seq();
gate.dispatch_in_background(
"requeue_out_reconnecting", conn, [this, cc_seq, msg_seq] {
return seastar::smp::submit_to(
io_handler.get_shard_id(), [this, cc_seq, msg_seq] {
return io_handler.requeue_out_sent_up_to(cc_seq, msg_seq);
});
});
return seastar::make_ready_future<next_step_t>(next_step_t::ready);
});
default: {
unexpected_tag(ret.tag, conn, "post_client_reconnect");
return seastar::make_ready_future<next_step_t>(next_step_t::none);
}
}
});
}
void ProtocolV2::execute_connecting()
{
ceph_assert_always(!is_socket_valid);
trigger_state(state_t::CONNECTING, io_state_t::delay);
gated_execute("execute_connecting", conn, [this] {
global_seq = messenger.get_global_seq();
assert(client_cookie != 0);
if (!conn.policy.lossy && server_cookie != 0) {
++connect_seq;
logger().debug("{} UPDATE: gs={}, cs={} for reconnect",
conn, global_seq, connect_seq);
} else { // conn.policy.lossy || server_cookie == 0
assert(connect_seq == 0);
assert(server_cookie == 0);
logger().debug("{} UPDATE: gs={} for connect", conn, global_seq);
}
return wait_exit_io().then([this] {
#ifdef UNIT_TESTS_BUILT
// process custom_bp_t::SOCKET_CONNECTING
// supports CONTINUE/FAULT/BLOCK
if (!conn.interceptor) {
return seastar::now();
}
return conn.interceptor->intercept(
conn, {Breakpoint{custom_bp_t::SOCKET_CONNECTING}}
).then([this](bp_action_t action) {
switch (action) {
case bp_action_t::CONTINUE:
return seastar::now();
case bp_action_t::FAULT:
logger().info("[Test] got FAULT");
abort_in_fault();
case bp_action_t::BLOCK:
logger().info("[Test] got BLOCK");
return conn.interceptor->blocker.block();
default:
ceph_abort("unexpected action from trap");
return seastar::now();
}
});;
}).then([this] {
#endif
ceph_assert_always(frame_assembler);
if (unlikely(state != state_t::CONNECTING)) {
logger().debug("{} triggered {} before Socket::connect()",
conn, get_state_name(state));
abort_protocol();
}
return Socket::connect(conn.peer_addr);
}).then([this](SocketRef _new_socket) {
logger().debug("{} socket connected", conn);
if (unlikely(state != state_t::CONNECTING)) {
logger().debug("{} triggered {} during Socket::connect()",
conn, get_state_name(state));
return _new_socket->close().then([sock=std::move(_new_socket)] {
abort_protocol();
});
}
SocketFRef new_socket = seastar::make_foreign(std::move(_new_socket));
if (!has_socket) {
frame_assembler->set_socket(std::move(new_socket));
has_socket = true;
} else {
gate.dispatch_in_background(
"replace_socket_connecting",
conn,
[this, new_socket=std::move(new_socket)]() mutable {
return frame_assembler->replace_shutdown_socket(std::move(new_socket));
}
);
}
is_socket_valid = true;
return seastar::now();
}).then([this] {
auth_meta = seastar::make_lw_shared<AuthConnectionMeta>();
frame_assembler->reset_handlers();
frame_assembler->start_recording();
return banner_exchange(true);
}).then([this] (auto&& ret) {
auto [_peer_type, _my_addr_from_peer] = std::move(ret);
if (conn.get_peer_type() != _peer_type) {
logger().warn("{} connection peer type does not match what peer advertises {} != {}",
conn, ceph_entity_type_name(conn.get_peer_type()),
ceph_entity_type_name(_peer_type));
ABORT_IN_CLOSE(true);
}
if (unlikely(state != state_t::CONNECTING)) {
logger().debug("{} triggered {} during banner_exchange(), abort",
conn, get_state_name(state));
abort_protocol();
}
frame_assembler->learn_socket_ephemeral_port_as_connector(
_my_addr_from_peer.get_port());
if (unlikely(_my_addr_from_peer.is_legacy())) {
logger().warn("{} peer sent a legacy address for me: {}",
conn, _my_addr_from_peer);
throw std::system_error(
make_error_code(crimson::net::error::bad_peer_address));
}
_my_addr_from_peer.set_type(entity_addr_t::TYPE_MSGR2);
messenger.learned_addr(_my_addr_from_peer, conn);
return client_auth();
}).then([this] {
if (server_cookie == 0) {
ceph_assert(connect_seq == 0);
return client_connect();
} else {
ceph_assert(connect_seq > 0);
return client_reconnect();
}
}).then([this] (next_step_t next) {
if (unlikely(state != state_t::CONNECTING)) {
logger().debug("{} triggered {} at the end of execute_connecting()",
conn, get_state_name(state));
abort_protocol();
}
switch (next) {
case next_step_t::ready: {
if (unlikely(state != state_t::CONNECTING)) {
logger().debug("{} triggered {} before dispatch_connect(), abort",
conn, get_state_name(state));
abort_protocol();
}
auto cc_seq = crosscore.prepare_submit();
// there are 2 hops with dispatch_connect()
crosscore.prepare_submit();
logger().info("{} connected: gs={}, pgs={}, cs={}, "
"client_cookie={}, server_cookie={}, {}, new_sid={}, "
"send {} IOHandler::dispatch_connect()",
conn, global_seq, peer_global_seq, connect_seq,
client_cookie, server_cookie, io_states,
frame_assembler->get_socket_shard_id(), cc_seq);
// set io_handler to a new shard
auto new_io_shard = frame_assembler->get_socket_shard_id();
ConnectionFRef conn_fref = seastar::make_foreign(
conn.shared_from_this());
ceph_assert_always(!pr_switch_io_shard.has_value());
pr_switch_io_shard = seastar::shared_promise<>();
return seastar::smp::submit_to(
io_handler.get_shard_id(),
[this, cc_seq, new_io_shard,
conn_fref=std::move(conn_fref)]() mutable {
return io_handler.dispatch_connect(
cc_seq, new_io_shard, std::move(conn_fref));
}).then([this, new_io_shard] {
ceph_assert_always(io_handler.get_shard_id() == new_io_shard);
pr_switch_io_shard->set_value();
pr_switch_io_shard = std::nullopt;
// user can make changes
if (unlikely(state != state_t::CONNECTING)) {
logger().debug("{} triggered {} after dispatch_connect(), abort",
conn, get_state_name(state));
abort_protocol();
}
execute_ready();
});
}
case next_step_t::wait: {
logger().info("{} execute_connecting(): going to WAIT(max-backoff)", conn);
ceph_assert_always(is_socket_valid);
frame_assembler->shutdown_socket<true>(&gate);
is_socket_valid = false;
execute_wait(true);
return seastar::now();
}
default: {
ceph_abort("impossible next step");
}
}
}).handle_exception([this](std::exception_ptr eptr) {
fault(state_t::CONNECTING, "execute_connecting", eptr);
});
});
}
// ACCEPTING state
seastar::future<> ProtocolV2::_auth_bad_method(int r)
{
// _auth_bad_method() logic
ceph_assert(r < 0);
auto [allowed_methods, allowed_modes] =
messenger.get_auth_server()->get_supported_auth_methods(conn.get_peer_type());
auto bad_method = AuthBadMethodFrame::Encode(
auth_meta->auth_method, r, allowed_methods, allowed_modes);
logger().warn("{} WRITE AuthBadMethodFrame: method={}, result={}, "
"allowed_methods={}, allowed_modes={})",
conn, auth_meta->auth_method, cpp_strerror(r),
allowed_methods, allowed_modes);
return frame_assembler->write_flush_frame(bad_method
).then([this] {
return server_auth();
});
}
seastar::future<> ProtocolV2::_handle_auth_request(bufferlist& auth_payload, bool more)
{
// _handle_auth_request() logic
ceph_assert(messenger.get_auth_server());
bufferlist reply;
int r = messenger.get_auth_server()->handle_auth_request(
conn,
*auth_meta,
more,
auth_meta->auth_method,
auth_payload,
&conn.peer_global_id,
&reply);
switch (r) {
// successful
case 1: {
auto auth_done = AuthDoneFrame::Encode(
conn.peer_global_id, auth_meta->con_mode, reply);
logger().debug("{} WRITE AuthDoneFrame: gid={}, con_mode={}, payload_len={}",
conn, conn.peer_global_id,
ceph_con_mode_name(auth_meta->con_mode), reply.length());
return frame_assembler->write_flush_frame(auth_done
).then([this] {
ceph_assert(auth_meta);
frame_assembler->create_session_stream_handlers(*auth_meta, true);
return finish_auth();
});
}
// auth more
case 0: {
auto more = AuthReplyMoreFrame::Encode(reply);
logger().debug("{} WRITE AuthReplyMoreFrame: payload_len={}",
conn, reply.length());
return frame_assembler->write_flush_frame(more
).then([this] {
return frame_assembler->read_main_preamble();
}).then([this](auto ret) {
expect_tag(Tag::AUTH_REQUEST_MORE, ret.tag, conn, "read_auth_request_more");
return frame_assembler->read_frame_payload();
}).then([this](auto payload) {
auto auth_more = AuthRequestMoreFrame::Decode(payload->back());
logger().debug("{} GOT AuthRequestMoreFrame: payload_len={}",
conn, auth_more.auth_payload().length());
return _handle_auth_request(auth_more.auth_payload(), true);
});
}
case -EBUSY: {
logger().warn("{} auth_server handle_auth_request returned -EBUSY", conn);
abort_in_fault();
return seastar::now();
}
default: {
logger().warn("{} auth_server handle_auth_request returned {}", conn, r);
return _auth_bad_method(r);
}
}
}
seastar::future<> ProtocolV2::server_auth()
{
return frame_assembler->read_main_preamble(
).then([this](auto ret) {
expect_tag(Tag::AUTH_REQUEST, ret.tag, conn, "read_auth_request");
return frame_assembler->read_frame_payload();
}).then([this](auto payload) {
// handle_auth_request() logic
auto request = AuthRequestFrame::Decode(payload->back());
logger().debug("{} GOT AuthRequestFrame: method={}, preferred_modes={},"
" payload_len={}",
conn, request.method(), request.preferred_modes(),
request.auth_payload().length());
auth_meta->auth_method = request.method();
auth_meta->con_mode = messenger.get_auth_server()->pick_con_mode(
conn.get_peer_type(), auth_meta->auth_method,
request.preferred_modes());
if (auth_meta->con_mode == CEPH_CON_MODE_UNKNOWN) {
logger().warn("{} auth_server pick_con_mode returned mode CEPH_CON_MODE_UNKNOWN", conn);
return _auth_bad_method(-EOPNOTSUPP);
}
return _handle_auth_request(request.auth_payload(), false);
});
}
bool ProtocolV2::validate_peer_name(const entity_name_t& peer_name) const
{
auto my_peer_name = conn.get_peer_name();
if (my_peer_name.type() != peer_name.type()) {
return false;
}
if (my_peer_name.num() != entity_name_t::NEW &&
peer_name.num() != entity_name_t::NEW &&
my_peer_name.num() != peer_name.num()) {
return false;
}
return true;
}
seastar::future<ProtocolV2::next_step_t>
ProtocolV2::send_wait()
{
auto wait = WaitFrame::Encode();
logger().debug("{} WRITE WaitFrame", conn);
return frame_assembler->write_flush_frame(wait
).then([] {
return next_step_t::wait;
});
}
seastar::future<ProtocolV2::next_step_t>
ProtocolV2::reuse_connection(
ProtocolV2* existing_proto, bool do_reset,
bool reconnect, uint64_t conn_seq, uint64_t msg_seq)
{
if (unlikely(state != state_t::ACCEPTING)) {
logger().debug("{} triggered {} before trigger_replacing()",
conn, get_state_name(state));
abort_protocol();
}
existing_proto->trigger_replacing(reconnect,
do_reset,
frame_assembler->to_replace(),
std::move(auth_meta),
peer_global_seq,
client_cookie,
conn.get_peer_name(),
conn.get_features(),
peer_supported_features,
conn_seq,
msg_seq);
ceph_assert_always(has_socket && is_socket_valid);
is_socket_valid = false;
has_socket = false;
#ifdef UNIT_TESTS_BUILT
if (conn.interceptor) {
conn.interceptor->register_conn_replaced(
conn.get_local_shared_foreign_from_this());
}
#endif
// close this connection because all the necessary information is delivered
// to the exisiting connection, and jump to error handling code to abort the
// current state.
ABORT_IN_CLOSE(false);
return seastar::make_ready_future<next_step_t>(next_step_t::none);
}
seastar::future<ProtocolV2::next_step_t>
ProtocolV2::handle_existing_connection(SocketConnectionRef existing_conn)
{
// handle_existing_connection() logic
ProtocolV2 *existing_proto = dynamic_cast<ProtocolV2*>(
existing_conn->protocol.get());
ceph_assert(existing_proto);
logger().debug("{}(gs={}, pgs={}, cs={}, cc={}, sc={}) connecting,"
" found existing {}(state={}, gs={}, pgs={}, cs={}, cc={}, sc={})",
conn, global_seq, peer_global_seq, connect_seq,
client_cookie, server_cookie,
fmt::ptr(existing_conn.get()), get_state_name(existing_proto->state),
existing_proto->global_seq,
existing_proto->peer_global_seq,
existing_proto->connect_seq,
existing_proto->client_cookie,
existing_proto->server_cookie);
if (!validate_peer_name(existing_conn->get_peer_name())) {
logger().error("{} server_connect: my peer_name doesn't match"
" the existing connection {}, abort", conn, fmt::ptr(existing_conn.get()));
abort_in_fault();
}
if (existing_proto->state == state_t::REPLACING) {
logger().warn("{} server_connect: racing replace happened while"
" replacing existing connection {}, send wait.",
conn, *existing_conn);
return send_wait();
}
if (existing_proto->peer_global_seq > peer_global_seq) {
logger().warn("{} server_connect:"
" this is a stale connection, because peer_global_seq({})"
" < existing->peer_global_seq({}), close this connection"
" in favor of existing connection {}",
conn, peer_global_seq,
existing_proto->peer_global_seq, *existing_conn);
abort_in_fault();
}
if (existing_conn->policy.lossy) {
// existing connection can be thrown out in favor of this one
logger().warn("{} server_connect:"
" existing connection {} is a lossy channel. Close existing in favor of"
" this connection", conn, *existing_conn);
if (unlikely(state != state_t::ACCEPTING)) {
logger().debug("{} triggered {} before execute_establishing()",
conn, get_state_name(state));
abort_protocol();
}
execute_establishing(existing_conn);
return seastar::make_ready_future<next_step_t>(next_step_t::ready);
}
if (existing_proto->server_cookie != 0) {
if (existing_proto->client_cookie != client_cookie) {
// Found previous session
// peer has reset and we're going to reuse the existing connection
// by replacing the socket
logger().warn("{} server_connect:"
" found new session (cs={})"
" when existing {} {} is with stale session (cs={}, ss={}),"
" peer must have reset",
conn,
client_cookie,
get_state_name(existing_proto->state),
*existing_conn,
existing_proto->client_cookie,
existing_proto->server_cookie);
return reuse_connection(existing_proto, conn.policy.resetcheck);
} else {
// session establishment interrupted between client_ident and server_ident,
// continuing...
logger().warn("{} server_connect: found client session with existing {} {}"
" matched (cs={}, ss={}), continuing session establishment",
conn,
get_state_name(existing_proto->state),
*existing_conn,
client_cookie,
existing_proto->server_cookie);
return reuse_connection(existing_proto);
}
} else {
// Looks like a connection race: server and client are both connecting to
// each other at the same time.
if (existing_proto->client_cookie != client_cookie) {
if (existing_conn->peer_wins()) {
// acceptor (this connection, the peer) wins
logger().warn("{} server_connect: connection race detected (cs={}, e_cs={}, ss=0)"
" and win, reusing existing {} {}",
conn,
client_cookie,
existing_proto->client_cookie,
get_state_name(existing_proto->state),
*existing_conn);
return reuse_connection(existing_proto);
} else {
// acceptor (this connection, the peer) loses
logger().warn("{} server_connect: connection race detected (cs={}, e_cs={}, ss=0)"
" and lose to existing {}, ask client to wait",
conn, client_cookie, existing_proto->client_cookie, *existing_conn);
return existing_conn->send_keepalive().then([this] {
return send_wait();
});
}
} else {
logger().warn("{} server_connect: found client session with existing {} {}"
" matched (cs={}, ss={}), continuing session establishment",
conn,
get_state_name(existing_proto->state),
*existing_conn,
client_cookie,
existing_proto->server_cookie);
return reuse_connection(existing_proto);
}
}
}
seastar::future<ProtocolV2::next_step_t>
ProtocolV2::server_connect()
{
return frame_assembler->read_frame_payload(
).then([this](auto payload) {
// handle_client_ident() logic
auto client_ident = ClientIdentFrame::Decode(payload->back());
logger().debug("{} GOT ClientIdentFrame: addrs={}, target={},"
" gid={}, gs={}, features_supported={},"
" features_required={}, flags={}, cookie={}",
conn, client_ident.addrs(), client_ident.target_addr(),
client_ident.gid(), client_ident.global_seq(),
client_ident.supported_features(),
client_ident.required_features(),
client_ident.flags(), client_ident.cookie());
if (client_ident.addrs().empty() ||
client_ident.addrs().front() == entity_addr_t()) {
logger().warn("{} oops, client_ident.addrs() is empty", conn);
throw std::system_error(
make_error_code(crimson::net::error::bad_peer_address));
}
if (!messenger.get_myaddrs().contains(client_ident.target_addr())) {
logger().warn("{} peer is trying to reach {} which is not us ({})",
conn, client_ident.target_addr(), messenger.get_myaddrs());
throw std::system_error(
make_error_code(crimson::net::error::bad_peer_address));
}
conn.peer_addr = client_ident.addrs().front();
logger().debug("{} UPDATE: peer_addr={}", conn, conn.peer_addr);
conn.target_addr = conn.peer_addr;
if (!conn.policy.lossy && !conn.policy.server && conn.target_addr.get_port() <= 0) {
logger().warn("{} we don't know how to reconnect to peer {}",
conn, conn.target_addr);
throw std::system_error(
make_error_code(crimson::net::error::bad_peer_address));
}
if (conn.get_peer_id() != entity_name_t::NEW &&
conn.get_peer_id() != client_ident.gid()) {
logger().error("{} client_ident peer_id ({}) does not match"
" what it should be ({}) during accepting, abort",
conn, client_ident.gid(), conn.get_peer_id());
abort_in_fault();
}
conn.set_peer_id(client_ident.gid());
client_cookie = client_ident.cookie();
uint64_t feat_missing =
(conn.policy.features_required | msgr2_required) &
~(uint64_t)client_ident.supported_features();
if (feat_missing) {
auto ident_missing_features = IdentMissingFeaturesFrame::Encode(feat_missing);
logger().warn("{} WRITE IdentMissingFeaturesFrame: features={} (peer missing)",
conn, feat_missing);
return frame_assembler->write_flush_frame(ident_missing_features
).then([] {
return next_step_t::wait;
});
}
conn.set_features(client_ident.supported_features() &
conn.policy.features_supported);
logger().debug("{} UPDATE: features={}", conn, conn.get_features());
peer_global_seq = client_ident.global_seq();
bool lossy = client_ident.flags() & CEPH_MSG_CONNECT_LOSSY;
if (lossy != conn.policy.lossy) {
logger().warn("{} my lossy policy {} doesn't match client {}, ignore",
conn, conn.policy.lossy, lossy);
}
// Looks good so far, let's check if there is already an existing connection
// to this peer.
SocketConnectionRef existing_conn = messenger.lookup_conn(conn.peer_addr);
if (existing_conn) {
return handle_existing_connection(existing_conn);
} else {
if (unlikely(state != state_t::ACCEPTING)) {
logger().debug("{} triggered {} before execute_establishing()",
conn, get_state_name(state));
abort_protocol();
}
execute_establishing(nullptr);
return seastar::make_ready_future<next_step_t>(next_step_t::ready);
}
});
}
seastar::future<ProtocolV2::next_step_t>
ProtocolV2::read_reconnect()
{
return frame_assembler->read_main_preamble(
).then([this](auto ret) {
expect_tag(Tag::SESSION_RECONNECT, ret.tag, conn, "read_session_reconnect");
return server_reconnect();
});
}
seastar::future<ProtocolV2::next_step_t>
ProtocolV2::send_retry(uint64_t connect_seq)
{
auto retry = RetryFrame::Encode(connect_seq);
logger().warn("{} WRITE RetryFrame: cs={}", conn, connect_seq);
return frame_assembler->write_flush_frame(retry
).then([this] {
return read_reconnect();
});
}
seastar::future<ProtocolV2::next_step_t>
ProtocolV2::send_retry_global(uint64_t global_seq)
{
auto retry = RetryGlobalFrame::Encode(global_seq);
logger().warn("{} WRITE RetryGlobalFrame: gs={}", conn, global_seq);
return frame_assembler->write_flush_frame(retry
).then([this] {
return read_reconnect();
});
}
seastar::future<ProtocolV2::next_step_t>
ProtocolV2::send_reset(bool full)
{
auto reset = ResetFrame::Encode(full);
logger().warn("{} WRITE ResetFrame: full={}", conn, full);
return frame_assembler->write_flush_frame(reset
).then([this] {
return frame_assembler->read_main_preamble();
}).then([this](auto ret) {
expect_tag(Tag::CLIENT_IDENT, ret.tag, conn, "post_send_reset");
return server_connect();
});
}
seastar::future<ProtocolV2::next_step_t>
ProtocolV2::server_reconnect()
{
return frame_assembler->read_frame_payload(
).then([this](auto payload) {
// handle_reconnect() logic
auto reconnect = ReconnectFrame::Decode(payload->back());
logger().debug("{} GOT ReconnectFrame: addrs={}, client_cookie={},"
" server_cookie={}, gs={}, cs={}, msg_seq={}",
conn, reconnect.addrs(),
reconnect.client_cookie(), reconnect.server_cookie(),
reconnect.global_seq(), reconnect.connect_seq(),
reconnect.msg_seq());
// can peer_addrs be changed on-the-fly?
// TODO: change peer_addr to entity_addrvec_t
entity_addr_t paddr = reconnect.addrs().front();
if (paddr.is_msgr2() || paddr.is_any()) {
// good
} else {
logger().warn("{} peer's address {} is not v2", conn, paddr);
throw std::system_error(
make_error_code(crimson::net::error::bad_peer_address));
}
if (conn.peer_addr == entity_addr_t()) {
conn.peer_addr = paddr;
} else if (conn.peer_addr != paddr) {
logger().error("{} peer identifies as {}, while conn.peer_addr={},"
" reconnect failed",
conn, paddr, conn.peer_addr);
throw std::system_error(
make_error_code(crimson::net::error::bad_peer_address));
}
peer_global_seq = reconnect.global_seq();
SocketConnectionRef existing_conn = messenger.lookup_conn(conn.peer_addr);
if (!existing_conn) {
// there is no existing connection therefore cannot reconnect to previous
// session
logger().warn("{} server_reconnect: no existing connection from address {},"
" reseting client", conn, conn.peer_addr);
return send_reset(true);
}
ProtocolV2 *existing_proto = dynamic_cast<ProtocolV2*>(
existing_conn->protocol.get());
ceph_assert(existing_proto);
logger().debug("{}(gs={}, pgs={}, cs={}, cc={}, sc={}) re-connecting,"
" found existing {}(state={}, gs={}, pgs={}, cs={}, cc={}, sc={})",
conn, global_seq, peer_global_seq, reconnect.connect_seq(),
reconnect.client_cookie(), reconnect.server_cookie(),
fmt::ptr(existing_conn.get()),
get_state_name(existing_proto->state),
existing_proto->global_seq,
existing_proto->peer_global_seq,
existing_proto->connect_seq,
existing_proto->client_cookie,
existing_proto->server_cookie);
if (!validate_peer_name(existing_conn->get_peer_name())) {
logger().error("{} server_reconnect: my peer_name doesn't match"
" the existing connection {}, abort", conn, fmt::ptr(existing_conn.get()));
abort_in_fault();
}
if (existing_proto->state == state_t::REPLACING) {
logger().warn("{} server_reconnect: racing replace happened while "
" replacing existing connection {}, retry global.",
conn, *existing_conn);
return send_retry_global(existing_proto->peer_global_seq);
}
if (existing_proto->client_cookie != reconnect.client_cookie()) {
logger().warn("{} server_reconnect:"
" client_cookie mismatch with existing connection {},"
" cc={} rcc={}. I must have reset, reseting client.",
conn, *existing_conn,
existing_proto->client_cookie, reconnect.client_cookie());
return send_reset(conn.policy.resetcheck);
} else if (existing_proto->server_cookie == 0) {
// this happens when:
// - a connects to b
// - a sends client_ident
// - b gets client_ident, sends server_ident and sets cookie X
// - connection fault
// - b reconnects to a with cookie X, connect_seq=1
// - a has cookie==0
logger().warn("{} server_reconnect: I was a client (cc={}) and didn't received the"
" server_ident with existing connection {}."
" Asking peer to resume session establishment",
conn, existing_proto->client_cookie, *existing_conn);
return send_reset(false);
}
if (existing_proto->peer_global_seq > reconnect.global_seq()) {
logger().warn("{} server_reconnect: stale global_seq: exist_pgs({}) > peer_gs({}),"
" with existing connection {},"
" ask client to retry global",
conn, existing_proto->peer_global_seq,
reconnect.global_seq(), *existing_conn);
return send_retry_global(existing_proto->peer_global_seq);
}
if (existing_proto->connect_seq > reconnect.connect_seq()) {
logger().warn("{} server_reconnect: stale peer connect_seq peer_cs({}) < exist_cs({}),"
" with existing connection {}, ask client to retry",
conn, reconnect.connect_seq(),
existing_proto->connect_seq, *existing_conn);
return send_retry(existing_proto->connect_seq);
} else if (existing_proto->connect_seq == reconnect.connect_seq()) {
// reconnect race: both peers are sending reconnect messages
if (existing_conn->peer_wins()) {
// acceptor (this connection, the peer) wins
logger().warn("{} server_reconnect: reconnect race detected (cs={})"
" and win, reusing existing {} {}",
conn,
reconnect.connect_seq(),
get_state_name(existing_proto->state),
*existing_conn);
return reuse_connection(
existing_proto, false,
true, reconnect.connect_seq(), reconnect.msg_seq());
} else {
// acceptor (this connection, the peer) loses
logger().warn("{} server_reconnect: reconnect race detected (cs={})"
" and lose to existing {}, ask client to wait",
conn, reconnect.connect_seq(), *existing_conn);
return send_wait();
}
} else { // existing_proto->connect_seq < reconnect.connect_seq()
logger().warn("{} server_reconnect: stale exsiting connect_seq exist_cs({}) < peer_cs({}),"
" reusing existing {} {}",
conn,
existing_proto->connect_seq,
reconnect.connect_seq(),
get_state_name(existing_proto->state),
*existing_conn);
return reuse_connection(
existing_proto, false,
true, reconnect.connect_seq(), reconnect.msg_seq());
}
});
}
void ProtocolV2::execute_accepting()
{
assert(is_socket_valid);
trigger_state(state_t::ACCEPTING, io_state_t::none);
gate.dispatch_in_background("execute_accepting", conn, [this] {
return seastar::futurize_invoke([this] {
#ifdef UNIT_TESTS_BUILT
if (conn.interceptor) {
// only notify socket accepted
gate.dispatch_in_background(
"test_intercept_socket_accepted", conn, [this] {
return conn.interceptor->intercept(
conn, {Breakpoint{custom_bp_t::SOCKET_ACCEPTED}}
).then([](bp_action_t action) {
ceph_assert(action == bp_action_t::CONTINUE);
});
});
}
#endif
auth_meta = seastar::make_lw_shared<AuthConnectionMeta>();
frame_assembler->reset_handlers();
frame_assembler->start_recording();
return banner_exchange(false);
}).then([this] (auto&& ret) {
auto [_peer_type, _my_addr_from_peer] = std::move(ret);
ceph_assert(conn.get_peer_type() == 0);
conn.set_peer_type(_peer_type);
conn.policy = messenger.get_policy(_peer_type);
logger().info("{} UPDATE: peer_type={},"
" policy(lossy={} server={} standby={} resetcheck={})",
conn, ceph_entity_type_name(_peer_type),
conn.policy.lossy, conn.policy.server,
conn.policy.standby, conn.policy.resetcheck);
if (!messenger.get_myaddr().is_blank_ip() &&
(messenger.get_myaddr().get_port() != _my_addr_from_peer.get_port() ||
messenger.get_myaddr().get_nonce() != _my_addr_from_peer.get_nonce())) {
logger().warn("{} my_addr_from_peer {} port/nonce doesn't match myaddr {}",
conn, _my_addr_from_peer, messenger.get_myaddr());
throw std::system_error(
make_error_code(crimson::net::error::bad_peer_address));
}
messenger.learned_addr(_my_addr_from_peer, conn);
return server_auth();
}).then([this] {
return frame_assembler->read_main_preamble();
}).then([this](auto ret) {
switch (ret.tag) {
case Tag::CLIENT_IDENT:
return server_connect();
case Tag::SESSION_RECONNECT:
return server_reconnect();
default: {
unexpected_tag(ret.tag, conn, "post_server_auth");
return seastar::make_ready_future<next_step_t>(next_step_t::none);
}
}
}).then([this] (next_step_t next) {
switch (next) {
case next_step_t::ready:
assert(state != state_t::ACCEPTING);
break;
case next_step_t::wait:
if (unlikely(state != state_t::ACCEPTING)) {
logger().debug("{} triggered {} at the end of execute_accepting()",
conn, get_state_name(state));
abort_protocol();
}
logger().info("{} execute_accepting(): going to SERVER_WAIT", conn);
execute_server_wait();
break;
default:
ceph_abort("impossible next step");
}
}).handle_exception([this](std::exception_ptr eptr) {
const char *e_what;
try {
std::rethrow_exception(eptr);
} catch (std::exception &e) {
e_what = e.what();
}
logger().info("{} execute_accepting(): fault at {}, going to CLOSING -- {}",
conn, get_state_name(state), e_what);
do_close(false);
});
});
}
// CONNECTING or ACCEPTING state
seastar::future<> ProtocolV2::finish_auth()
{
ceph_assert(auth_meta);
auto records = frame_assembler->stop_recording();
const auto sig = auth_meta->session_key.empty() ? sha256_digest_t() :
auth_meta->session_key.hmac_sha256(nullptr, records.rxbuf);
auto sig_frame = AuthSignatureFrame::Encode(sig);
logger().debug("{} WRITE AuthSignatureFrame: signature={}", conn, sig);
return frame_assembler->write_flush_frame(sig_frame
).then([this] {
return frame_assembler->read_main_preamble();
}).then([this](auto ret) {
expect_tag(Tag::AUTH_SIGNATURE, ret.tag, conn, "post_finish_auth");
return frame_assembler->read_frame_payload();
}).then([this, txbuf=std::move(records.txbuf)](auto payload) {
// handle_auth_signature() logic
auto sig_frame = AuthSignatureFrame::Decode(payload->back());
logger().debug("{} GOT AuthSignatureFrame: signature={}", conn, sig_frame.signature());
const auto actual_tx_sig = auth_meta->session_key.empty() ?
sha256_digest_t() : auth_meta->session_key.hmac_sha256(nullptr, txbuf);
if (sig_frame.signature() != actual_tx_sig) {
logger().warn("{} pre-auth signature mismatch actual_tx_sig={}"
" sig_frame.signature()={}",
conn, actual_tx_sig, sig_frame.signature());
abort_in_fault();
}
});
}
// ESTABLISHING
void ProtocolV2::execute_establishing(SocketConnectionRef existing_conn) {
auto accept_me = [this] {
messenger.register_conn(
seastar::static_pointer_cast<SocketConnection>(
conn.shared_from_this()));
messenger.unaccept_conn(
seastar::static_pointer_cast<SocketConnection>(
conn.shared_from_this()));
};
ceph_assert_always(is_socket_valid);
trigger_state(state_t::ESTABLISHING, io_state_t::delay);
bool is_replace;
if (existing_conn) {
logger().info("{} start establishing: gs={}, pgs={}, cs={}, "
"client_cookie={}, server_cookie={}, {}, new_sid={}, "
"close existing {}",
conn, global_seq, peer_global_seq, connect_seq,
client_cookie, server_cookie,
io_states, frame_assembler->get_socket_shard_id(),
*existing_conn);
is_replace = true;
ProtocolV2 *existing_proto = dynamic_cast<ProtocolV2*>(
existing_conn->protocol.get());
existing_proto->do_close(
true, // is_dispatch_reset
std::move(accept_me));
if (unlikely(state != state_t::ESTABLISHING)) {
logger().warn("{} triggered {} during execute_establishing(), "
"the accept event will not be delivered!",
conn, get_state_name(state));
abort_protocol();
}
} else {
logger().info("{} start establishing: gs={}, pgs={}, cs={}, "
"client_cookie={}, server_cookie={}, {}, new_sid={}, "
"no existing",
conn, global_seq, peer_global_seq, connect_seq,
client_cookie, server_cookie, io_states,
frame_assembler->get_socket_shard_id());
is_replace = false;
accept_me();
}
gated_execute("execute_establishing", conn, [this, is_replace] {
ceph_assert_always(state == state_t::ESTABLISHING);
// set io_handler to a new shard
auto cc_seq = crosscore.prepare_submit();
// there are 2 hops with dispatch_accept()
crosscore.prepare_submit();
auto new_io_shard = frame_assembler->get_socket_shard_id();
logger().debug("{} send {} IOHandler::dispatch_accept({})",
conn, cc_seq, new_io_shard);
ConnectionFRef conn_fref = seastar::make_foreign(
conn.shared_from_this());
ceph_assert_always(!pr_switch_io_shard.has_value());
pr_switch_io_shard = seastar::shared_promise<>();
return seastar::smp::submit_to(
io_handler.get_shard_id(),
[this, cc_seq, new_io_shard, is_replace,
conn_fref=std::move(conn_fref)]() mutable {
return io_handler.dispatch_accept(
cc_seq, new_io_shard, std::move(conn_fref), is_replace);
}).then([this, new_io_shard] {
ceph_assert_always(io_handler.get_shard_id() == new_io_shard);
pr_switch_io_shard->set_value();
pr_switch_io_shard = std::nullopt;
// user can make changes
if (unlikely(state != state_t::ESTABLISHING)) {
logger().debug("{} triggered {} after dispatch_accept() during execute_establishing()",
conn, get_state_name(state));
abort_protocol();
}
return send_server_ident();
}).then([this] {
if (unlikely(state != state_t::ESTABLISHING)) {
logger().debug("{} triggered {} at the end of execute_establishing()",
conn, get_state_name(state));
abort_protocol();
}
logger().info("{} established, going to ready", conn);
execute_ready();
}).handle_exception([this](std::exception_ptr eptr) {
fault(state_t::ESTABLISHING, "execute_establishing", eptr);
});
});
}
// ESTABLISHING or REPLACING state
seastar::future<>
ProtocolV2::send_server_ident()
{
ceph_assert_always(state == state_t::ESTABLISHING ||
state == state_t::REPLACING);
// send_server_ident() logic
// refered to async-conn v2: not assign gs to global_seq
global_seq = messenger.get_global_seq();
auto cc_seq = crosscore.prepare_submit();
logger().debug("{} UPDATE: gs={} for server ident, "
"send {} IOHandler::reset_peer_state()",
conn, global_seq, cc_seq);
// this is required for the case when this connection is being replaced
io_states.reset_peer_state();
gate.dispatch_in_background(
"reset_peer_state", conn, [this, cc_seq] {
return seastar::smp::submit_to(
io_handler.get_shard_id(), [this, cc_seq] {
return io_handler.reset_peer_state(cc_seq);
});
});
if (!conn.policy.lossy) {
server_cookie = ceph::util::generate_random_number<uint64_t>(1, -1ll);
}
uint64_t flags = 0;
if (conn.policy.lossy) {
flags = flags | CEPH_MSG_CONNECT_LOSSY;
}
auto server_ident = ServerIdentFrame::Encode(
messenger.get_myaddrs(),
messenger.get_myname().num(),
global_seq,
conn.policy.features_supported,
conn.policy.features_required | msgr2_required,
flags,
server_cookie);
logger().debug("{} WRITE ServerIdentFrame: addrs={}, gid={},"
" gs={}, features_supported={}, features_required={},"
" flags={}, cookie={}",
conn, messenger.get_myaddrs(), messenger.get_myname().num(),
global_seq, conn.policy.features_supported,
conn.policy.features_required | msgr2_required,
flags, server_cookie);
return frame_assembler->write_flush_frame(server_ident);
}
// REPLACING state
void ProtocolV2::trigger_replacing(bool reconnect,
bool do_reset,
FrameAssemblerV2::mover_t &&mover,
AuthConnectionMetaRef&& new_auth_meta,
uint64_t new_peer_global_seq,
uint64_t new_client_cookie,
entity_name_t new_peer_name,
uint64_t new_conn_features,
uint64_t new_peer_supported_features,
uint64_t new_connect_seq,
uint64_t new_msg_seq)
{
ceph_assert_always(state >= state_t::ESTABLISHING);
ceph_assert_always(state <= state_t::WAIT);
ceph_assert_always(has_socket || state == state_t::CONNECTING);
// mover.socket shouldn't be shutdown
logger().info("{} start replacing ({}): pgs was {}, cs was {}, "
"client_cookie was {}, {}, new_sid={}",
conn, reconnect ? "reconnected" : "connected",
peer_global_seq, connect_seq, client_cookie,
io_states, mover.socket->get_shard_id());
if (is_socket_valid) {
frame_assembler->shutdown_socket<true>(&gate);
is_socket_valid = false;
}
trigger_state_phase1(state_t::REPLACING);
gate.dispatch_in_background(
"trigger_replacing",
conn,
[this,
reconnect,
do_reset,
mover = std::move(mover),
new_auth_meta = std::move(new_auth_meta),
new_client_cookie, new_peer_name,
new_conn_features, new_peer_supported_features,
new_peer_global_seq,
new_connect_seq, new_msg_seq] () mutable {
ceph_assert_always(state == state_t::REPLACING);
auto new_io_shard = mover.socket->get_shard_id();
// state may become CLOSING below, but we cannot abort the chain until
// mover.socket is correctly handled (closed or replaced).
// this is preemptive
return wait_switch_io_shard(
).then([this] {
if (unlikely(state != state_t::REPLACING)) {
ceph_assert_always(state == state_t::CLOSING);
return seastar::now();
}
trigger_state_phase2(state_t::REPLACING, io_state_t::delay);
return wait_exit_io();
}).then([this] {
if (unlikely(state != state_t::REPLACING)) {
ceph_assert_always(state == state_t::CLOSING);
return seastar::now();
}
ceph_assert_always(frame_assembler);
protocol_timer.cancel();
auto done = std::move(execution_done);
execution_done = seastar::now();
return done;
}).then([this, new_io_shard] {
if (unlikely(state != state_t::REPLACING)) {
ceph_assert_always(state == state_t::CLOSING);
return seastar::now();
}
// set io_handler to a new shard
// we should prevent parallel switching core attemps
auto cc_seq = crosscore.prepare_submit();
// there are 2 hops with dispatch_accept()
crosscore.prepare_submit();
logger().debug("{} send {} IOHandler::dispatch_accept({})",
conn, cc_seq, new_io_shard);
ConnectionFRef conn_fref = seastar::make_foreign(
conn.shared_from_this());
ceph_assert_always(!pr_switch_io_shard.has_value());
pr_switch_io_shard = seastar::shared_promise<>();
return seastar::smp::submit_to(
io_handler.get_shard_id(),
[this, cc_seq, new_io_shard,
conn_fref=std::move(conn_fref)]() mutable {
return io_handler.dispatch_accept(
cc_seq, new_io_shard, std::move(conn_fref), false);
}).then([this, new_io_shard] {
ceph_assert_always(io_handler.get_shard_id() == new_io_shard);
pr_switch_io_shard->set_value();
pr_switch_io_shard = std::nullopt;
// user can make changes
});
}).then([this,
reconnect,
do_reset,
mover = std::move(mover),
new_auth_meta = std::move(new_auth_meta),
new_client_cookie, new_peer_name,
new_conn_features, new_peer_supported_features,
new_peer_global_seq,
new_connect_seq, new_msg_seq] () mutable {
if (state == state_t::REPLACING && do_reset) {
reset_session(true);
// user can make changes
}
if (unlikely(state != state_t::REPLACING)) {
logger().debug("{} triggered {} in the middle of trigger_replacing(), abort",
conn, get_state_name(state));
ceph_assert_always(state == state_t::CLOSING);
return mover.socket->close(
).then([sock = std::move(mover.socket)] {
abort_protocol();
});
}
auth_meta = std::move(new_auth_meta);
peer_global_seq = new_peer_global_seq;
gate.dispatch_in_background(
"replace_frame_assembler",
conn,
[this, mover=std::move(mover)]() mutable {
return frame_assembler->replace_by(std::move(mover));
}
);
is_socket_valid = true;
has_socket = true;
if (reconnect) {
connect_seq = new_connect_seq;
// send_reconnect_ok() logic
auto cc_seq = crosscore.prepare_submit();
logger().debug("{} send {} IOHandler::requeue_out_sent_up_to({})",
conn, cc_seq, new_msg_seq);
io_states.requeue_out_sent_up_to();
gate.dispatch_in_background(
"requeue_out_replacing", conn, [this, cc_seq, new_msg_seq] {
return seastar::smp::submit_to(
io_handler.get_shard_id(), [this, cc_seq, new_msg_seq] {
return io_handler.requeue_out_sent_up_to(cc_seq, new_msg_seq);
});
});
auto reconnect_ok = ReconnectOkFrame::Encode(io_states.in_seq);
logger().debug("{} WRITE ReconnectOkFrame: msg_seq={}", conn, io_states.in_seq);
return frame_assembler->write_flush_frame(reconnect_ok);
} else {
client_cookie = new_client_cookie;
assert(conn.get_peer_type() == new_peer_name.type());
if (conn.get_peer_id() == entity_name_t::NEW) {
conn.set_peer_id(new_peer_name.num());
}
conn.set_features(new_conn_features);
peer_supported_features = new_peer_supported_features;
bool is_rev1 = HAVE_MSGR2_FEATURE(peer_supported_features, REVISION_1);
frame_assembler->set_is_rev1(is_rev1);
return send_server_ident();
}
}).then([this, reconnect] {
if (unlikely(state != state_t::REPLACING)) {
logger().debug("{} triggered {} at the end of trigger_replacing(), abort",
conn, get_state_name(state));
ceph_assert_always(state == state_t::CLOSING);
abort_protocol();
}
logger().info("{} replaced ({}), going to ready: "
"gs={}, pgs={}, cs={}, "
"client_cookie={}, server_cookie={}, {}",
conn, reconnect ? "reconnected" : "connected",
global_seq, peer_global_seq, connect_seq,
client_cookie, server_cookie, io_states);
execute_ready();
}).handle_exception([this](std::exception_ptr eptr) {
fault(state_t::REPLACING, "trigger_replacing", eptr);
});
});
}
// READY state
seastar::future<> ProtocolV2::notify_out_fault(
crosscore_t::seq_t cc_seq,
const char *where,
std::exception_ptr eptr,
io_handler_state _io_states)
{
assert(seastar::this_shard_id() == conn.get_messenger_shard_id());
if (!crosscore.proceed_or_wait(cc_seq)) {
logger().debug("{} got {} notify_out_fault(), wait at {}",
conn, cc_seq, crosscore.get_in_seq());
return crosscore.wait(cc_seq
).then([this, cc_seq, where, eptr, _io_states] {
return notify_out_fault(cc_seq, where, eptr, _io_states);
});
}
io_states = _io_states;
logger().debug("{} got {} notify_out_fault(): io_states={}",
conn, cc_seq, io_states);
fault(state_t::READY, where, eptr);
return seastar::now();
}
void ProtocolV2::execute_ready()
{
assert(conn.policy.lossy || (client_cookie != 0 && server_cookie != 0));
protocol_timer.cancel();
ceph_assert_always(is_socket_valid);
// I'm not responsible to shutdown the socket at READY
is_socket_valid = false;
trigger_state(state_t::READY, io_state_t::open);
#ifdef UNIT_TESTS_BUILT
if (conn.interceptor) {
// FIXME: doesn't support cross-core
conn.interceptor->register_conn_ready(
conn.get_local_shared_foreign_from_this());
}
#endif
}
// STANDBY state
void ProtocolV2::execute_standby()
{
ceph_assert_always(!is_socket_valid);
trigger_state(state_t::STANDBY, io_state_t::delay);
}
seastar::future<> ProtocolV2::notify_out(
crosscore_t::seq_t cc_seq)
{
assert(seastar::this_shard_id() == conn.get_messenger_shard_id());
if (!crosscore.proceed_or_wait(cc_seq)) {
logger().debug("{} got {} notify_out(), wait at {}",
conn, cc_seq, crosscore.get_in_seq());
return crosscore.wait(cc_seq
).then([this, cc_seq] {
return notify_out(cc_seq);
});
}
logger().debug("{} got {} notify_out(): at {}",
conn, cc_seq, get_state_name(state));
io_states.is_out_queued = true;
if (unlikely(state == state_t::STANDBY && !conn.policy.server)) {
logger().info("{} notify_out(): at {}, going to CONNECTING",
conn, get_state_name(state));
execute_connecting();
}
return seastar::now();
}
// WAIT state
void ProtocolV2::execute_wait(bool max_backoff)
{
ceph_assert_always(!is_socket_valid);
trigger_state(state_t::WAIT, io_state_t::delay);
gated_execute("execute_wait", conn, [this, max_backoff] {
double backoff = protocol_timer.last_dur();
if (max_backoff) {
backoff = local_conf().get_val<double>("ms_max_backoff");
} else if (backoff > 0) {
backoff = std::min(local_conf().get_val<double>("ms_max_backoff"), 2 * backoff);
} else {
backoff = local_conf().get_val<double>("ms_initial_backoff");
}
return protocol_timer.backoff(backoff).then([this] {
if (unlikely(state != state_t::WAIT)) {
logger().debug("{} triggered {} at the end of execute_wait()",
conn, get_state_name(state));
abort_protocol();
}
logger().info("{} execute_wait(): going to CONNECTING", conn);
execute_connecting();
}).handle_exception([this](std::exception_ptr eptr) {
const char *e_what;
try {
std::rethrow_exception(eptr);
} catch (std::exception &e) {
e_what = e.what();
}
logger().info("{} execute_wait(): protocol aborted at {} -- {}",
conn, get_state_name(state), e_what);
assert(state == state_t::REPLACING ||
state == state_t::CLOSING);
});
});
}
// SERVER_WAIT state
void ProtocolV2::execute_server_wait()
{
ceph_assert_always(is_socket_valid);
trigger_state(state_t::SERVER_WAIT, io_state_t::none);
gated_execute("execute_server_wait", conn, [this] {
return frame_assembler->read_exactly(1
).then([this](auto bptr) {
logger().warn("{} SERVER_WAIT got read, abort", conn);
abort_in_fault();
}).handle_exception([this](std::exception_ptr eptr) {
const char *e_what;
try {
std::rethrow_exception(eptr);
} catch (std::exception &e) {
e_what = e.what();
}
logger().info("{} execute_server_wait(): fault at {}, going to CLOSING -- {}",
conn, get_state_name(state), e_what);
do_close(false);
});
});
}
// CLOSING state
seastar::future<> ProtocolV2::notify_mark_down(
crosscore_t::seq_t cc_seq)
{
assert(seastar::this_shard_id() == conn.get_messenger_shard_id());
if (!crosscore.proceed_or_wait(cc_seq)) {
logger().debug("{} got {} notify_mark_down(), wait at {}",
conn, cc_seq, crosscore.get_in_seq());
return crosscore.wait(cc_seq
).then([this, cc_seq] {
return notify_mark_down(cc_seq);
});
}
logger().debug("{} got {} notify_mark_down()",
conn, cc_seq);
do_close(false);
return seastar::now();
}
seastar::future<> ProtocolV2::close_clean_yielded()
{
// yield() so that do_close() can be called *after* close_clean_yielded() is
// applied to all connections in a container using
// seastar::parallel_for_each(). otherwise, we could erase a connection in
// the container when seastar::parallel_for_each() is still iterating in it.
// that'd lead to a segfault.
return seastar::yield(
).then([this] {
do_close(false);
return pr_closed_clean.get_shared_future();
// connection may be unreferenced from the messenger,
// so need to hold the additional reference.
}).finally([conn_ref = conn.shared_from_this()] {});;
}
void ProtocolV2::do_close(
bool is_dispatch_reset,
std::optional<std::function<void()>> f_accept_new)
{
if (state == state_t::CLOSING) {
// already closing
return;
}
bool is_replace = f_accept_new ? true : false;
logger().info("{} closing: reset {}, replace {}", conn,
is_dispatch_reset ? "yes" : "no",
is_replace ? "yes" : "no");
/*
* atomic operations
*/
ceph_assert_always(!gate.is_closed());
// messenger registrations, must before user events
messenger.closing_conn(
seastar::static_pointer_cast<SocketConnection>(
conn.shared_from_this()));
if (state == state_t::ACCEPTING || state == state_t::SERVER_WAIT) {
messenger.unaccept_conn(
seastar::static_pointer_cast<SocketConnection>(
conn.shared_from_this()));
} else if (state >= state_t::ESTABLISHING && state < state_t::CLOSING) {
messenger.unregister_conn(
seastar::static_pointer_cast<SocketConnection>(
conn.shared_from_this()));
} else {
// cannot happen
ceph_assert(false);
}
if (f_accept_new) {
// the replacing connection must be registerred after the replaced
// connection is unreigsterred.
(*f_accept_new)();
}
protocol_timer.cancel();
if (is_socket_valid) {
frame_assembler->shutdown_socket<true>(&gate);
is_socket_valid = false;
}
trigger_state_phase1(state_t::CLOSING);
gate.dispatch_in_background(
"close_io", conn, [this, is_dispatch_reset, is_replace] {
// this is preemptive
return wait_switch_io_shard(
).then([this, is_dispatch_reset, is_replace] {
trigger_state_phase2(state_t::CLOSING, io_state_t::drop);
auto cc_seq = crosscore.prepare_submit();
logger().debug("{} send {} IOHandler::close_io(reset={}, replace={})",
conn, cc_seq, is_dispatch_reset, is_replace);
std::ignore = gate.close(
).then([this] {
ceph_assert_always(!need_exit_io);
ceph_assert_always(!pr_exit_io.has_value());
if (has_socket) {
ceph_assert_always(frame_assembler);
return frame_assembler->close_shutdown_socket();
} else {
return seastar::now();
}
}).then([this] {
logger().debug("{} closed!", conn);
messenger.closed_conn(
seastar::static_pointer_cast<SocketConnection>(
conn.shared_from_this()));
pr_closed_clean.set_value();
#ifdef UNIT_TESTS_BUILT
closed_clean = true;
if (conn.interceptor) {
conn.interceptor->register_conn_closed(
conn.get_local_shared_foreign_from_this());
}
#endif
// connection is unreferenced from the messenger,
// so need to hold the additional reference.
}).handle_exception([conn_ref = conn.shared_from_this(), this] (auto eptr) {
logger().error("{} closing got unexpected exception {}",
conn, eptr);
ceph_abort();
});
return seastar::smp::submit_to(
io_handler.get_shard_id(),
[this, cc_seq, is_dispatch_reset, is_replace] {
return io_handler.close_io(cc_seq, is_dispatch_reset, is_replace);
});
// user can make changes
});
});
}
} // namespace crimson::net
|