-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibfabintercept.cpp
More file actions
1105 lines (1027 loc) · 35 KB
/
Copy pathlibfabintercept.cpp
File metadata and controls
1105 lines (1027 loc) · 35 KB
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
/*
* libfab-intercepter — implementation.
*
* Interposes the exported setup symbols (fi_getinfo / fi_fabric / fi_fabric2),
* walks the fabric -> domain -> {endpoint, cq} object graph, and patches each
* object's ops table in place so selected data-path slots dispatch through
* consumer hooks. See libfabintercept.h for the model and the contract for
* adding a new op.
*
* Only slots a consumer hooked are overridden; the object handed back to the
* app is always the provider's real fid. Setup-path work (cloning tables,
* re-patching on enable) is off the hot path; the only per-call cost on a
* hooked op is one indirect call through the shim plus the wrap.
*/
/* Required on glibc for RTLD_NEXT; must precede any system header. */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "libfabintercept.h"
#include <rdma/fi_errno.h>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <dlfcn.h>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace
{
// The registered hook table (a copy) and whether anything is registered.
// Until lfi_register() runs, g_registered is false and the interposed
// entry points forward straight to the provider.
static struct lfi_hooks g_hooks;
static bool g_registered = false;
// Setup-path tracing, off unless LFI_TRACE=1. Never on the hot path.
static bool g_trace = false;
static void trace(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
static void trace(const char *fmt, ...)
{
if (!g_trace)
return;
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "[libfabintercept pid=%d] ", (int)getpid());
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
fflush(stderr);
va_end(ap);
}
// Which object families a consumer actually asked for. Used to skip
// patching objects nobody hooked (e.g. don't touch CQs for a send-only
// consumer).
static bool want_ep()
{
return g_hooks.on_ep_create || g_hooks.on_ep_close ||
g_hooks.on_ep_bind || g_hooks.on_ep_info ||
g_hooks.rma_read ||
g_hooks.rma_readv ||
g_hooks.rma_readmsg || g_hooks.rma_write ||
g_hooks.rma_writev || g_hooks.rma_writemsg ||
g_hooks.rma_inject || g_hooks.rma_writedata ||
g_hooks.rma_injectdata || g_hooks.msg_recv ||
g_hooks.msg_recvv || g_hooks.msg_recvmsg ||
g_hooks.msg_send || g_hooks.msg_sendv ||
g_hooks.msg_sendmsg || g_hooks.msg_inject ||
g_hooks.msg_senddata || g_hooks.msg_injectdata ||
g_hooks.tagged_recv || g_hooks.tagged_recvv ||
g_hooks.tagged_recvmsg || g_hooks.tagged_send ||
g_hooks.tagged_sendv || g_hooks.tagged_sendmsg ||
g_hooks.tagged_inject || g_hooks.tagged_senddata ||
g_hooks.tagged_injectdata;
}
static bool want_cq()
{
return g_hooks.on_cq_create || g_hooks.on_cq_close ||
g_hooks.cq_open || g_hooks.cq_read ||
g_hooks.cq_readfrom || g_hooks.cq_readerr ||
g_hooks.cq_sread || g_hooks.cq_sreadfrom;
}
// ---- Generic ops-table patching --------------------------------------
//
// Each fid family's ops table begins with a size_t `size`; we clone the
// provider's real layout into a zeroed buffer at least as large as our
// own view of the struct, so overriding fields known to our headers is
// always in bounds even if the provider table is older/newer.
template <typename T> static T *clone_ops(const T *orig)
{
size_t sz = orig->size;
size_t alloc = sz < sizeof(T) ? sizeof(T) : sz;
T *copy = static_cast<T *>(calloc(1, alloc));
if (copy)
memcpy(copy, orig, sz);
return copy;
}
// True if the provider table advertises the member (offset within its
// size) and the member is non-null.
#define HAS_OP(orig, type, member) \
((orig)->size > offsetof(type, member) && (orig)->member != nullptr)
template <typename T> struct patched_ops {
const T *orig;
T *spec;
};
static std::vector<patched_ops<struct fi_ops_rma>> g_rma_ops;
static std::vector<patched_ops<struct fi_ops_msg>> g_msg_ops;
static std::vector<patched_ops<struct fi_ops_tagged>> g_tagged_ops;
static std::vector<patched_ops<struct fi_ops_cq>> g_cq_ops;
template <typename T>
static T *existing_spec_for(const std::vector<patched_ops<T>> &ops,
const T *table)
{
for (const auto &p : ops)
if (p.spec == table || p.orig == table)
return p.spec;
return nullptr;
}
template <typename T>
static const T *orig_for_spec(const std::vector<patched_ops<T>> &ops,
const T *spec)
{
for (const auto &p : ops)
if (p.spec == spec)
return p.orig;
return nullptr;
}
static const struct fi_ops_rma *orig_rma_for(const struct fid_ep *ep)
{
return ep ? orig_for_spec(g_rma_ops, ep->rma) : nullptr;
}
static const struct fi_ops_msg *orig_msg_for(const struct fid_ep *ep)
{
return ep ? orig_for_spec(g_msg_ops, ep->msg) : nullptr;
}
static const struct fi_ops_tagged *
orig_tagged_for(const struct fid_ep *ep)
{
return ep ? orig_for_spec(g_tagged_ops, ep->tagged) : nullptr;
}
static const struct fi_ops_cq *orig_cq_for(const struct fid_cq *cq)
{
return cq ? orig_for_spec(g_cq_ops, cq->ops) : nullptr;
}
// ---- Shims: forward through the consumer hook, else call provider ----
//
// Each shim has the provider op's exact signature. It resolves the
// provider op for this object, then: if the consumer registered a hook,
// call it with (args..., provider_op, user); else call the provider op
// directly. A shim is only installed on a table when its hook is set
// (see the patched_*_ops installers), so the hook check is belt-and-
// suspenders, not a per-op gate on an unhooked call.
#define FWD_OR_HOOK(orig, member, hook, ...) \
do { \
if (!orig || !orig->member) \
return -FI_EINVAL; \
if (g_hooks.hook) \
return g_hooks.hook(__VA_ARGS__, orig->member, \
g_hooks.user); \
return orig->member(__VA_ARGS__); \
} while (0)
// RMA -----------------------------------------------------------------
static ssize_t sh_rma_read(struct fid_ep *ep, void *buf, size_t len,
void *desc, fi_addr_t src_addr, uint64_t addr,
uint64_t key, void *context)
{
const struct fi_ops_rma *o = orig_rma_for(ep);
FWD_OR_HOOK(o, read, rma_read, ep, buf, len, desc, src_addr,
addr, key, context);
}
static ssize_t sh_rma_readv(struct fid_ep *ep, const struct iovec *iov,
void **desc, size_t count,
fi_addr_t src_addr, uint64_t addr,
uint64_t key, void *context)
{
const struct fi_ops_rma *o = orig_rma_for(ep);
FWD_OR_HOOK(o, readv, rma_readv, ep, iov, desc, count, src_addr,
addr, key, context);
}
static ssize_t sh_rma_readmsg(struct fid_ep *ep,
const struct fi_msg_rma *msg,
uint64_t flags)
{
const struct fi_ops_rma *o = orig_rma_for(ep);
FWD_OR_HOOK(o, readmsg, rma_readmsg, ep, msg, flags);
}
static ssize_t sh_rma_write(struct fid_ep *ep, const void *buf,
size_t len, void *desc, fi_addr_t dest_addr,
uint64_t addr, uint64_t key, void *context)
{
const struct fi_ops_rma *o = orig_rma_for(ep);
FWD_OR_HOOK(o, write, rma_write, ep, buf, len, desc, dest_addr,
addr, key, context);
}
static ssize_t sh_rma_writev(struct fid_ep *ep, const struct iovec *iov,
void **desc, size_t count,
fi_addr_t dest_addr, uint64_t addr,
uint64_t key, void *context)
{
const struct fi_ops_rma *o = orig_rma_for(ep);
FWD_OR_HOOK(o, writev, rma_writev, ep, iov, desc, count,
dest_addr, addr, key, context);
}
static ssize_t sh_rma_writemsg(struct fid_ep *ep,
const struct fi_msg_rma *msg,
uint64_t flags)
{
const struct fi_ops_rma *o = orig_rma_for(ep);
FWD_OR_HOOK(o, writemsg, rma_writemsg, ep, msg, flags);
}
static ssize_t sh_rma_inject(struct fid_ep *ep, const void *buf,
size_t len, fi_addr_t dest_addr,
uint64_t addr, uint64_t key)
{
const struct fi_ops_rma *o = orig_rma_for(ep);
FWD_OR_HOOK(o, inject, rma_inject, ep, buf, len, dest_addr, addr,
key);
}
static ssize_t sh_rma_writedata(struct fid_ep *ep, const void *buf,
size_t len, void *desc, uint64_t data,
fi_addr_t dest_addr, uint64_t addr,
uint64_t key, void *context)
{
const struct fi_ops_rma *o = orig_rma_for(ep);
FWD_OR_HOOK(o, writedata, rma_writedata, ep, buf, len, desc,
data, dest_addr, addr, key, context);
}
static ssize_t sh_rma_injectdata(struct fid_ep *ep, const void *buf,
size_t len, uint64_t data,
fi_addr_t dest_addr, uint64_t addr,
uint64_t key)
{
const struct fi_ops_rma *o = orig_rma_for(ep);
FWD_OR_HOOK(o, injectdata, rma_injectdata, ep, buf, len, data,
dest_addr, addr, key);
}
// MSG -----------------------------------------------------------------
static ssize_t sh_msg_recv(struct fid_ep *ep, void *buf, size_t len,
void *desc, fi_addr_t src_addr, void *context)
{
const struct fi_ops_msg *o = orig_msg_for(ep);
FWD_OR_HOOK(o, recv, msg_recv, ep, buf, len, desc, src_addr,
context);
}
static ssize_t sh_msg_recvv(struct fid_ep *ep, const struct iovec *iov,
void **desc, size_t count,
fi_addr_t src_addr, void *context)
{
const struct fi_ops_msg *o = orig_msg_for(ep);
FWD_OR_HOOK(o, recvv, msg_recvv, ep, iov, desc, count, src_addr,
context);
}
static ssize_t sh_msg_recvmsg(struct fid_ep *ep, const struct fi_msg *msg,
uint64_t flags)
{
const struct fi_ops_msg *o = orig_msg_for(ep);
FWD_OR_HOOK(o, recvmsg, msg_recvmsg, ep, msg, flags);
}
static ssize_t sh_msg_send(struct fid_ep *ep, const void *buf, size_t len,
void *desc, fi_addr_t dest_addr, void *context)
{
const struct fi_ops_msg *o = orig_msg_for(ep);
FWD_OR_HOOK(o, send, msg_send, ep, buf, len, desc, dest_addr,
context);
}
static ssize_t sh_msg_sendv(struct fid_ep *ep, const struct iovec *iov,
void **desc, size_t count,
fi_addr_t dest_addr, void *context)
{
const struct fi_ops_msg *o = orig_msg_for(ep);
FWD_OR_HOOK(o, sendv, msg_sendv, ep, iov, desc, count, dest_addr,
context);
}
static ssize_t sh_msg_sendmsg(struct fid_ep *ep, const struct fi_msg *msg,
uint64_t flags)
{
const struct fi_ops_msg *o = orig_msg_for(ep);
FWD_OR_HOOK(o, sendmsg, msg_sendmsg, ep, msg, flags);
}
static ssize_t sh_msg_inject(struct fid_ep *ep, const void *buf,
size_t len, fi_addr_t dest_addr)
{
const struct fi_ops_msg *o = orig_msg_for(ep);
FWD_OR_HOOK(o, inject, msg_inject, ep, buf, len, dest_addr);
}
static ssize_t sh_msg_senddata(struct fid_ep *ep, const void *buf,
size_t len, void *desc, uint64_t data,
fi_addr_t dest_addr, void *context)
{
const struct fi_ops_msg *o = orig_msg_for(ep);
FWD_OR_HOOK(o, senddata, msg_senddata, ep, buf, len, desc, data,
dest_addr, context);
}
static ssize_t sh_msg_injectdata(struct fid_ep *ep, const void *buf,
size_t len, uint64_t data,
fi_addr_t dest_addr)
{
const struct fi_ops_msg *o = orig_msg_for(ep);
FWD_OR_HOOK(o, injectdata, msg_injectdata, ep, buf, len, data,
dest_addr);
}
// TAGGED --------------------------------------------------------------
static ssize_t sh_tagged_recv(struct fid_ep *ep, void *buf, size_t len,
void *desc, fi_addr_t src_addr,
uint64_t tag, uint64_t ignore,
void *context)
{
const struct fi_ops_tagged *o = orig_tagged_for(ep);
FWD_OR_HOOK(o, recv, tagged_recv, ep, buf, len, desc, src_addr,
tag, ignore, context);
}
static ssize_t sh_tagged_recvv(struct fid_ep *ep, const struct iovec *iov,
void **desc, size_t count,
fi_addr_t src_addr, uint64_t tag,
uint64_t ignore, void *context)
{
const struct fi_ops_tagged *o = orig_tagged_for(ep);
FWD_OR_HOOK(o, recvv, tagged_recvv, ep, iov, desc, count,
src_addr, tag, ignore, context);
}
static ssize_t sh_tagged_recvmsg(struct fid_ep *ep,
const struct fi_msg_tagged *msg,
uint64_t flags)
{
const struct fi_ops_tagged *o = orig_tagged_for(ep);
FWD_OR_HOOK(o, recvmsg, tagged_recvmsg, ep, msg, flags);
}
static ssize_t sh_tagged_send(struct fid_ep *ep, const void *buf,
size_t len, void *desc,
fi_addr_t dest_addr, uint64_t tag,
void *context)
{
const struct fi_ops_tagged *o = orig_tagged_for(ep);
FWD_OR_HOOK(o, send, tagged_send, ep, buf, len, desc, dest_addr,
tag, context);
}
static ssize_t sh_tagged_sendv(struct fid_ep *ep, const struct iovec *iov,
void **desc, size_t count,
fi_addr_t dest_addr, uint64_t tag,
void *context)
{
const struct fi_ops_tagged *o = orig_tagged_for(ep);
FWD_OR_HOOK(o, sendv, tagged_sendv, ep, iov, desc, count,
dest_addr, tag, context);
}
static ssize_t sh_tagged_sendmsg(struct fid_ep *ep,
const struct fi_msg_tagged *msg,
uint64_t flags)
{
const struct fi_ops_tagged *o = orig_tagged_for(ep);
FWD_OR_HOOK(o, sendmsg, tagged_sendmsg, ep, msg, flags);
}
static ssize_t sh_tagged_inject(struct fid_ep *ep, const void *buf,
size_t len, fi_addr_t dest_addr,
uint64_t tag)
{
const struct fi_ops_tagged *o = orig_tagged_for(ep);
FWD_OR_HOOK(o, inject, tagged_inject, ep, buf, len, dest_addr,
tag);
}
static ssize_t sh_tagged_senddata(struct fid_ep *ep, const void *buf,
size_t len, void *desc, uint64_t data,
fi_addr_t dest_addr, uint64_t tag,
void *context)
{
const struct fi_ops_tagged *o = orig_tagged_for(ep);
FWD_OR_HOOK(o, senddata, tagged_senddata, ep, buf, len, desc,
data, dest_addr, tag, context);
}
static ssize_t sh_tagged_injectdata(struct fid_ep *ep, const void *buf,
size_t len, uint64_t data,
fi_addr_t dest_addr, uint64_t tag)
{
const struct fi_ops_tagged *o = orig_tagged_for(ep);
FWD_OR_HOOK(o, injectdata, tagged_injectdata, ep, buf, len, data,
dest_addr, tag);
}
// CQ ------------------------------------------------------------------
static ssize_t sh_cq_read(struct fid_cq *cq, void *buf, size_t count)
{
const struct fi_ops_cq *o = orig_cq_for(cq);
FWD_OR_HOOK(o, read, cq_read, cq, buf, count);
}
static ssize_t sh_cq_readfrom(struct fid_cq *cq, void *buf, size_t count,
fi_addr_t *src_addr)
{
const struct fi_ops_cq *o = orig_cq_for(cq);
FWD_OR_HOOK(o, readfrom, cq_readfrom, cq, buf, count, src_addr);
}
static ssize_t sh_cq_readerr(struct fid_cq *cq,
struct fi_cq_err_entry *buf, uint64_t flags)
{
const struct fi_ops_cq *o = orig_cq_for(cq);
FWD_OR_HOOK(o, readerr, cq_readerr, cq, buf, flags);
}
static ssize_t sh_cq_sread(struct fid_cq *cq, void *buf, size_t count,
const void *cond, int timeout)
{
const struct fi_ops_cq *o = orig_cq_for(cq);
FWD_OR_HOOK(o, sread, cq_sread, cq, buf, count, cond, timeout);
}
static ssize_t sh_cq_sreadfrom(struct fid_cq *cq, void *buf, size_t count,
fi_addr_t *src_addr, const void *cond,
int timeout)
{
const struct fi_ops_cq *o = orig_cq_for(cq);
FWD_OR_HOOK(o, sreadfrom, cq_sreadfrom, cq, buf, count, src_addr,
cond, timeout);
}
#undef FWD_OR_HOOK
// ---- ops-table installers --------------------------------------------
//
// Clone the provider table once (cached by orig pointer), override only
// the slots whose hook is registered and whose provider slot exists.
#define INSTALL(orig, spec, type, member, hook, shim) \
do { \
if (g_hooks.hook && HAS_OP(orig, type, member)) \
(spec)->member = shim; \
} while (0)
static struct fi_ops_rma *patched_rma_ops(const struct fi_ops_rma *orig)
{
if (!orig)
return nullptr;
if (struct fi_ops_rma *s = existing_spec_for(g_rma_ops, orig))
return s;
struct fi_ops_rma *spec = clone_ops(orig);
if (!spec)
return nullptr;
INSTALL(orig, spec, struct fi_ops_rma, read, rma_read,
sh_rma_read);
INSTALL(orig, spec, struct fi_ops_rma, readv, rma_readv,
sh_rma_readv);
INSTALL(orig, spec, struct fi_ops_rma, readmsg, rma_readmsg,
sh_rma_readmsg);
INSTALL(orig, spec, struct fi_ops_rma, write, rma_write,
sh_rma_write);
INSTALL(orig, spec, struct fi_ops_rma, writev, rma_writev,
sh_rma_writev);
INSTALL(orig, spec, struct fi_ops_rma, writemsg, rma_writemsg,
sh_rma_writemsg);
INSTALL(orig, spec, struct fi_ops_rma, inject, rma_inject,
sh_rma_inject);
INSTALL(orig, spec, struct fi_ops_rma, writedata, rma_writedata,
sh_rma_writedata);
INSTALL(orig, spec, struct fi_ops_rma, injectdata,
rma_injectdata, sh_rma_injectdata);
g_rma_ops.push_back({orig, spec});
trace("installed rma shims (orig=%p spec=%p)", (void *)orig,
(void *)spec);
return spec;
}
static struct fi_ops_msg *patched_msg_ops(const struct fi_ops_msg *orig)
{
if (!orig)
return nullptr;
if (struct fi_ops_msg *s = existing_spec_for(g_msg_ops, orig))
return s;
struct fi_ops_msg *spec = clone_ops(orig);
if (!spec)
return nullptr;
INSTALL(orig, spec, struct fi_ops_msg, recv, msg_recv,
sh_msg_recv);
INSTALL(orig, spec, struct fi_ops_msg, recvv, msg_recvv,
sh_msg_recvv);
INSTALL(orig, spec, struct fi_ops_msg, recvmsg, msg_recvmsg,
sh_msg_recvmsg);
INSTALL(orig, spec, struct fi_ops_msg, send, msg_send,
sh_msg_send);
INSTALL(orig, spec, struct fi_ops_msg, sendv, msg_sendv,
sh_msg_sendv);
INSTALL(orig, spec, struct fi_ops_msg, sendmsg, msg_sendmsg,
sh_msg_sendmsg);
INSTALL(orig, spec, struct fi_ops_msg, inject, msg_inject,
sh_msg_inject);
INSTALL(orig, spec, struct fi_ops_msg, senddata, msg_senddata,
sh_msg_senddata);
INSTALL(orig, spec, struct fi_ops_msg, injectdata,
msg_injectdata, sh_msg_injectdata);
g_msg_ops.push_back({orig, spec});
trace("installed msg shims (orig=%p spec=%p)", (void *)orig,
(void *)spec);
return spec;
}
static struct fi_ops_tagged *
patched_tagged_ops(const struct fi_ops_tagged *orig)
{
if (!orig)
return nullptr;
if (struct fi_ops_tagged *s =
existing_spec_for(g_tagged_ops, orig))
return s;
struct fi_ops_tagged *spec = clone_ops(orig);
if (!spec)
return nullptr;
INSTALL(orig, spec, struct fi_ops_tagged, recv, tagged_recv,
sh_tagged_recv);
INSTALL(orig, spec, struct fi_ops_tagged, recvv, tagged_recvv,
sh_tagged_recvv);
INSTALL(orig, spec, struct fi_ops_tagged, recvmsg,
tagged_recvmsg, sh_tagged_recvmsg);
INSTALL(orig, spec, struct fi_ops_tagged, send, tagged_send,
sh_tagged_send);
INSTALL(orig, spec, struct fi_ops_tagged, sendv, tagged_sendv,
sh_tagged_sendv);
INSTALL(orig, spec, struct fi_ops_tagged, sendmsg,
tagged_sendmsg, sh_tagged_sendmsg);
INSTALL(orig, spec, struct fi_ops_tagged, inject, tagged_inject,
sh_tagged_inject);
INSTALL(orig, spec, struct fi_ops_tagged, senddata,
tagged_senddata, sh_tagged_senddata);
INSTALL(orig, spec, struct fi_ops_tagged, injectdata,
tagged_injectdata, sh_tagged_injectdata);
g_tagged_ops.push_back({orig, spec});
trace("installed tagged shims (orig=%p spec=%p)", (void *)orig,
(void *)spec);
return spec;
}
static struct fi_ops_cq *patched_cq_ops(const struct fi_ops_cq *orig)
{
if (!orig)
return nullptr;
if (struct fi_ops_cq *s = existing_spec_for(g_cq_ops, orig))
return s;
struct fi_ops_cq *spec = clone_ops(orig);
if (!spec)
return nullptr;
INSTALL(orig, spec, struct fi_ops_cq, read, cq_read, sh_cq_read);
INSTALL(orig, spec, struct fi_ops_cq, readfrom, cq_readfrom,
sh_cq_readfrom);
INSTALL(orig, spec, struct fi_ops_cq, readerr, cq_readerr,
sh_cq_readerr);
INSTALL(orig, spec, struct fi_ops_cq, sread, cq_sread,
sh_cq_sread);
INSTALL(orig, spec, struct fi_ops_cq, sreadfrom, cq_sreadfrom,
sh_cq_sreadfrom);
g_cq_ops.push_back({orig, spec});
trace("installed cq shims (orig=%p spec=%p)", (void *)orig,
(void *)spec);
return spec;
}
#undef INSTALL
// ---- Endpoint patching -----------------------------------------------
//
// The data-op tables may be null at fi_endpoint() time and only get
// populated by the provider at fi_enable(); re-apply the swaps after a
// successful bind/control/setopt so we don't miss them. fid.ops and the
// fi_ops_ep table are assumed shared across endpoints of a provider, so
// they are cloned once into single globals.
static const struct fi_ops *g_orig_ep_fid_ops = nullptr;
static const struct fi_ops_ep *g_orig_ep_ops = nullptr;
static struct fi_ops *g_spec_ep_fid_ops = nullptr;
static struct fi_ops_ep *g_spec_ep_ops = nullptr;
static void patch_endpoint(struct fid_ep *ep);
static int ep_fid_bind(struct fid *fid, struct fid *bfid, uint64_t flags)
{
int ret = g_orig_ep_fid_ops->bind(fid, bfid, flags);
if (!ret) {
struct fid_ep *ep =
reinterpret_cast<struct fid_ep *>(fid);
if (g_hooks.on_ep_bind)
g_hooks.on_ep_bind(ep, bfid, flags,
g_hooks.user);
patch_endpoint(ep);
}
return ret;
}
static int ep_fid_control(struct fid *fid, int command, void *arg)
{
int ret = g_orig_ep_fid_ops->control(fid, command, arg);
if (!ret)
patch_endpoint(reinterpret_cast<struct fid_ep *>(fid));
return ret;
}
// Notify the consumer before the provider tears the endpoint down, while
// it (and its domain) are still valid, then close for real.
static int ep_fid_close(struct fid *fid)
{
if (g_hooks.on_ep_close)
g_hooks.on_ep_close(
reinterpret_cast<struct fid_ep *>(fid),
g_hooks.user);
return g_orig_ep_fid_ops->close(fid);
}
static int ep_setopt(fid_t fid, int level, int optname,
const void *optval, size_t optlen)
{
int ret = g_orig_ep_ops->setopt(fid, level, optname, optval,
optlen);
if (!ret)
patch_endpoint(reinterpret_cast<struct fid_ep *>(fid));
return ret;
}
// ep -> domain, so on_ep_create can hand the consumer the owning domain.
static std::unordered_map<const void *, struct fid_domain *> g_ep_domain;
static std::unordered_set<const void *> g_ep_announced;
static void announce_ep(struct fid_ep *ep, struct fid_domain *dom)
{
if (ep && dom)
g_ep_domain[ep] = dom;
if (!g_hooks.on_ep_create || !ep)
return;
if (!g_ep_announced.insert(ep).second)
return; // once per ep
auto it = g_ep_domain.find(ep);
g_hooks.on_ep_create(
ep, it != g_ep_domain.end() ? it->second : nullptr,
g_hooks.user);
}
static int ep_tx_ctx(struct fid_ep *sep, int index,
struct fi_tx_attr *attr, struct fid_ep **tx_ep,
void *context)
{
int ret = g_orig_ep_ops->tx_ctx(sep, index, attr, tx_ep,
context);
if (!ret && tx_ep && *tx_ep) {
patch_endpoint(*tx_ep);
auto it = g_ep_domain.find(sep);
announce_ep(*tx_ep, it != g_ep_domain.end() ? it->second
: nullptr);
}
return ret;
}
static int ep_rx_ctx(struct fid_ep *sep, int index,
struct fi_rx_attr *attr, struct fid_ep **rx_ep,
void *context)
{
int ret = g_orig_ep_ops->rx_ctx(sep, index, attr, rx_ep,
context);
if (!ret && rx_ep && *rx_ep) {
patch_endpoint(*rx_ep);
auto it = g_ep_domain.find(sep);
announce_ep(*rx_ep, it != g_ep_domain.end() ? it->second
: nullptr);
}
return ret;
}
static void patch_endpoint(struct fid_ep *ep)
{
trace("patch_endpoint ep=%p rma=%p msg=%p tagged=%p", (void *)ep,
(void *)ep->rma, (void *)ep->msg, (void *)ep->tagged);
if (ep->rma) {
struct fi_ops_rma *s = patched_rma_ops(ep->rma);
if (s)
ep->rma = s;
}
if (ep->msg) {
struct fi_ops_msg *s = patched_msg_ops(ep->msg);
if (s)
ep->msg = s;
}
if (ep->tagged) {
struct fi_ops_tagged *s = patched_tagged_ops(ep->tagged);
if (s)
ep->tagged = s;
}
if (ep->fid.ops) {
if (!g_spec_ep_fid_ops) {
g_orig_ep_fid_ops = ep->fid.ops;
g_spec_ep_fid_ops = clone_ops(ep->fid.ops);
if (g_spec_ep_fid_ops) {
if (HAS_OP(g_orig_ep_fid_ops,
struct fi_ops, bind))
g_spec_ep_fid_ops->bind =
ep_fid_bind;
if (HAS_OP(g_orig_ep_fid_ops,
struct fi_ops, close))
g_spec_ep_fid_ops->close =
ep_fid_close;
g_spec_ep_fid_ops->control =
ep_fid_control;
}
}
if (g_spec_ep_fid_ops &&
ep->fid.ops == g_orig_ep_fid_ops)
ep->fid.ops = g_spec_ep_fid_ops;
}
if (ep->ops) {
if (!g_spec_ep_ops) {
g_orig_ep_ops = ep->ops;
g_spec_ep_ops = clone_ops(ep->ops);
if (g_spec_ep_ops) {
if (HAS_OP(g_orig_ep_ops,
struct fi_ops_ep, setopt))
g_spec_ep_ops->setopt =
ep_setopt;
if (HAS_OP(g_orig_ep_ops,
struct fi_ops_ep, tx_ctx))
g_spec_ep_ops->tx_ctx =
ep_tx_ctx;
if (HAS_OP(g_orig_ep_ops,
struct fi_ops_ep, rx_ctx))
g_spec_ep_ops->rx_ctx =
ep_rx_ctx;
}
}
if (g_spec_ep_ops && ep->ops == g_orig_ep_ops)
ep->ops = g_spec_ep_ops;
}
}
// ---- CQ patching -----------------------------------------------------
static const struct fi_ops *g_orig_cq_fid_ops = nullptr;
static struct fi_ops *g_spec_cq_fid_ops = nullptr;
static int cq_fid_close(struct fid *fid)
{
if (g_hooks.on_cq_close)
g_hooks.on_cq_close(
reinterpret_cast<struct fid_cq *>(fid),
g_hooks.user);
return g_orig_cq_fid_ops->close(fid);
}
static void patch_cq(struct fid_cq *cq)
{
trace("patch_cq cq=%p ops=%p", (void *)cq, (void *)cq->ops);
if (cq->ops) {
struct fi_ops_cq *s = patched_cq_ops(cq->ops);
if (s)
cq->ops = s;
}
if (cq->fid.ops) {
if (!g_spec_cq_fid_ops) {
g_orig_cq_fid_ops = cq->fid.ops;
g_spec_cq_fid_ops = clone_ops(cq->fid.ops);
if (g_spec_cq_fid_ops &&
HAS_OP(g_orig_cq_fid_ops, struct fi_ops,
close))
g_spec_cq_fid_ops->close = cq_fid_close;
}
if (g_spec_cq_fid_ops &&
cq->fid.ops == g_orig_cq_fid_ops)
cq->fid.ops = g_spec_cq_fid_ops;
}
}
// ---- Domain patching: intercept endpoint / cq creation ---------------
static const struct fi_ops_domain *g_orig_domain_ops = nullptr;
static struct fi_ops_domain *g_spec_domain_ops = nullptr;
static int domain_endpoint(struct fid_domain *domain,
struct fi_info *info, struct fid_ep **ep,
void *context)
{
int ret = g_orig_domain_ops->endpoint(domain, info, ep,
context);
if (!ret && ep && *ep) {
patch_endpoint(*ep);
announce_ep(*ep, domain);
if (g_hooks.on_ep_info)
g_hooks.on_ep_info(*ep, info, g_hooks.user);
}
return ret;
}
static int domain_endpoint2(struct fid_domain *domain,
struct fi_info *info, struct fid_ep **ep,
uint64_t flags, void *context)
{
int ret = g_orig_domain_ops->endpoint2(domain, info, ep, flags,
context);
if (!ret && ep && *ep) {
patch_endpoint(*ep);
announce_ep(*ep, domain);
if (g_hooks.on_ep_info)
g_hooks.on_ep_info(*ep, info, g_hooks.user);
}
return ret;
}
static int domain_scalable_ep(struct fid_domain *domain,
struct fi_info *info, struct fid_ep **sep,
void *context)
{
int ret = g_orig_domain_ops->scalable_ep(domain, info, sep,
context);
if (!ret && sep && *sep) {
patch_endpoint(*sep);
announce_ep(*sep, domain);
if (g_hooks.on_ep_info)
g_hooks.on_ep_info(*sep, info, g_hooks.user);
}
return ret;
}
static int domain_srx_ctx(struct fid_domain *domain,
struct fi_rx_attr *attr,
struct fid_ep **rx_ep, void *context)
{
int ret = g_orig_domain_ops->srx_ctx(domain, attr, rx_ep,
context);
if (!ret && rx_ep && *rx_ep) {
patch_endpoint(*rx_ep);
announce_ep(*rx_ep, domain);
}
return ret;
}
// Real cq_open plus wrap: create the provider CQ, then patch it and fire
// on_cq_create. This is what the cq_open hook receives as `next`, so a
// hook that forwards still gets the CQ wrapped and the lifecycle fired;
// a hook that returns without calling it suppresses the open and nothing
// is patched (no CQ exists).
static int cq_open_forward(struct fid_domain *domain,
struct fi_cq_attr *attr, struct fid_cq **cq,
void *context)
{
int ret = g_orig_domain_ops->cq_open(domain, attr, cq, context);
if (!ret && cq && *cq) {
patch_cq(*cq);
if (g_hooks.on_cq_create)
g_hooks.on_cq_create(*cq, domain, g_hooks.user);
}
return ret;
}
static int domain_cq_open(struct fid_domain *domain,
struct fi_cq_attr *attr, struct fid_cq **cq,
void *context)
{
if (g_hooks.cq_open)
return g_hooks.cq_open(domain, attr, cq, context,
cq_open_forward, g_hooks.user);
return cq_open_forward(domain, attr, cq, context);
}
static void patch_domain(struct fid_domain *dom)
{
trace("patch_domain dom=%p ops=%p", (void *)dom,
(void *)(dom ? dom->ops : nullptr));
if (!dom->ops)
return;
if (g_spec_domain_ops && dom->ops == g_spec_domain_ops)
return;
if (!g_orig_domain_ops) {
g_orig_domain_ops = dom->ops;
g_spec_domain_ops = clone_ops(dom->ops);
if (g_spec_domain_ops) {
const struct fi_ops_domain *o = g_orig_domain_ops;
if (want_ep()) {
if (HAS_OP(o, struct fi_ops_domain,
endpoint))
g_spec_domain_ops->endpoint =
domain_endpoint;
if (HAS_OP(o, struct fi_ops_domain,
endpoint2))
g_spec_domain_ops->endpoint2 =
domain_endpoint2;
if (HAS_OP(o, struct fi_ops_domain,
scalable_ep))
g_spec_domain_ops->scalable_ep =
domain_scalable_ep;
if (HAS_OP(o, struct fi_ops_domain,
srx_ctx))
g_spec_domain_ops->srx_ctx =
domain_srx_ctx;
}
if (want_cq() &&
HAS_OP(o, struct fi_ops_domain, cq_open))
g_spec_domain_ops->cq_open =
domain_cq_open;
}
}
if (g_spec_domain_ops && dom->ops == g_orig_domain_ops)
dom->ops = g_spec_domain_ops;
}
// ---- Fabric patching: intercept domain creation ----------------------
static const struct fi_ops_fabric *g_orig_fabric_ops = nullptr;
static struct fi_ops_fabric *g_spec_fabric_ops = nullptr;
static int fabric_domain(struct fid_fabric *fabric, struct fi_info *info,
struct fid_domain **domain, void *context)
{
int ret = g_orig_fabric_ops->domain(fabric, info, domain,
context);
if (!ret && domain && *domain) {
patch_domain(*domain);
if (g_hooks.on_domain_info)
g_hooks.on_domain_info(*domain, info,
g_hooks.user);
}
return ret;
}
static int fabric_domain2(struct fid_fabric *fabric,
struct fi_info *info,
struct fid_domain **domain, uint64_t flags,
void *context)
{
int ret = g_orig_fabric_ops->domain2(fabric, info, domain, flags,
context);
if (!ret && domain && *domain) {
patch_domain(*domain);
if (g_hooks.on_domain_info)
g_hooks.on_domain_info(*domain, info,
g_hooks.user);
}
return ret;
}
static void patch_fabric(struct fid_fabric *fab)
{
trace("patch_fabric fab=%p ops=%p", (void *)fab,
(void *)(fab ? fab->ops : nullptr));
if (!fab->ops)
return;
if (g_spec_fabric_ops && fab->ops == g_spec_fabric_ops)
return;
if (!g_orig_fabric_ops) {
g_orig_fabric_ops = fab->ops;
g_spec_fabric_ops = clone_ops(fab->ops);
if (g_spec_fabric_ops) {
const struct fi_ops_fabric *o = g_orig_fabric_ops;
if (HAS_OP(o, struct fi_ops_fabric, domain))
g_spec_fabric_ops->domain =
fabric_domain;
if (HAS_OP(o, struct fi_ops_fabric, domain2))
g_spec_fabric_ops->domain2 =
fabric_domain2;
}
}
if (g_spec_fabric_ops && fab->ops == g_orig_fabric_ops)
fab->ops = g_spec_fabric_ops;
}
#undef HAS_OP
// ---- Resolve the real exported setup symbols -------------------------
using fi_fabric_fn = int (*)(struct fi_fabric_attr *,
struct fid_fabric **, void *);
using fi_fabric2_fn = int (*)(struct fi_info *, struct fid_fabric **,
uint64_t, void *);
// Resolve a real exported libfabric symbol behind our interposer.
//
// dlsym(RTLD_NEXT, ...) only searches the *global* scope after this
// object. That suffices when the app links libfabric directly (it's in
// the global scope). But some apps reach libfabric through a library
// dlopen'd with RTLD_LOCAL -- e.g. MPI via a Python C-extension
// (mpi4py/torch) -- which loads libfabric into a private namespace that
// is *not* on the global search list. Our interposed symbol is still
// what the app binds to (LD_PRELOAD symbols are global and win), but
// RTLD_NEXT then sees no provider and we'd wrongly return -FI_ENOSYS.
//
// Fall back to dlopen'ing libfabric by soname with RTLD_GLOBAL: NOLOAD
// first, which (when it is already loaded privately) returns the app's
// existing map and promotes it into the global scope -- so we use the
// very same libfabric instance, no second copy. If it isn't loaded yet,
// load it. This is setup-path only, never the hot path.