-
Notifications
You must be signed in to change notification settings - Fork 45
/
homa_plumbing.c
1725 lines (1598 loc) · 47.7 KB
/
homa_plumbing.c
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
// SPDX-License-Identifier: BSD-2-Clause
/* This file consists mostly of "glue" that hooks Homa into the rest of
* the Linux kernel. The guts of the protocol are in other files.
*/
#include "homa_impl.h"
#include "homa_offload.h"
#include "homa_peer.h"
#include "homa_pool.h"
/* Not yet sure what these variables are for */
static long sysctl_homa_mem[3] __read_mostly;
static int sysctl_homa_rmem_min __read_mostly;
static int sysctl_homa_wmem_min __read_mostly;
/* Global data for Homa. Never reference homa_data directly. Always use
* the global_homa variable instead; this allows overriding during unit tests.
*/
static struct homa homa_data;
/* This variable contains the address of the statically-allocated struct homa
* used throughout Homa. This variable should almost never be used directly:
* it should be passed as a parameter to functions that need it. This
* variable is used only by functions called from Linux (so they can't pass
* in a pointer).
*/
struct homa *global_homa = &homa_data;
/* True means that the Homa module is in the process of unloading itself,
* so everyone should clean up.
*/
static bool exiting;
/* Thread that runs timer code to detect lost packets and crashed peers. */
static struct task_struct *timer_kthread;
/* Set via sysctl to request that a particular action be taken. The value
* written determines the action.
*/
static int action;
/* This structure defines functions that handle various operations on
* Homa sockets. These functions are relatively generic: they are called
* to implement top-level system calls. Many of these operations can
* be implemented by PF_INET6 functions that are independent of the
* Homa protocol.
*/
static const struct proto_ops homa_proto_ops = {
.family = PF_INET,
.owner = THIS_MODULE,
.release = inet_release,
.bind = homa_bind,
.connect = inet_dgram_connect,
.socketpair = sock_no_socketpair,
.accept = sock_no_accept,
.getname = inet_getname,
.poll = homa_poll,
.ioctl = inet_ioctl,
.listen = sock_no_listen,
.shutdown = homa_shutdown,
.setsockopt = sock_common_setsockopt,
.getsockopt = sock_common_getsockopt,
.sendmsg = inet_sendmsg,
.recvmsg = inet_recvmsg,
.mmap = sock_no_mmap,
.set_peek_off = sk_set_peek_off,
};
static const struct proto_ops homav6_proto_ops = {
.family = PF_INET6,
.owner = THIS_MODULE,
.release = inet6_release,
.bind = homa_bind,
.connect = inet_dgram_connect,
.socketpair = sock_no_socketpair,
.accept = sock_no_accept,
.getname = inet6_getname,
.poll = homa_poll,
.ioctl = inet6_ioctl,
.listen = sock_no_listen,
.shutdown = homa_shutdown,
.setsockopt = sock_common_setsockopt,
.getsockopt = sock_common_getsockopt,
.sendmsg = inet_sendmsg,
.recvmsg = inet_recvmsg,
.mmap = sock_no_mmap,
.set_peek_off = sk_set_peek_off,
};
/* This structure also defines functions that handle various operations
* on Homa sockets. However, these functions are lower-level than those
* in homa_proto_ops: they are specific to the PF_INET or PF_INET6
* protocol family, and in many cases they are invoked by functions in
* homa_proto_ops. Most of these functions have Homa-specific implementations.
*/
static struct proto homa_prot = {
.name = "HOMA",
.owner = THIS_MODULE,
.close = homa_close,
.connect = ip4_datagram_connect,
.disconnect = homa_disconnect,
.ioctl = homa_ioctl,
.init = homa_socket,
.destroy = NULL,
.setsockopt = homa_setsockopt,
.getsockopt = homa_getsockopt,
.sendmsg = homa_sendmsg,
.recvmsg = homa_recvmsg,
.backlog_rcv = homa_backlog_rcv,
.hash = homa_hash,
.unhash = homa_unhash,
.get_port = homa_get_port,
.sysctl_mem = sysctl_homa_mem,
.sysctl_wmem = &sysctl_homa_wmem_min,
.sysctl_rmem = &sysctl_homa_rmem_min,
.obj_size = sizeof(struct homa_sock),
.no_autobind = 1,
};
static struct proto homav6_prot = {
.name = "HOMAv6",
.owner = THIS_MODULE,
.close = homa_close,
.connect = ip6_datagram_connect,
.disconnect = homa_disconnect,
.ioctl = homa_ioctl,
.init = homa_socket,
.destroy = NULL,
.setsockopt = homa_setsockopt,
.getsockopt = homa_getsockopt,
.sendmsg = homa_sendmsg,
.recvmsg = homa_recvmsg,
.backlog_rcv = homa_backlog_rcv,
.hash = homa_hash,
.unhash = homa_unhash,
.get_port = homa_get_port,
.sysctl_mem = sysctl_homa_mem,
.sysctl_wmem = &sysctl_homa_wmem_min,
.sysctl_rmem = &sysctl_homa_rmem_min,
.obj_size = sizeof(struct homa_v6_sock),
.ipv6_pinfo_offset = offsetof(struct homa_v6_sock, inet6),
.no_autobind = 1,
};
/* Top-level structure describing the Homa protocol. */
static struct inet_protosw homa_protosw = {
.type = SOCK_DGRAM,
.protocol = IPPROTO_HOMA,
.prot = &homa_prot,
.ops = &homa_proto_ops,
.flags = INET_PROTOSW_REUSE,
};
static struct inet_protosw homav6_protosw = {
.type = SOCK_DGRAM,
.protocol = IPPROTO_HOMA,
.prot = &homav6_prot,
.ops = &homav6_proto_ops,
.flags = INET_PROTOSW_REUSE,
};
/* This structure is used by IP to deliver incoming Homa packets to us. */
static struct net_protocol homa_protocol = {
.handler = homa_softirq,
.err_handler = homa_err_handler_v4,
.no_policy = 1,
};
static struct inet6_protocol homav6_protocol = {
.handler = homa_softirq,
.err_handler = homa_err_handler_v6,
.flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
};
/* Describes file operations implemented for /proc/net/homa_metrics. */
static const struct proc_ops homa_metrics_pops = {
.proc_open = homa_metrics_open,
.proc_read = homa_metrics_read,
.proc_lseek = homa_metrics_lseek,
.proc_release = homa_metrics_release,
};
/* Used to remove /proc/net/homa_metrics when the module is unloaded. */
static struct proc_dir_entry *metrics_dir_entry;
/* Used to configure sysctl access to Homa configuration parameters.*/
static struct ctl_table homa_ctl_table[] = {
{
.procname = "action",
.data = &action,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "bpage_lease_usecs",
.data = &homa_data.bpage_lease_usecs,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "busy_usecs",
.data = &homa_data.busy_usecs,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "cutoff_version",
.data = &homa_data.cutoff_version,
.maxlen = sizeof(int),
.mode = 0444,
.proc_handler = proc_dointvec
},
{
.procname = "dead_buffs_limit",
.data = &homa_data.dead_buffs_limit,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "fifo_grant_increment",
.data = &homa_data.fifo_grant_increment,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "flags",
.data = &homa_data.flags,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "freeze_type",
.data = &homa_data.freeze_type,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "gen3_softirq_cores",
.data = NULL,
.maxlen = 0,
.mode = 0644,
.proc_handler = homa_sysctl_softirq_cores
},
{
.procname = "grant_fifo_fraction",
.data = &homa_data.grant_fifo_fraction,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "gro_busy_usecs",
.data = &homa_data.gro_busy_usecs,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "gro_policy",
.data = &homa_data.gro_policy,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "gso_force_software",
.data = &homa_data.gso_force_software,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "hijack_tcp",
.data = &homa_data.hijack_tcp,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "link_mbps",
.data = &homa_data.link_mbps,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "max_dead_buffs",
.data = &homa_data.max_dead_buffs,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "max_grantable_rpcs",
.data = &homa_data.max_grantable_rpcs,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "max_gro_skbs",
.data = &homa_data.max_gro_skbs,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "max_gso_size",
.data = &homa_data.max_gso_size,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "max_nic_queue_ns",
.data = &homa_data.max_nic_queue_ns,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "max_incoming",
.data = &homa_data.max_incoming,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "max_overcommit",
.data = &homa_data.max_overcommit,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "max_rpcs_per_peer",
.data = &homa_data.max_rpcs_per_peer,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "max_sched_prio",
.data = &homa_data.max_sched_prio,
.maxlen = sizeof(int),
.mode = 0444,
.proc_handler = proc_dointvec
},
{
.procname = "next_id",
.data = &homa_data.next_id,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "num_priorities",
.data = &homa_data.num_priorities,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "pacer_fifo_fraction",
.data = &homa_data.pacer_fifo_fraction,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "poll_usecs",
.data = &homa_data.poll_usecs,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "priority_map",
.data = &homa_data.priority_map,
.maxlen = HOMA_MAX_PRIORITIES * sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "reap_limit",
.data = &homa_data.reap_limit,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "request_ack_ticks",
.data = &homa_data.request_ack_ticks,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "resend_interval",
.data = &homa_data.resend_interval,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "resend_ticks",
.data = &homa_data.resend_ticks,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "skb_page_frees_per_sec",
.data = &homa_data.skb_page_frees_per_sec,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "skb_page_pool_min_kb",
.data = &homa_data.skb_page_pool_min_kb,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "temp",
.data = homa_data.temp,
.maxlen = sizeof(homa_data.temp),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "throttle_min_bytes",
.data = &homa_data.throttle_min_bytes,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "timeout_resends",
.data = &homa_data.timeout_resends,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "timeout_ticks",
.data = &homa_data.timeout_ticks,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "unsched_bytes",
.data = &homa_data.unsched_bytes,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "unsched_cutoffs",
.data = &homa_data.unsched_cutoffs,
.maxlen = HOMA_MAX_PRIORITIES * sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "verbose",
.data = &homa_data.verbose,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "window",
.data = &homa_data.window_param,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 12, 0)
{}
#endif
};
/* Sizes of the headers for each Homa packet type, in bytes. */
static __u16 header_lengths[] = {
sizeof32(struct homa_data_hdr),
sizeof32(struct homa_grant_hdr),
sizeof32(struct homa_resend_hdr),
sizeof32(struct homa_unknown_hdr),
sizeof32(struct homa_busy_hdr),
sizeof32(struct homa_cutoffs_hdr),
sizeof32(struct homa_freeze_hdr),
sizeof32(struct homa_need_ack_hdr),
sizeof32(struct homa_ack_hdr)
};
/* Used to remove sysctl values when the module is unloaded. */
static struct ctl_table_header *homa_ctl_header;
static DECLARE_COMPLETION(timer_thread_done);
/**
* homa_load() - invoked when this module is loaded into the Linux kernel
* Return: 0 on success, otherwise a negative errno.
*/
int __init homa_load(void)
{
struct homa *homa = global_homa;
int status;
pr_notice("Homa module loading\n");
#ifndef __STRIP__ /* See strip.py */
pr_notice("Homa structure sizes: homa_data_hdr %u, homa_seg_hdr %u, ack %u, homa_grant_hdr %u, peer %u, ip_hdr %u flowi %u ipv6_hdr %u, flowi6 %u tcp_sock %u homa_rpc %u sk_buff %u rcvmsg_control %u union sockaddr_in_union %u HOMA_MAX_BPAGES %u NR_CPUS %u nr_cpu_ids %u, MAX_NUMNODES %d\n",
sizeof32(struct homa_data_hdr),
sizeof32(struct homa_seg_hdr),
sizeof32(struct homa_ack),
sizeof32(struct homa_grant_hdr),
sizeof32(struct homa_peer),
sizeof32(struct iphdr),
sizeof32(struct flowi),
sizeof32(struct ipv6hdr),
sizeof32(struct flowi6),
sizeof32(struct tcp_sock),
sizeof32(struct homa_rpc),
sizeof32(struct sk_buff),
sizeof32(struct homa_recvmsg_args),
sizeof32(union sockaddr_in_union),
HOMA_MAX_BPAGES,
NR_CPUS,
nr_cpu_ids,
MAX_NUMNODES);
#endif /* See strip.py */
status = proto_register(&homa_prot, 1);
if (status != 0) {
pr_err("proto_register failed for homa_prot: %d\n", status);
goto proto_register_err;
}
status = proto_register(&homav6_prot, 1);
if (status != 0) {
pr_err("proto_register failed for homav6_prot: %d\n", status);
goto proto_register_v6_err;
}
inet_register_protosw(&homa_protosw);
status = inet6_register_protosw(&homav6_protosw);
if (status != 0) {
pr_err("inet6_register_protosw failed in %s: %d\n", __func__,
status);
goto register_protosw_v6_err;
}
status = inet_add_protocol(&homa_protocol, IPPROTO_HOMA);
if (status != 0) {
pr_err("inet_add_protocol failed in %s: %d\n", __func__,
status);
goto add_protocol_err;
}
status = inet6_add_protocol(&homav6_protocol, IPPROTO_HOMA);
if (status != 0) {
pr_err("inet6_add_protocol failed in %s: %d\n", __func__,
status);
goto add_protocol_v6_err;
}
status = homa_init(homa);
if (status)
goto homa_init_err;
metrics_dir_entry = proc_create("homa_metrics", 0444,
init_net.proc_net, &homa_metrics_pops);
if (!metrics_dir_entry) {
pr_err("couldn't create /proc/net/homa_metrics\n");
status = -ENOMEM;
goto metrics_err;
}
homa_ctl_header = register_net_sysctl(&init_net, "net/homa",
homa_ctl_table);
if (!homa_ctl_header) {
pr_err("couldn't register Homa sysctl parameters\n");
status = -ENOMEM;
goto sysctl_err;
}
status = homa_offload_init();
if (status != 0) {
pr_err("Homa couldn't init offloads\n");
goto offload_err;
}
timer_kthread = kthread_run(homa_timer_main, homa, "homa_timer");
if (IS_ERR(timer_kthread)) {
status = PTR_ERR(timer_kthread);
pr_err("couldn't create homa pacer thread: error %d\n",
status);
timer_kthread = NULL;
goto timer_err;
}
homa_gro_hook_tcp();
#ifndef __STRIP__ /* See strip.py */
tt_init("timetrace", homa->temp);
#endif /* See strip.py */
return 0;
timer_err:
homa_offload_end();
offload_err:
unregister_net_sysctl_table(homa_ctl_header);
sysctl_err:
proc_remove(metrics_dir_entry);
metrics_err:
homa_destroy(homa);
homa_init_err:
inet6_del_protocol(&homav6_protocol, IPPROTO_HOMA);
add_protocol_v6_err:
inet_del_protocol(&homa_protocol, IPPROTO_HOMA);
add_protocol_err:
inet6_unregister_protosw(&homav6_protosw);
register_protosw_v6_err:
inet_unregister_protosw(&homa_protosw);
proto_unregister(&homav6_prot);
proto_register_v6_err:
proto_unregister(&homa_prot);
proto_register_err:
return status;
}
/**
* homa_unload() - invoked when this module is unloaded from the Linux kernel.
*/
void __exit homa_unload(void)
{
struct homa *homa = global_homa;
pr_notice("Homa module unloading\n");
exiting = true;
#ifndef __STRIP__ /* See strip.py */
tt_destroy();
#endif /* See strip.py */
homa_gro_unhook_tcp();
if (timer_kthread)
wake_up_process(timer_kthread);
if (homa_offload_end() != 0)
pr_err("Homa couldn't stop offloads\n");
wait_for_completion(&timer_thread_done);
unregister_net_sysctl_table(homa_ctl_header);
proc_remove(metrics_dir_entry);
homa_destroy(homa);
inet_del_protocol(&homa_protocol, IPPROTO_HOMA);
inet_unregister_protosw(&homa_protosw);
inet6_del_protocol(&homav6_protocol, IPPROTO_HOMA);
inet6_unregister_protosw(&homav6_protosw);
proto_unregister(&homa_prot);
proto_unregister(&homav6_prot);
}
module_init(homa_load);
module_exit(homa_unload);
/**
* homa_bind() - Implements the bind system call for Homa sockets: associates
* a well-known service port with a socket. Unlike other AF_INET6 protocols,
* there is no need to invoke this system call for sockets that are only
* used as clients.
* @sock: Socket on which the system call was invoked.
* @addr: Contains the desired port number.
* @addr_len: Number of bytes in uaddr.
* Return: 0 on success, otherwise a negative errno.
*/
int homa_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
{
union sockaddr_in_union *addr_in = (union sockaddr_in_union *)addr;
struct homa_sock *hsk = homa_sk(sock->sk);
int port = 0;
if (unlikely(addr->sa_family != sock->sk->sk_family))
return -EAFNOSUPPORT;
if (addr_in->in6.sin6_family == AF_INET6) {
if (addr_len < sizeof(struct sockaddr_in6))
return -EINVAL;
port = ntohs(addr_in->in4.sin_port);
} else if (addr_in->in4.sin_family == AF_INET) {
if (addr_len < sizeof(struct sockaddr_in))
return -EINVAL;
port = ntohs(addr_in->in6.sin6_port);
}
return homa_sock_bind(hsk->homa->port_map, hsk, port);
}
/**
* homa_close() - Invoked when close system call is invoked on a Homa socket.
* @sk: Socket being closed
* @timeout: ??
*/
void homa_close(struct sock *sk, long timeout)
{
struct homa_sock *hsk = homa_sk(sk);
homa_sock_destroy(hsk);
sk_common_release(sk);
tt_record1("closed socket, port %d", hsk->port);
if (hsk->homa->freeze_type == SOCKET_CLOSE)
tt_freeze();
}
/**
* homa_shutdown() - Implements the shutdown system call for Homa sockets.
* @sock: Socket to shut down.
* @how: Ignored: for other sockets, can independently shut down
* sending and receiving, but for Homa any shutdown will
* shut down everything.
*
* Return: 0 on success, otherwise a negative errno.
*/
int homa_shutdown(struct socket *sock, int how)
{
homa_sock_shutdown(homa_sk(sock->sk));
return 0;
}
/**
* homa_disconnect() - Invoked when disconnect system call is invoked on a
* Homa socket.
* @sk: Socket to disconnect
* @flags: ??
*
* Return: 0 on success, otherwise a negative errno.
*/
int homa_disconnect(struct sock *sk, int flags)
{
pr_warn("unimplemented disconnect invoked on Homa socket\n");
return -EINVAL;
}
/**
* homa_ioc_abort() - The top-level function for the ioctl that implements
* the homa_abort user-level API.
* @sk: Socket for this request.
* @karg: Used to pass information from user space.
*
* Return: 0 on success, otherwise a negative errno.
*/
int homa_ioc_abort(struct sock *sk, int *karg)
{
int ret = 0;
struct homa_sock *hsk = homa_sk(sk);
struct homa_abort_args args;
struct homa_rpc *rpc;
if (unlikely(copy_from_user(&args, (void __user *)karg, sizeof(args))))
return -EFAULT;
if (args._pad1 || args._pad2[0] || args._pad2[1])
return -EINVAL;
if (args.id == 0) {
homa_abort_sock_rpcs(hsk, -args.error);
return 0;
}
rpc = homa_find_client_rpc(hsk, args.id);
if (!rpc)
return -EINVAL;
if (args.error == 0)
homa_rpc_free(rpc);
else
homa_rpc_abort(rpc, -args.error);
homa_rpc_unlock(rpc); /* Locked by homa_find_client_rpc. */
return ret;
}
/**
* homa_ioctl() - Implements the ioctl system call for Homa sockets.
* @sk: Socket on which the system call was invoked.
* @cmd: Identifier for a particular ioctl operation.
* @karg: Operation-specific argument; typically the address of a block
* of data in user address space.
*
* Return: 0 on success, otherwise a negative errno.
*/
int homa_ioctl(struct sock *sk, int cmd, int *karg)
{
int result;
__u64 start = sched_clock();
switch (cmd) {
case HOMAIOCABORT:
result = homa_ioc_abort(sk, karg);
INC_METRIC(abort_calls, 1);
INC_METRIC(abort_ns, sched_clock() - start);
break;
case HOMAIOCFREEZE:
tt_record1("Freezing timetrace because of HOMAIOCFREEZE ioctl, pid %d",
current->pid);
tt_freeze();
result = 0;
break;
default:
pr_notice("Unknown Homa ioctl: %d\n", cmd);
result = -EINVAL;
break;
}
return result;
}
/**
* homa_socket() - Implements the socket(2) system call for sockets.
* @sk: Socket on which the system call was invoked. The non-Homa
* parts have already been initialized.
*
* Return: always 0 (success).
*/
int homa_socket(struct sock *sk)
{
struct homa_sock *hsk = homa_sk(sk);
struct homa *homa = global_homa;
int result;
result = homa_sock_init(hsk, homa);
if (result != 0)
homa_sock_destroy(hsk);
return result;
}
/**
* homa_setsockopt() - Implements the getsockopt system call for Homa sockets.
* @sk: Socket on which the system call was invoked.
* @level: Level at which the operation should be handled; will always
* be IPPROTO_HOMA.
* @optname: Identifies a particular setsockopt operation.
* @optval: Address in user space of information about the option.
* @optlen: Number of bytes of data at @optval.
* Return: 0 on success, otherwise a negative errno.
*/
int homa_setsockopt(struct sock *sk, int level, int optname,
sockptr_t optval, unsigned int optlen)
{
struct homa_sock *hsk = homa_sk(sk);
struct homa_rcvbuf_args args;
__u64 start = sched_clock();
int ret;
if (level != IPPROTO_HOMA || optname != SO_HOMA_RCVBUF)
return -ENOPROTOOPT;
if (optlen != sizeof(struct homa_rcvbuf_args))
return -EINVAL;
if (copy_from_sockptr(&args, optval, optlen))
return -EFAULT;
/* Do a trivial test to make sure we can at least write the first
* page of the region.
*/
if (copy_to_user((__force void __user *)args.start, &args,
sizeof(args)))
return -EFAULT;
homa_sock_lock(hsk, "homa_setsockopt SO_HOMA_RCV_BUF");
ret = homa_pool_init(hsk, (__force void __user *)args.start,
args.length);
homa_sock_unlock(hsk);
INC_METRIC(so_set_buf_calls, 1);
INC_METRIC(so_set_buf_ns, sched_clock() - start);
return ret;
}
/**
* homa_getsockopt() - Implements the getsockopt system call for Homa sockets.
* @sk: Socket on which the system call was invoked.
* @level: Selects level in the network stack to handle the request;
* must be IPPROTO_HOMA.
* @optname: Identifies a particular setsockopt operation.
* @optval: Address in user space where the option's value should be stored.
* @optlen: Number of bytes available at optval; will be overwritten with
* actual number of bytes stored.
* Return: 0 on success, otherwise a negative errno.
*/
int homa_getsockopt(struct sock *sk, int level, int optname,
char __user *optval, int __user *optlen)
{
struct homa_sock *hsk = homa_sk(sk);
struct homa_rcvbuf_args val;
int len;
if (copy_from_sockptr(&len, USER_SOCKPTR(optlen), sizeof(int)))
return -EFAULT;
if (level != IPPROTO_HOMA || optname != SO_HOMA_RCVBUF)
return -ENOPROTOOPT;
if (len < sizeof(val))
return -EINVAL;
homa_pool_get_rcvbuf(hsk, &val);
len = sizeof(val);
if (copy_to_sockptr(USER_SOCKPTR(optlen), &len, sizeof(int)))
return -EFAULT;
if (copy_to_sockptr(USER_SOCKPTR(optval), &val, len))
return -EFAULT;
return 0;
}
/**
* homa_sendmsg() - Send a request or response message on a Homa socket.
* @sk: Socket on which the system call was invoked.
* @msg: Structure describing the message to send; the msg_control
* field points to additional information.
* @length: Number of bytes of the message.
* Return: 0 on success, otherwise a negative errno.
*/
int homa_sendmsg(struct sock *sk, struct msghdr *msg, size_t length)
{
struct homa_sock *hsk = homa_sk(sk);
struct homa_sendmsg_args args;
union sockaddr_in_union *addr;
__u64 start = sched_clock();
struct homa_rpc *rpc = NULL;
int result = 0;
__u64 finish;
per_cpu(homa_offload_core, raw_smp_processor_id()).last_app_active =
start;
addr = (union sockaddr_in_union *)msg->msg_name;
if (!addr) {
result = -EINVAL;
goto error;
}
if (unlikely(!msg->msg_control_is_user)) {
tt_record("homa_sendmsg error: !msg->msg_control_is_user");
result = -EINVAL;
goto error;
}
if (unlikely(copy_from_user(&args, (void __user *)msg->msg_control,
sizeof(args)))) {
result = -EFAULT;
goto error;
}
if (addr->sa.sa_family != sk->sk_family) {
result = -EAFNOSUPPORT;
goto error;
}
if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
(msg->msg_namelen < sizeof(struct sockaddr_in6) &&
addr->in6.sin6_family == AF_INET6)) {
tt_record("homa_sendmsg error: msg_namelen too short");
result = -EINVAL;
goto error;
}
if (!args.id) {
/* This is a request message. */
rpc = homa_rpc_new_client(hsk, addr);
if (IS_ERR(rpc)) {
result = PTR_ERR(rpc);
rpc = NULL;
goto error;
}
INC_METRIC(send_calls, 1);
tt_record4("homa_sendmsg request, target 0x%x:%d, id %u, length %d",
(addr->in6.sin6_family == AF_INET)
? ntohl(addr->in4.sin_addr.s_addr)
: tt_addr(addr->in6.sin6_addr),
ntohs(addr->in6.sin6_port), rpc->id, length);
rpc->completion_cookie = args.completion_cookie;
result = homa_message_out_fill(rpc, &msg->msg_iter, 1);
if (result)
goto error;
args.id = rpc->id;
homa_rpc_unlock(rpc); /* Locked by homa_rpc_new_client. */
rpc = NULL;
if (unlikely(copy_to_user((void __user *)msg->msg_control,
&args, sizeof(args)))) {
rpc = homa_find_client_rpc(hsk, args.id);
result = -EFAULT;
goto error;
}
finish = sched_clock();
INC_METRIC(send_ns, finish - start);
} else {
/* This is a response message. */
struct in6_addr canonical_dest;
INC_METRIC(reply_calls, 1);