This repository has been archived by the owner on May 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
homa_utils.c
1658 lines (1579 loc) · 51.1 KB
/
homa_utils.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-2021 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.
*/
/* Copyright (c) 2022-2024, Tianyi Gao, University of Edinburgh
*
* 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 contains miscellaneous utility functions for the Homa protocol. */
#include "homa_impl.h"
#include "homals.h"
/* Core-specific information. NR_CPUS is an overestimate of the actual
* number, but allows us to allocate the array statically.
*/
struct homa_core *homa_cores[NR_CPUS];
/* Points to block of memory holding all homa_cores; used to free it. */
char *core_memory;
struct completion homa_pacer_kthread_done;
/**
* homa_init() - Constructor for homa objects.
* @homa: Object to initialize.
*
* Return: 0 on success, or a negative errno if there was an error. Even
* if an error occurs, it is safe (and necessary) to call
* homa_destroy at some point.
*/
int homa_init(struct homa *homa)
{
size_t aligned_size;
char *first;
int i, err;
_Static_assert(HOMA_MAX_PRIORITIES >= 8,
"homa_init assumes at least 8 priority levels");
/* Initialize core-specific info (if no-one else has already done it),
* making sure that each core has private cache lines.
*/
if (!core_memory) {
aligned_size = (sizeof(struct homa_core) + 0x3f) & ~0x3f;
core_memory = vmalloc(0x3f + (nr_cpu_ids*aligned_size));
if (!core_memory) {
printk(KERN_ERR "Homa couldn't allocate memory "
"for core-specific data\n");
return -ENOMEM;
}
first = (char *) (((__u64) core_memory + 0x3f) & ~0x3f);
for (i = 0; i < nr_cpu_ids; i++) {
struct homa_core *core;
core = (struct homa_core *) (first + i*aligned_size);
homa_cores[i] = core;
core->last_active = 0;
core->last_gro = 0;
atomic_set(&core->softirq_backlog, 0);
core->softirq_offset = 0;
core->held_skb = NULL;
core->held_bucket = 0;
core->thread = NULL;
core->syscall_end_time = 0;
memset(&core->metrics, 0, sizeof(core->metrics));
}
}
homa->pacer_kthread = NULL;
init_completion(&homa_pacer_kthread_done);
atomic64_set(&homa->next_outgoing_id, 2);
atomic64_set(&homa->link_idle_time, get_cycles());
spin_lock_init(&homa->grantable_lock);
INIT_LIST_HEAD(&homa->grantable_peers);
homa->num_grantable_peers = 0;
homa->grant_nonfifo = 0;
homa->grant_nonfifo_left = 0;
spin_lock_init(&homa->pacer_mutex);
homa->pacer_fifo_fraction = 50;
homa->pacer_fifo_count = 1;
spin_lock_init(&homa->throttle_lock);
INIT_LIST_HEAD_RCU(&homa->throttled_rpcs);
homa->throttle_add = 0;
homa->throttle_min_bytes = 1000;
atomic_set(&homa->extra_incoming, 0);
homa->next_client_port = HOMA_MIN_DEFAULT_PORT;
homa_socktab_init(&homa->port_map);
err = homa_peertab_init(&homa->peers);
if (err) {
printk(KERN_ERR "Couldn't initialize peer table (errno %d)\n",
-err);
return err;
}
/* Wild guesses to initialize configuration values... */
homa->rtt_bytes = 10000;
homa->link_mbps = 10000;
homa->poll_usecs = 50;
homa->num_priorities = HOMA_MAX_PRIORITIES;
for (i = 0; i < HOMA_MAX_PRIORITIES; i++)
homa->priority_map[i] = i;
homa->max_sched_prio = HOMA_MAX_PRIORITIES - 5;
homa->unsched_cutoffs[HOMA_MAX_PRIORITIES-1] = 200;
homa->unsched_cutoffs[HOMA_MAX_PRIORITIES-2] = 2800;
homa->unsched_cutoffs[HOMA_MAX_PRIORITIES-3] = 15000;
homa->unsched_cutoffs[HOMA_MAX_PRIORITIES-4] = HOMA_MAX_MESSAGE_LENGTH;
#ifdef __UNIT_TEST__
/* Unit tests won't send CUTOFFS messages unless the test changes
* this variable.
*/
homa->cutoff_version = 0;
#else
homa->cutoff_version = 1;
#endif
homa->grant_increment = 0;
homa->grant_fifo_fraction = 50;
homa->duty_cycle = 800;
homa->grant_threshold = 0;
homa->max_overcommit = 8;
homa->max_incoming = 0;
homa->resend_ticks = 15;
homa->resend_interval = 10;
homa->timeout_resends = 5;
homa->request_ack_ticks = 2;
homa->reap_limit = 10;
homa->dead_buffs_limit = 5000;
homa->max_dead_buffs = 0;
homa->pacer_kthread = kthread_run(homa_pacer_main, homa,
"homa_pacer");
if (IS_ERR(homa->pacer_kthread)) {
err = PTR_ERR(homa->pacer_kthread);
homa->pacer_kthread = NULL;
printk(KERN_ERR "couldn't create homa pacer thread: error %d\n",
err);
return err;
}
homa->pacer_exit = false;
homa->max_nic_queue_ns = 2000;
homa->cycles_per_kbyte = 0;
homa->verbose = 0;
homa->max_gso_size = 10000;
homa->max_gro_skbs = 20;
homa->gro_policy = HOMA_GRO_NORMAL;
homa->gro_busy_usecs = 10;
homa->timer_ticks = 0;
spin_lock_init(&homa->metrics_lock);
homa->metrics = NULL;
homa->metrics_capacity = 0;
homa->metrics_length = 0;
homa->metrics_active_opens = 0;
homa->flags = 0;
homa->freeze_type = 0;
homa->sync_freeze = 0;
homa_outgoing_sysctl_changed(homa);
homa_incoming_sysctl_changed(homa);
return 0;
}
/**
* homa_destroy() - Destructor for homa objects.
* @homa: Object to destroy.
*/
void homa_destroy(struct homa *homa)
{
int i;
if (homa->pacer_kthread) {
homa_pacer_stop(homa);
wait_for_completion(&homa_pacer_kthread_done);
}
/* The order of the following 2 statements matters! */
homa_socktab_destroy(&homa->port_map);
homa_peertab_destroy(&homa->peers);
if (core_memory) {
vfree(core_memory);
core_memory = NULL;
for (i = 0; i < nr_cpu_ids; i++) {
homa_cores[i] = NULL;
}
}
if (homa->metrics)
kfree(homa->metrics);
}
/**
* homa_rpc_new_client() - Allocate and construct a client RPC (one that is used
* to issue an outgoing request). Doesn't send any packets. Invoked with no
* locks held.
* @hsk: Socket to which the RPC belongs.
* @dest: Address of host (ip and port) to which the RPC will be sent.
* @iter: Describes the location(s) of request message data in user space.
*
* Return: A printer to the newly allocated object, or a negative
* errno if an error occurred. The RPC will be locked; the
* caller must eventually unlock it.
*/
struct homa_rpc *homa_rpc_new_client(struct homa_sock *hsk,
struct sockaddr_in *dest, struct iov_iter *iter)
{
int err;
struct homa_rpc *crpc;
struct homa_rpc_bucket *bucket;
struct sk_buff *skb = NULL;
size_t length = iter->count;
crpc = (struct homa_rpc *) kmalloc(sizeof(*crpc), GFP_KERNEL);
if (unlikely(!crpc))
return ERR_PTR(-ENOMEM);
/* Initialize fields that don't require the socket lock. */
crpc->hsk = hsk;
crpc->id = atomic64_fetch_add(2, &hsk->homa->next_outgoing_id);
bucket = homa_client_rpc_bucket(hsk, crpc->id);
crpc->lock = &bucket->lock;
crpc->state = RPC_OUTGOING;
crpc->dont_reap = false;
atomic_set(&crpc->grants_in_progress, 0);
crpc->peer = homa_peer_find(&hsk->homa->peers,
dest->sin_addr.s_addr, &hsk->inet);
if (unlikely(IS_ERR(crpc->peer))) {
tt_record("error in homa_peer_find");
err = PTR_ERR(crpc->peer);
goto error;
}
crpc->dport = ntohs(dest->sin_port);
crpc->homals_ctx = homals_get_ctx_hash(hsk, crpc->peer->addr, dest->sin_port);
crpc->error = 0;
crpc->msgin.total_length = -1;
crpc->msgin.num_skbs = 0;
tt_record("homa_rpc_new_client call homa_fill_packets"); // debug
skb = homa_fill_packets(hsk, crpc->peer, iter, crpc);
if (IS_ERR(skb)) {
err = PTR_ERR(skb);
tt_record1("error in homa_fill_packets: %d", err);
skb = NULL;
goto error;
}
homa_message_out_init(crpc, hsk->port, skb, length);
INIT_LIST_HEAD(&crpc->dead_links);
crpc->interest = NULL;
INIT_LIST_HEAD(&crpc->ready_links);
INIT_LIST_HEAD(&crpc->grantable_links);
INIT_LIST_HEAD(&crpc->throttled_links);
crpc->silent_ticks = 0;
crpc->resend_timer_ticks = hsk->homa->timer_ticks;
crpc->done_timer_ticks = 0;
crpc->magic = HOMA_RPC_MAGIC;
crpc->start_cycles = get_cycles();
/* Initialize fields that require locking. This allows the most
* expensive work, such as copying in the message from user space,
* to be performed without holding locks. Also, can't hold spin
* locks while doing things that could block, such as memory allocation.
*/
homa_bucket_lock(bucket, client);
homa_sock_lock(hsk, "homa_rpc_new_client");
if (hsk->shutdown) {
homa_sock_unlock(hsk);
homa_rpc_unlock(crpc);
err = -ESHUTDOWN;
goto error;
}
hlist_add_head(&crpc->hash_links, &bucket->rpcs);
list_add_tail_rcu(&crpc->active_links, &hsk->active_rpcs);
homa_sock_unlock(hsk);
return crpc;
error:
homa_free_skbs(skb);
kfree(crpc);
return ERR_PTR(err);
}
/**
* homa_rpc_new_server() - Allocate and construct a server RPC (one that is
* used to manage an incoming request).
* @hsk: Socket that owns this RPC.
* @source: IP address (network byte order) of the RPC's client.
* @h: Header for the first data packet received for this RPC; used
* to initialize the RPC.
*
* Return: A pointer to a new RPC, which is locked, or a negative errno
* if an error occurred. If there is already an RPC corresponding
* to h, then it is returned instead of creating a new RPC.
* If a new RPC is created, it is not yet linked into
* @hsk->active_rpcs.
*/
struct homa_rpc *homa_rpc_new_server(struct homa_sock *hsk,
__be32 source, struct data_header *h)
{
int err;
struct homa_rpc *srpc;
__u64 id = homa_local_id(h->common.sender_id);
struct homa_rpc_bucket *bucket = homa_server_rpc_bucket(hsk, id);
/* Lock the bucket, and make sure no-one else has already created
* the desired RPC.
*/
homa_bucket_lock(bucket, server);
hlist_for_each_entry_rcu(srpc, &bucket->rpcs, hash_links) {
if ((srpc->id == id) &&
(srpc->dport == ntohs(h->common.sport)) &&
(srpc->peer->addr == source)) {
/* RPC already exists; just return it instead
* of creating a new RPC.
*/
return srpc;
}
}
/* Initialize fields that don't require the socket lock. */
srpc = (struct homa_rpc *) kmalloc(sizeof(*srpc), GFP_KERNEL);
if (!srpc) {
err = -ENOMEM;
goto error;
}
srpc->hsk = hsk;
srpc->lock = &bucket->lock;
srpc->state = RPC_INCOMING;
srpc->dont_reap = false;
atomic_set(&srpc->grants_in_progress, 0);
srpc->peer = homa_peer_find(&hsk->homa->peers, source, &hsk->inet);
if (unlikely(IS_ERR(srpc->peer))) {
err = PTR_ERR(srpc->peer);
kfree(srpc);
goto error;
}
srpc->dport = ntohs(h->common.sport);
srpc->homals_ctx = homals_get_ctx_hash(hsk, source, h->common.sport);
srpc->id = id;
srpc->error = 0;
homa_message_in_init(&srpc->msgin, ntohl(h->message_length),
ntohl(h->incoming));
srpc->msgout.length = -1;
srpc->msgout.num_skbs = 0;
srpc->active_links.next = LIST_POISON1;
srpc->interest = NULL;
INIT_LIST_HEAD(&srpc->ready_links);
INIT_LIST_HEAD(&srpc->grantable_links);
INIT_LIST_HEAD(&srpc->throttled_links);
srpc->silent_ticks = 0;
srpc->resend_timer_ticks = hsk->homa->timer_ticks;
srpc->done_timer_ticks = 0;
srpc->magic = HOMA_RPC_MAGIC;
srpc->start_cycles = get_cycles();
hlist_add_head(&srpc->hash_links, &bucket->rpcs);
return srpc;
error:
spin_unlock_bh(&bucket->lock);
return ERR_PTR(err);
}
/**
* homa_rpc_lock_slow() - This function implements the slow path for
* acquiring an RPC lock. It is invoked when an RPC lock isn't immediately
* available. It waits for the lock, but also records statistics about
* the waiting time.
* @rpc: RPC to lock.
*/
void homa_rpc_lock_slow(struct homa_rpc *rpc)
{
__u64 start = get_cycles();
spin_lock_bh(rpc->lock);
if (homa_is_client(rpc->id)) {
INC_METRIC(client_lock_misses, 1);
INC_METRIC(client_lock_miss_cycles, get_cycles() - start);
} else {
INC_METRIC(server_lock_misses, 1);
INC_METRIC(server_lock_miss_cycles, get_cycles() - start);
}
}
/**
* homa_rpc_acked() - This function is invoked when an ack is received
* for an RPC; if the RPC still exists, is freed.
* @hsk: Socket on which the ack was received. May or may not correspond
* to the RPC, but can sometimes be used to avoid a socket lookup.
* @saddr: Source address from which the act was received (the client
* note for the RPC)
* @ack: Information about an RPC from @saddr that may now be deleted safely.
*/
void homa_rpc_acked(struct homa_sock *hsk, __be32 saddr, struct homa_ack *ack)
{
struct homa_rpc *rpc;
struct homa_sock *hsk2 = hsk;
__u64 id = homa_local_id(ack->client_id);
__u16 client_port = ntohs(ack->client_port);
__u16 server_port = ntohs(ack->server_port);
if (hsk2->port != server_port) {
/* Without RCU, sockets other than hsk can be deleted
* out from under us.
*/
rcu_read_lock();
hsk2 = homa_sock_find(&hsk->homa->port_map, server_port);
if (!hsk2)
goto done;
}
rpc = homa_find_server_rpc(hsk2, saddr, client_port, id);
if (rpc) {
homa_rpc_free(rpc);
homa_rpc_unlock(rpc);
}
done:
if (hsk->port != server_port)
rcu_read_unlock();
}
/**
* homa_rpc_free() - Destructor for homa_rpc; will arrange for all resources
* associated with the RPC to be released (eventually).
* @rpc: Structure to clean up, or NULL. Must be locked. Its socket must
* not be locked.
*/
void homa_rpc_free(struct homa_rpc *rpc)
{
if (!rpc || (rpc->state == RPC_DEAD))
return;
/* Before doing anything else, unlink the input message from
* homa->grantable_msgs. This will synchronize to ensure that
* homa_manage_grants doesn't access this RPC after destruction
* begins.
*/
rpc->state = RPC_DEAD;
homa_remove_from_grantable(rpc->hsk->homa, rpc);
/* Unlink from all lists, so no-one will ever find this RPC again. */
homa_sock_lock(rpc->hsk, "homa_rpc_free");
__hlist_del(&rpc->hash_links);
list_del_rcu(&rpc->active_links);
__list_del_entry(&rpc->ready_links);
if (rpc->interest != NULL) {
rpc->interest->reg_rpc = NULL;
wake_up_process(rpc->interest->thread);
rpc->interest = NULL;
}
list_add_tail_rcu(&rpc->dead_links, &rpc->hsk->dead_rpcs);
rpc->hsk->dead_skbs += rpc->msgin.num_skbs + rpc->msgout.num_skbs;
if (rpc->hsk->dead_skbs > rpc->hsk->homa->max_dead_buffs)
/* This update isn't thread-safe, but it's just a
* statistic so it's OK if updates occasionally get missed.
*/
rpc->hsk->homa->max_dead_buffs = rpc->hsk->dead_skbs;
// tt_record3("Freeing rpc id %d, socket %d, dead_skbs %d", rpc->id,
// rpc->hsk->client_port,
// rpc->hsk->dead_skbs);
homa_sock_unlock(rpc->hsk);
homa_remove_from_throttled(rpc);
}
/**
* homa_rpc_reap() - Invoked to release resources associated with dead
* RPCs for a given socket. For a large RPC, it can take a long time to
* free all of its packet buffers, so we try to perform this work
* off the critical path where it won't delay applications. Each call to
* this function does a small chunk of work. See the file reap.txt for
* more information.
* @hsk: Homa socket that may contain dead RPCs. Must not be locked by the
* caller; this function will lock and release.
* @count: Number of buffers to free during this call.
*
* Return: A return value of 0 means that we ran out of work to do; calling
* again will do no work (there could be unreaped RPCs, but if so,
* reaping has been disabled for them). A value greater than
* zero means there is still more reaping work to be done.
*/
int homa_rpc_reap(struct homa_sock *hsk, int count)
{
#ifdef __UNIT_TEST__
#define BATCH_MAX 3
#else
#define BATCH_MAX 20
#endif
struct sk_buff *skbs[BATCH_MAX];
struct homa_rpc *rpcs[BATCH_MAX];
int num_skbs, num_rpcs;
struct homa_rpc *rpc;
int i, batch_size;
int result;
INC_METRIC(reaper_calls, 1);
INC_METRIC(reaper_dead_skbs, hsk->dead_skbs);
/* Each iteration through the following loop will reap
* BATCH_MAX skbs.
*/
while (count > 0) {
batch_size = count;
if (batch_size > BATCH_MAX)
batch_size = BATCH_MAX;
count -= batch_size;
num_skbs = num_rpcs = 0;
homa_sock_lock(hsk, "homa_rpc_reap");
if (atomic_read(&hsk->protect_count)) {
INC_METRIC(disabled_reaps, 1);
tt_record2("homa_rpc_reap returning: protect_count "
"%d, dead_skbs %d",
atomic_read(&hsk->protect_count),
hsk->dead_skbs);
homa_sock_unlock(hsk);
return 0;
}
/* Collect buffers and freeable RPCs. */
list_for_each_entry_rcu(rpc, &hsk->dead_rpcs, dead_links) {
if (rpc->dont_reap || (atomic_read(
&rpc->grants_in_progress) != 0)) {
INC_METRIC(disabled_rpc_reaps, 1);
continue;
}
rpc->magic = 0;
if (rpc->msgout.length >= 0) {
while (rpc->msgout.packets) {
skbs[num_skbs] = rpc->msgout.packets;
rpc->msgout.packets = *homa_next_skb(
rpc->msgout.packets);
num_skbs++;
rpc->msgout.num_skbs--;
if (num_skbs >= batch_size)
goto release;
}
}
i = 0;
if (rpc->msgin.total_length >= 0) {
while (1) {
struct sk_buff *skb;
skb = skb_dequeue(&rpc->msgin.packets);
if (!skb)
break;
skbs[num_skbs] = skb;
num_skbs++;
rpc->msgin.num_skbs--;
if (num_skbs >= batch_size)
goto release;
}
}
/* If we get here, it means all packets have been
* removed from the RPC.
*/
rpcs[num_rpcs] = rpc;
num_rpcs++;
list_del_rcu(&rpc->dead_links);
if (num_rpcs >= batch_size)
goto release;
}
/* Free all of the collected resources; release the socket
* lock while doing this.
*/
release:
hsk->dead_skbs -= num_skbs;
result = !list_empty(&hsk->dead_rpcs)
&& ((num_skbs + num_rpcs) != 0);
homa_sock_unlock(hsk);
for (i = 0; i < num_skbs; i++) {
kfree_skb(skbs[i]);
}
for (i = 0; i < num_rpcs; i++) {
UNIT_LOG("; ", "reaped %llu", rpcs[i]->id);
/* Lock and unlock the RPC before freeing it. This
* is needed to deal with races where the last user
* of the RPC (such as homa_ioc_reply) hasn't
* unlocked it yet.
*/
homa_rpc_lock(rpcs[i]);
homa_rpc_unlock(rpcs[i]);
rpcs[i]->state = 0;
kfree(rpcs[i]);
}
tt_record4("reaped %d skbs, %d rpcs; %d skbs remain for port %d",
num_skbs, num_rpcs, hsk->dead_skbs, hsk->port);
if (!result)
break;
}
return result;
}
/**
* homa_rpc_send_offset() - Return the offset of the first unsent byte of a
* message.
* @rpc: RPC whose msgout will be examined.
*
* Return: The first unsent byte for rpc->msgout (0 if the msgout
* hasn't been initialized, message length if all bytes have been
* sent).
*/
int homa_rpc_send_offset(struct homa_rpc *rpc)
{
struct sk_buff *pkt = rpc->msgout.next_packet;
if (rpc->msgout.length < 0)
return 0;
if (!pkt)
return rpc->msgout.length;
return homa_data_offset(pkt);
}
/**
* homa_find_client_rpc() - Locate client-side information about the RPC that
* a packet belongs to, if there is any. Thread-safe without socket lock.
* @hsk: Socket via which packet was received.
* @id: Unique identifier for the RPC.
*
* Return: A pointer to the homa_rpc for this id, or NULL if none.
* The RPC will be locked; the caller must eventually unlock it
* by invoking homa_unlock_client_rpc.
*/
struct homa_rpc *homa_find_client_rpc(struct homa_sock *hsk, __u64 id)
{
struct homa_rpc *crpc;
struct homa_rpc_bucket *bucket = homa_client_rpc_bucket(hsk, id);
homa_bucket_lock(bucket, client);
hlist_for_each_entry_rcu(crpc, &bucket->rpcs, hash_links) {
if (crpc->id == id) {
return crpc;
}
}
spin_unlock_bh(&bucket->lock);
return NULL;
}
/**
* homa_find_server_rpc() - Locate server-side information about the RPC that
* a packet belongs to, if there is any. Thread-safe without socket lock.
* @hsk: Socket via which packet was received.
* @saddr: Address from which the packet was sent.
* @sport: Port at @saddr from which the packet was sent.
* @id: Unique identifier for the RPC (must have server bit set).
*
* Return: A pointer to the homa_rpc matching the arguments, or NULL
* if none. The RPC will be locked; the caller must eventually
* unlock it by invoking homa_unlock_server_rpc.
*/
struct homa_rpc *homa_find_server_rpc(struct homa_sock *hsk,
__be32 saddr, __u16 sport, __u64 id)
{
struct homa_rpc *srpc;
struct homa_rpc_bucket *bucket = homa_server_rpc_bucket(hsk, id);
homa_bucket_lock(bucket, server);
hlist_for_each_entry_rcu(srpc, &bucket->rpcs, hash_links) {
if ((srpc->id == id) && (srpc->dport == sport) &&
(srpc->peer->addr == saddr)) {
return srpc;
}
}
spin_unlock_bh(&bucket->lock);
return NULL;
}
/**
* homa_rpc_log() - Log info about a particular RPC; this is functionality
* pulled out of homa_rpc_log_active because its indentation got too deep.
* @rpc: RPC for which key info should be written to the system log.
*/
void homa_rpc_log(struct homa_rpc *rpc)
{
char *type = homa_is_client(rpc->id) ? "Client" : "Server";
char *peer = homa_print_ipv4_addr(rpc->peer->addr);
if (rpc->state == RPC_INCOMING)
printk(KERN_NOTICE "%s RPC INCOMING, id %llu, peer %s:%d, "
"%d/%d bytes received, incoming %d\n",
type, rpc->id, peer, rpc->dport,
rpc->msgin.total_length
- rpc->msgin.bytes_remaining,
rpc->msgin.total_length, rpc->msgin.incoming);
else if (rpc->state == RPC_OUTGOING) {
int offset = homa_rpc_send_offset(rpc);
printk(KERN_NOTICE "%s RPC OUTGOING, id %llu, peer %s:%d, "
"out length %d, left %d, granted %d, "
"in left %d, resend_ticks %u, silent_ticks %d\n",
type, rpc->id, peer, rpc->dport,
rpc->msgout.length,
rpc->msgout.length - offset,
rpc->msgout.granted,
rpc->msgin.bytes_remaining,
rpc->resend_timer_ticks,
rpc->silent_ticks);
} else {
char *queued = "";
if ((rpc->state == RPC_READY) && list_empty(&rpc->ready_links))
queued = " (not queued)";
printk(KERN_NOTICE "%s RPC %s%s, id %llu, peer %s:%d, "
"incoming length %d, outgoing length %d\n",
type, homa_symbol_for_state(rpc), queued,
rpc->id, peer, rpc->dport,
rpc->msgin.total_length, rpc->msgout.length);
}
}
/**
* homa_rpc_log_active() - Print information to the system log about all
* active RPCs. Intended primarily for debugging.
* @homa: Overall data about the Homa protocol implementation.
* @id: An RPC id: if nonzero, then only RPCs with this id will be
* logged.
*/
void homa_rpc_log_active(struct homa *homa, uint64_t id)
{
struct homa_socktab_scan scan;
struct homa_sock *hsk;
struct homa_rpc *rpc;
int count = 0;
printk("Logging active Homa RPCs:\n");
rcu_read_lock();
for (hsk = homa_socktab_start_scan(&homa->port_map, &scan);
hsk != NULL; hsk = homa_socktab_next(&scan)) {
if (list_empty(&hsk->active_rpcs) || hsk->shutdown)
continue;
if (!homa_protect_rpcs(hsk))
continue;
list_for_each_entry_rcu(rpc, &hsk->active_rpcs, active_links) {
count++;
if ((id != 0) && (id != rpc->id))
continue;
homa_rpc_log(rpc);
}
homa_unprotect_rpcs(hsk);
}
rcu_read_unlock();
printk("Finished logging active Homa RPCs: %d active RPCs\n", count);
}
/**
* homa_print_ipv4_addr() - Convert an IPV4 address to the standard string
* representation.
* @addr: Address to convert, in network byte order.
*
* Return: The converted value. Values are stored in static memory, so
* the caller need not free. This also means that storage is
* eventually reused (there are enough buffers to accommodate
* multiple "active" values).
*
* Note: Homa uses this function, rather than the %pI4 format specifier
* for snprintf et al., because the kernel's version of snprintf isn't
* available in Homa's unit test environment.
*/
char *homa_print_ipv4_addr(__be32 addr)
{
#define NUM_BUFS 4
#define BUF_SIZE 30
static char buffers[NUM_BUFS][BUF_SIZE];
static int next_buf = 0;
__u32 a2 = ntohl(addr);
char *buffer = buffers[next_buf];
next_buf++;
if (next_buf >= NUM_BUFS)
next_buf = 0;
snprintf(buffer, BUF_SIZE, "%u.%u.%u.%u", (a2 >> 24) & 0xff,
(a2 >> 16) & 0xff, (a2 >> 8) & 0xff, a2 & 0xff);
return buffer;
}
/**
* homa_print_packet() - Print a human-readable string describing the
* information in a Homa packet.
* @skb: Packet whose information should be printed.
* @buffer: Buffer in which to generate the string.
* @buf_len: Number of bytes available at @buffer.
*
* Return: @buffer
*/
char *homa_print_packet(struct sk_buff *skb, char *buffer, int buf_len)
{
int used = 0;
struct common_header *common = (struct common_header *) skb->data;
used = homa_snprintf(buffer, buf_len, used,
"%s from %s:%u, dport %d, id %llu",
homa_symbol_for_type(common->type),
homa_print_ipv4_addr(ip_hdr(skb)->saddr),
ntohs(common->sport), ntohs(common->dport),
be64_to_cpu(common->sender_id));
switch (common->type) {
case DATA: {
struct data_header *h = (struct data_header *)
skb->data;
struct data_segment *seg;
int seg_length = ntohl(h->seg.segment_length);
int bytes_left, i;
used = homa_snprintf(buffer, buf_len, used,
", message_length %d, offset %d, "
"data_length %d, incoming %d",
ntohl(h->message_length),
ntohl(h->seg.offset), seg_length,
ntohl(h->incoming));
if (ntohs(h->cutoff_version != 0))
used = homa_snprintf(buffer, buf_len, used,
", cutoff_version %d",
ntohs(h->cutoff_version));
if (h->retransmit)
used = homa_snprintf(buffer, buf_len, used,
", RETRANSMIT");
bytes_left = skb->len - sizeof32(*h) - seg_length;
if (skb_shinfo(skb)->gso_segs <= 1)
break;
used = homa_snprintf(buffer, buf_len, used, ", extra segs");
for (i = skb_shinfo(skb)->gso_segs - 1; i > 0; i--) {
seg = (struct data_segment *) (skb->data + skb->len
- bytes_left);
seg_length = ntohl(seg->segment_length);
used = homa_snprintf(buffer, buf_len, used,
" %d@%d", seg_length,
ntohl(seg->offset));
bytes_left -= sizeof32(*seg) + seg_length;
};
break;
}
case GRANT: {
struct grant_header *h = (struct grant_header *) skb->data;
used = homa_snprintf(buffer, buf_len, used,
", offset %d, grant_prio %u",
ntohl(h->offset), h->priority);
break;
}
case RESEND: {
struct resend_header *h = (struct resend_header *) skb->data;
used = homa_snprintf(buffer, buf_len, used,
", offset %d, length %d, resend_prio %u",
ntohl(h->offset), ntohl(h->length),
h->priority);
break;
}
case UNKNOWN:
/* Nothing to add here. */
break;
case BUSY:
/* Nothing to add here. */
break;
case CUTOFFS: {
struct cutoffs_header *h = (struct cutoffs_header *) skb->data;
used = homa_snprintf(buffer, buf_len, used,
", cutoffs %d %d %d %d %d %d %d %d, version %u",
ntohl(h->unsched_cutoffs[0]),
ntohl(h->unsched_cutoffs[1]),
ntohl(h->unsched_cutoffs[2]),
ntohl(h->unsched_cutoffs[3]),
ntohl(h->unsched_cutoffs[4]),
ntohl(h->unsched_cutoffs[5]),
ntohl(h->unsched_cutoffs[6]),
ntohl(h->unsched_cutoffs[7]),
ntohs(h->cutoff_version));
break;
}
case FREEZE:
/* Nothing to add here. */
break;
case NEED_ACK:
/* Nothing to add here. */
break;
case ACK: {
struct ack_header *h = (struct ack_header *) skb->data;
int i, count;
count = ntohs(h->num_acks);
used = homa_snprintf(buffer, buf_len, used, ", acks");
for (i = 0; i < count; i++) {
used = homa_snprintf(buffer, buf_len, used,
" [cp %d, sp %d, id %llu]",
ntohs(h->acks[i].client_port),
ntohs(h->acks[i].server_port),
be64_to_cpu(h->acks[i].client_id));
}
break;
}
}
buffer[buf_len-1] = 0;
return buffer;
}
/**
* homa_print_packet_short() - Print a human-readable string describing the
* information in a Homa packet. This function generates a shorter
* description than homa_print_packet.
* @skb: Packet whose information should be printed.
* @buffer: Buffer in which to generate the string.
* @buf_len: Number of bytes available at @buffer.
*
* Return: @buffer
*/
char *homa_print_packet_short(struct sk_buff *skb, char *buffer, int buf_len)
{
struct common_header *common =
(struct common_header *) skb_transport_header(skb);
switch (common->type) {
case DATA: {
struct data_header *h = (struct data_header *) common;
struct data_segment *seg;
int bytes_left, used, i;
int seg_length = ntohl(h->seg.segment_length);
used = homa_snprintf(buffer, buf_len, 0, "DATA%s %d@%d",
h->retransmit ? " retrans" : "",
seg_length, ntohl(h->seg.offset));
bytes_left = skb->len - sizeof32(*h) - seg_length;
for (i = skb_shinfo(skb)->gso_segs - 1; i > 0; i--) {
seg = (struct data_segment *) (skb->data + skb->len
- bytes_left);
seg_length = ntohl(seg->segment_length);
used = homa_snprintf(buffer, buf_len, used,
" %d@%d", seg_length,
ntohl(seg->offset));
bytes_left -= sizeof32(*seg) + seg_length;
}
break;
}
case GRANT: {
struct grant_header *h = (struct grant_header *) common;
snprintf(buffer, buf_len, "GRANT %d@%d", ntohl(h->offset),
h->priority);
break;
}
case RESEND: {
struct resend_header *h = (struct resend_header *) common;
snprintf(buffer, buf_len, "RESEND %d-%d@%d", ntohl(h->offset),
ntohl(h->offset) + ntohl(h->length) - 1,
h->priority);
break;
}
case UNKNOWN:
snprintf(buffer, buf_len, "UNKNOWN");
break;
case BUSY:
snprintf(buffer, buf_len, "BUSY");
break;
case CUTOFFS:
snprintf(buffer, buf_len, "CUTOFFS");
break;
case FREEZE:
snprintf(buffer, buf_len, "FREEZE");
break;
case NEED_ACK:
snprintf(buffer, buf_len, "NEED_ACK");
break;
break;
case ACK:
snprintf(buffer, buf_len, "ACK");
break;
default:
snprintf(buffer, buf_len, "unknown packet type %d",
common->type);
break;
}
return buffer;
}
/**
* homa_snprintf() - This function makes it easy to use a series of calls
* to snprintf to gradually append information to a fixed-size buffer.
* If the buffer fills, the function can continue to be called, but nothing
* more will get added to the buffer.
* @buffer: Characters accumulate here.
* @size: Total space available in @buffer.
* @used: Number of bytes currently occupied in the buffer, not including
* a terminating null character; this is typically the result of
* the previous call to this function.
* @format: Format string suitable for passing to printf-like functions,
* followed by values for the various substitutions requested
* in @format
* @ ...
*
* Return: The number of characters now occupied in @buffer, not
* including the terminating null character.
*/
int homa_snprintf(char *buffer, int size, int used, const char* format, ...)
{
int new_chars;