forked from PlatformLab/HomaModule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
homa_plumbing.c
1743 lines (1615 loc) · 49 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
/* Copyright (c) 2019-2023 Stanford University
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* 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_lcache.h"
#ifndef __UNIT_TEST__
MODULE_LICENSE("Dual MIT/GPL");
#endif
MODULE_AUTHOR("John Ousterhout");
MODULE_DESCRIPTION("Homa transport protocol");
MODULE_VERSION("0.01");
/* Export symbols for use in nvme-over-homa */
/* EXPORT_SYMBOL(homa_bind);
EXPORT_SYMBOL(homa_shutdown);
// EXPORT_SYMBOL(homa_abort);
EXPORT_SYMBOL(homa_socket);
EXPORT_SYMBOL(homa_setsockopt);
EXPORT_SYMBOL(homa_sendmsg);
EXPORT_SYMBOL(homa_recvmsg); */
/* Not yet sure what these variables are for */
long sysctl_homa_mem[3] __read_mostly;
int sysctl_homa_rmem_min __read_mostly;
int sysctl_homa_wmem_min __read_mostly;
atomic_long_t homa_memory_allocated;
static DEFINE_PER_CPU(int, homa_memory_per_cpu_fw_alloc);
/* Global data for Homa. Never reference homa_data directory. Always use
* the homa variable instead; this allows overriding during unit tests.
*/
struct homa homa_data;
struct homa *homa = &homa_data;
/* True means that the Homa module is in the process of unloading itself,
* so everyone should clean up.
*/
static bool exiting = false;
/* Thread that runs timer code to detect lost packets and crashed peers. */
static struct task_struct *timer_kthread;
/* Set via sysctl to request that information on a particular topic
* be printed to the system log. The value written determines the
* topic.
*/
static int log_topic;
/* 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.
*/
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,
// .sendpage = sock_no_sendpage,
.set_peek_off = sk_set_peek_off,
};
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,
// .sendpage = sock_no_sendpage,
.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.
*/
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 = 0,
.setsockopt = homa_setsockopt,
.getsockopt = homa_getsockopt,
.sendmsg = homa_sendmsg,
.recvmsg = homa_recvmsg,
// .sendpage = homa_sendpage,
.backlog_rcv = homa_backlog_rcv,
.release_cb = ip4_datagram_release_cb,
.hash = homa_hash,
.unhash = homa_unhash,
.rehash = homa_rehash,
.get_port = homa_get_port,
.memory_allocated = &homa_memory_allocated,
.per_cpu_fw_alloc = &homa_memory_per_cpu_fw_alloc,
.sysctl_mem = sysctl_homa_mem,
.sysctl_wmem = &sysctl_homa_wmem_min,
.sysctl_rmem = &sysctl_homa_rmem_min,
.obj_size = sizeof(struct homa_sock),
.diag_destroy = homa_diag_destroy,
.no_autobind = 1,
};
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 = 0,
.setsockopt = homa_setsockopt,
.getsockopt = homa_getsockopt,
.sendmsg = homa_sendmsg,
.recvmsg = homa_recvmsg,
// .sendpage = homa_sendpage,
.backlog_rcv = homa_backlog_rcv,
.release_cb = ip6_datagram_release_cb,
.hash = homa_hash,
.unhash = homa_unhash,
.rehash = homa_rehash,
.get_port = homa_get_port,
.memory_allocated = &homa_memory_allocated,
.per_cpu_fw_alloc = &homa_memory_per_cpu_fw_alloc,
.sysctl_mem = sysctl_homa_mem,
.sysctl_wmem = &sysctl_homa_wmem_min,
.sysctl_rmem = &sysctl_homa_rmem_min,
/* IPv6 data comes *after* Homa's data, and isn't included in
* struct homa_sock.
*/
.obj_size = sizeof(struct homa_sock) + sizeof(struct ipv6_pinfo),
.diag_destroy = homa_diag_destroy,
.no_autobind = 1,
};
/* Top-level structure describing the Homa protocol. */
struct inet_protosw homa_protosw = {
.type = SOCK_DGRAM,
.protocol = IPPROTO_HOMA,
.prot = &homa_prot,
.ops = &homa_proto_ops,
.flags = INET_PROTOSW_REUSE,
};
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 = NULL;
/* Used to configure sysctl access to Homa configuration parameters.*/
static struct ctl_table homa_ctl_table[] = {
{
.procname = "bpage_lease_usecs",
.data = &homa_data.bpage_lease_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 = "duty_cycle",
.data = &homa_data.duty_cycle,
.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 = "grant_fifo_fraction",
.data = &homa_data.grant_fifo_fraction,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "fifo_grant_increment",
.data = &homa_data.fifo_grant_increment,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "gro_busy_us",
.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 = "link_mbps",
.data = &homa_data.link_mbps,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "log_topic",
.data = &log_topic,
.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 = "max_dead_buffs",
.data = &homa_data.max_dead_buffs,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "max_grant_window",
.data = &homa_data.max_grant_window,
.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_overcommit",
.data = &homa_data.max_overcommit,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "max_sched_prio",
.data = &homa_data.max_sched_prio,
.maxlen = sizeof(int),
.mode = 0444,
.proc_handler = proc_dointvec
},
{
.procname = "num_priorities",
.data = &homa_data.num_priorities,
.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 = "rtt_bytes",
.data = &homa_data.rtt_bytes,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_dointvec
},
{
.procname = "sync_freeze",
.data = &homa_data.sync_freeze,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_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 = "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
},
{}
};
/* Sizes of the headers for each Homa packet type, in bytes. */
static __u16 header_lengths[] = {
sizeof32(struct data_header),
sizeof32(struct grant_header),
sizeof32(struct resend_header),
sizeof32(struct unknown_header),
sizeof32(struct busy_header),
sizeof32(struct cutoffs_header),
sizeof32(struct freeze_header),
sizeof32(struct need_ack_header),
sizeof32(struct ack_header)
};
/* 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.
*/
static int __init homa_load(void) {
int status;
printk(KERN_NOTICE "Homa module loading\n");
printk(KERN_NOTICE "Homa structure sizes: data_header %u, "
"data_segment %u, ack %u, "
"grant_header %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 sockaddr_in_union %u "
"HOMA_MAX_BPAGES %u NR_CPUS %u "
"nr_cpu_ids %u\n",
sizeof32(struct data_header),
sizeof32(struct data_segment),
sizeof32(struct homa_ack),
sizeof32(struct grant_header),
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(sockaddr_in_union),
HOMA_MAX_BPAGES,
NR_CPUS,
nr_cpu_ids);
status = proto_register(&homa_prot, 1);
if (status != 0) {
printk(KERN_ERR "proto_register failed for homa_prot: %d\n",
status);
goto out;
}
status = proto_register(&homav6_prot, 1);
if (status != 0) {
printk(KERN_ERR "proto_register failed for homav6_prot: %d\n",
status);
goto out;
}
inet_register_protosw(&homa_protosw);
inet6_register_protosw(&homav6_protosw);
status = inet_add_protocol(&homa_protocol, IPPROTO_HOMA);
if (status != 0) {
printk(KERN_ERR "inet_add_protocol failed in homa_load: %d\n",
status);
goto out_cleanup;
}
status = inet6_add_protocol(&homav6_protocol, IPPROTO_HOMA);
if (status != 0) {
printk(KERN_ERR "inet6_add_protocol failed in homa_load: %d\n",
status);
goto out_cleanup;
}
status = homa_init(homa);
if (status)
goto out_cleanup;
metrics_dir_entry = proc_create("homa_metrics", S_IRUGO,
init_net.proc_net, &homa_metrics_pops);
if (!metrics_dir_entry) {
printk(KERN_ERR "couldn't create /proc/net/homa_metrics\n");
status = -ENOMEM;
goto out_cleanup;
}
homa_ctl_header = register_net_sysctl(&init_net, "net/homa",
homa_ctl_table);
if (!homa_ctl_header) {
printk(KERN_ERR "couldn't register Homa sysctl parameters\n");
status = -ENOMEM;
goto out_cleanup;
}
status = homa_offload_init();
if (status != 0) {
printk(KERN_ERR "Homa couldn't init offloads\n");
goto out_cleanup;
}
timer_kthread = kthread_run(homa_timer_main, homa, "homa_timer");
if (IS_ERR(timer_kthread)) {
status = PTR_ERR(timer_kthread);
printk(KERN_ERR "couldn't create homa pacer thread: error %d\n",
status);
timer_kthread = NULL;
goto out_cleanup;
}
tt_init("timetrace", homa->temp);
return 0;
out_cleanup:
homa_offload_end();
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);
out:
return status;
}
/**
* homa_unload() - invoked when this module is unloaded from the Linux kernel.
*/
static void __exit homa_unload(void) {
printk(KERN_NOTICE "Homa module unloading\n");
exiting = true;
tt_destroy();
if (timer_kthread)
wake_up_process(timer_kthread);
if (homa_offload_end() != 0)
printk(KERN_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)
{
struct homa_sock *hsk = homa_sk(sock->sk);
sockaddr_in_union *addr_in = (sockaddr_in_union *) addr;
int port;
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(&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\n", hsk->port);
if (hsk->homa->freeze_type == SOCKET_CLOSE)
tt_freeze();
}
/**
* homa_shutdown() - Implements the shutdown system call for Homa sockets.
* @sk: 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) {
printk(KERN_WARNING "unimplemented disconnect invoked on Homa socket\n");
return -ENOSYS;
}
/**
* homa_ioc_abort() - The top-level function for the ioctl that implements
* the homa_abort user-level API.
* @sk: Socket for this request.
* @arg: Used to pass information from user space.
*
* Return: 0 on success, otherwise a negative errno.
*/
int homa_ioc_abort(struct sock *sk, unsigned long arg) {
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 *) arg, 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 == NULL)
return -EINVAL;
if (args.error == 0) {
homa_rpc_free(rpc);
} else {
homa_rpc_abort(rpc, -args.error);
}
homa_rpc_unlock(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.
* @arg: 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 arg = *karg;
int result;
__u64 start = get_cycles();
switch (cmd) {
case HOMAIOCABORT:
result = homa_ioc_abort(sk, arg);
INC_METRIC(abort_calls, 1);
INC_METRIC(abort_cycles, get_cycles() - start);
break;
case HOMAIOCFREEZE:
tt_record1("Freezing timetrace because of HOMAIOCFREEZE ioctl, "
"pid %d", current->pid);
tt_freeze();
result = 0;
break;
default:
printk(KERN_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);
homa_sock_init(hsk, homa);
return 0;
}
/**
* 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_set_buf_args args;
__u64 start = get_cycles();
int ret;
if ((level != IPPROTO_HOMA) || (optname != SO_HOMA_SET_BUF)
|| (optlen != sizeof(struct homa_set_buf_args)))
return -EINVAL;
if (copy_from_sockptr(&args, optval, optlen))
{
pr_warn("%s\n", "HOMA MODULE copy_from_sockptr returned EFAULT");
return -EFAULT;
}
/* Do a trivial test to make sure we can at least write the first
* page of the region.
*/
/* if (copy_to_user(args.start, &args, sizeof(args))) // this won't work in kernel, get rid of this.
{
pr_warn("%s\n", "HOMA MODULE copy_to_user returned EFAULT");
return -EFAULT;
} */
homa_sock_lock(hsk, "homa_setsockopt SO_HOMA_SET_BUF");
ret = homa_pool_init(&hsk->buffer_pool, hsk->homa, args.start,
args.length);
homa_sock_unlock(hsk);
INC_METRIC(so_set_buf_calls, 1);
INC_METRIC(so_set_buf_cycles, get_cycles() - start);
return ret;
}
/**
* homa_getsockopt() - Implements the getsockopt system call for Homa sockets.
* @sk: Socket on which the system call was invoked.
* @level: ??
* @optname: Identifies a particular setsockopt operation.
* @optval: Address in user space where the option's value should be stored.
* @option: ??.
* Return: 0 on success, otherwise a negative errno.
*/
int homa_getsockopt(struct sock *sk, int level, int optname,
char __user *optval, int __user *option) {
printk(KERN_WARNING "unimplemented getsockopt invoked on Homa socket:"
" level %d, optname %d\n", level, optname);
return -EINVAL;
}
/**
* 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.
* @len: 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;
__u64 start = get_cycles();
__u64 finish;
int result = 0;
struct homa_rpc *rpc = NULL;
sockaddr_in_union *addr = (sockaddr_in_union *) msg->msg_name;
int zc = 0;
if (msg->msg_flags & MSG_SPLICE_PAGES)
{
// TODO: should probably need this
// if (sk->sk_route_caps & NETIF_F_SG)
zc = MSG_SPLICE_PAGES;
pr_info("homa_sendmsg using zc = MSG_SPLICE_PAGES\n");
}
// pr_info("gfp for sk: %d, sk->sk_allocation == GFP_KERNEL: %d\n", sk->sk_allocation, sk->sk_allocation == GFP_KERNEL); // true
// we are in kernel, this doesn't apply
/* if (unlikely(!msg->msg_control_is_user)) {
result = -EINVAL;
goto error;
} */
if (memcpy(&args, msg->msg_control,
sizeof(args)) == 0)
{
result = -EFAULT;
goto error;
}
if (addr->in6.sin6_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))) {
result = -EINVAL;
goto error;
}
if (!args.id) {
/* This is a request message. */
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),
atomic64_read(&hsk->homa->next_outgoing_id),
length);
rpc = homa_rpc_new_client(hsk, addr);
if (IS_ERR(rpc)) {
result = PTR_ERR(rpc);
rpc = NULL;
goto error;
}
rpc->completion_cookie = args.completion_cookie;
result = homa_message_out_init(rpc, &msg->msg_iter, 1, zc);
if (result){
pr_err("homa_message_out_init: %d\n", result);
goto error;
}
args.id = rpc->id;
homa_rpc_unlock(rpc);
rpc = NULL;
if (memcpy(msg->msg_control, &args,
sizeof(args)) == 0) {
rpc = homa_find_client_rpc(hsk, args.id);
result = -EFAULT;
goto error;
}
finish = get_cycles();
INC_METRIC(send_cycles, finish - start);
} else {
/* This is a response message. */
struct in6_addr canonical_dest;
INC_METRIC(reply_calls, 1);
tt_record4("homa_sendmsg response, id %llu, port %d, pid %d, length %d",
args.id, hsk->port, current->pid, length);
if (args.completion_cookie != 0) {
result = -EINVAL;
goto error;
}
canonical_dest = canonical_ipv6_addr(addr);
rpc = homa_find_server_rpc(hsk, &canonical_dest,
ntohs(addr->in6.sin6_port), args.id);
if (!rpc) {
result = -EINVAL;
goto error;
}
if (rpc->error) {
result = rpc->error;
goto error;
}
if (rpc->state != RPC_IN_SERVICE) {
homa_rpc_unlock(rpc);
rpc = 0;
result = -EINVAL;
goto error;
}
rpc->state = RPC_OUTGOING;
result = homa_message_out_init(rpc, &msg->msg_iter, 1, zc);
if (result)
goto error;
homa_rpc_unlock(rpc);
finish = get_cycles();
INC_METRIC(reply_cycles, finish - start);
}
tt_record1("homa_sendmsg finished, id %d", args.id);
return 0;
error:
if (rpc) {
homa_rpc_free(rpc);
homa_rpc_unlock(rpc);
}
tt_record2("homa_sendmsg returning error %d for id %d",
result, args.id);
tt_freeze();
return result;
}