-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathhoma_peer.c
More file actions
739 lines (678 loc) · 21.9 KB
/
homa_peer.c
File metadata and controls
739 lines (678 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
// SPDX-License-Identifier: BSD-2-Clause or GPL-2.0+
/* This file provides functions related to homa_peer and homa_peertab
* objects.
*/
#include "homa_impl.h"
#include "homa_peer.h"
#include "homa_rpc.h"
#include "murmurhash3.h"
#ifdef __UNIT_TEST__
#undef rhashtable_init
#define rhashtable_init mock_rht_init
#undef rhashtable_lookup_get_insert_fast
#define rhashtable_lookup_get_insert_fast mock_rht_lookup_get_insert_fast
#undef rhashtable_walk_next
#define rhashtable_walk_next mock_rht_walk_next
#endif /* __UNIT_TEST__ */
static const struct rhashtable_params ht_params = {
.key_len = sizeof(struct homa_peer_key),
.key_offset = offsetof(struct homa_peer, ht_key),
.head_offset = offsetof(struct homa_peer, ht_linkage),
.nelem_hint = 10000,
.hashfn = murmurhash3,
.obj_cmpfn = homa_peer_compare
};
#ifndef __STRIP__ /* See strip.py */
/* Used to enable sysctl access to peertab-specific configuration parameters.
* The @data fields are actually offsets within a struct homa_peertab; these
* are converted to pointers into a struct peertab later.
*/
#define OFFSET(field) ((void *)offsetof(struct homa_peertab, field))
static struct ctl_table peer_ctl_table[] = {
{
.procname = "peer_gc_threshold",
.data = OFFSET(gc_threshold),
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_peer_dointvec
},
{
.procname = "peer_idle_secs_min",
.data = OFFSET(idle_secs_min),
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_peer_dointvec
},
{
.procname = "peer_idle_secs_max",
.data = OFFSET(idle_secs_max),
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_peer_dointvec
},
{
.procname = "peer_net_max",
.data = OFFSET(net_max),
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = homa_peer_dointvec
},
};
#endif /* See strip.py */
/**
* homa_peer_alloc_peertab() - Allocate and initialize a homa_peertab.
*
* Return: A pointer to the new homa_peertab, or ERR_PTR(-errno) if there
* was a problem.
*/
struct homa_peertab *homa_peer_alloc_peertab(void)
{
struct homa_peertab *peertab;
int err;
peertab = kzalloc(sizeof(*peertab), GFP_KERNEL);
if (!peertab)
return ERR_PTR(-ENOMEM);
spin_lock_init(&peertab->lock);
err = rhashtable_init(&peertab->ht, &ht_params);
if (err) {
kfree(peertab);
return ERR_PTR(err);
}
peertab->ht_valid = true;
rhashtable_walk_enter(&peertab->ht, &peertab->ht_iter);
peertab->gc_threshold = 5000;
peertab->net_max = 10000;
peertab->idle_secs_min = 10;
peertab->idle_secs_max = 120;
#ifndef __STRIP__ /* See strip.py */
peertab->sysctl_header = register_net_sysctl(&init_net, "net/homa",
peer_ctl_table);
if (!peertab->sysctl_header) {
err = -ENOMEM;
pr_err("couldn't register sysctl parameters for Homa peertab\n");
goto error;
}
#endif /* See strip.py */
homa_peer_update_sysctl_deps(peertab);
return peertab;
#ifndef __STRIP__ /* See strip.py */
error:
homa_peer_free_peertab(peertab);
return ERR_PTR(err);
#endif /* See strip.py */
}
/**
* homa_peer_free_net() - Garbage collect all of the peer information
* associated with a particular network namespace.
* @hnet: Network namespace whose peers should be freed. There must not
* be any active sockets or RPCs for this namespace.
*/
void homa_peer_free_net(struct homa_net *hnet)
{
struct homa_peertab *peertab = hnet->homa->peertab;
struct rhashtable_iter iter;
struct homa_peer *peer;
spin_lock_bh(&peertab->lock);
rhashtable_walk_enter(&peertab->ht, &iter);
rhashtable_walk_start(&iter);
while (1) {
peer = rhashtable_walk_next(&iter);
if (!peer)
break;
if (IS_ERR(peer))
continue;
if (peer->ht_key.hnet != hnet)
continue;
if (rhashtable_remove_fast(&peertab->ht, &peer->ht_linkage,
ht_params) == 0) {
homa_peer_release(peer);
hnet->num_peers--;
peertab->num_peers--;
}
}
rhashtable_walk_stop(&iter);
rhashtable_walk_exit(&iter);
WARN(hnet->num_peers != 0, "%s ended up with hnet->num_peers %d",
__func__, hnet->num_peers);
spin_unlock_bh(&peertab->lock);
}
/**
* homa_peer_release_fn() - This function is invoked for each entry in
* the peer hash table by the rhashtable code when the table is being
* deleted. It frees its argument.
* @object: homa_peer to free.
* @dummy: Not used.
*/
void homa_peer_release_fn(void *object, void *dummy)
{
struct homa_peer *peer = object;
homa_peer_release(peer);
}
/**
* homa_peer_free_peertab() - Destructor for homa_peertabs.
* @peertab: The table to destroy. Caller must ensure that it will never
* be accessed again.
*/
void homa_peer_free_peertab(struct homa_peertab *peertab)
{
if (peertab->ht_valid) {
rhashtable_walk_exit(&peertab->ht_iter);
rhashtable_free_and_destroy(&peertab->ht, homa_peer_release_fn,
NULL);
}
#ifndef __STRIP__ /* See strip.py */
if (peertab->sysctl_header) {
unregister_net_sysctl_table(peertab->sysctl_header);
peertab->sysctl_header = NULL;
}
#endif /* See strip.py */
kfree(peertab);
}
/**
* homa_peer_prefer_evict() - Given two peers, determine which one is
* a better candidate for eviction.
* @peertab: Overall information used to manage peers.
* @peer1: First peer.
* @peer2: Second peer.
* Return: True if @peer1 is a better candidate for eviction than @peer2.
*/
int homa_peer_prefer_evict(struct homa_peertab *peertab,
struct homa_peer *peer1,
struct homa_peer *peer2)
{
/* Prefer a peer whose homa-net is over its limit; if both are either
* over or under, then prefer the peer with the longest idle time.
*/
if (peer1->ht_key.hnet->num_peers > peertab->net_max) {
if (peer2->ht_key.hnet->num_peers <= peertab->net_max)
return true;
else
return peer1->access_jiffies < peer2->access_jiffies;
}
if (peer2->ht_key.hnet->num_peers > peertab->net_max)
return false;
else
return peer1->access_jiffies < peer2->access_jiffies;
}
/**
* homa_peer_pick_victims() - Select a few peers that can be freed.
* @peertab: Choose peers that are stored here.
* @victims: Return addresses of victims here.
* @max_victims: Limit on how many victims to choose (and size of @victims
* array).
* Return: The number of peers stored in @victims; may be zero.
*/
int homa_peer_pick_victims(struct homa_peertab *peertab,
struct homa_peer *victims[], int max_victims)
{
struct homa_peer *peer;
int num_victims = 0;
int to_scan;
int i, idle;
/* Scan 2 peers for every potential victim and keep the "best"
* peers for removal.
*/
rhashtable_walk_start(&peertab->ht_iter);
for (to_scan = 2 * max_victims; to_scan > 0; to_scan--) {
peer = rhashtable_walk_next(&peertab->ht_iter);
if (!peer) {
/* Reached the end of the table; restart at
* the beginning.
*/
rhashtable_walk_stop(&peertab->ht_iter);
rhashtable_walk_exit(&peertab->ht_iter);
rhashtable_walk_enter(&peertab->ht, &peertab->ht_iter);
rhashtable_walk_start(&peertab->ht_iter);
peer = rhashtable_walk_next(&peertab->ht_iter);
if (!peer)
break;
}
if (IS_ERR(peer)) {
/* rhashtable decided to restart the search at the
* beginning.
*/
peer = rhashtable_walk_next(&peertab->ht_iter);
if (!peer || IS_ERR(peer))
break;
}
/* Has this peer been idle long enough to be candidate for
* eviction?
*/
idle = jiffies - peer->access_jiffies;
if (idle < peertab->idle_jiffies_min)
continue;
if (idle < peertab->idle_jiffies_max &&
peer->ht_key.hnet->num_peers <= peertab->net_max)
continue;
/* Sort the candidate into the existing list of victims. */
for (i = 0; i < num_victims; i++) {
if (peer == victims[i]) {
/* This can happen if there aren't very many
* peers and we wrapped around in the hash
* table.
*/
peer = NULL;
break;
}
if (homa_peer_prefer_evict(peertab, peer, victims[i])) {
struct homa_peer *tmp;
tmp = victims[i];
victims[i] = peer;
peer = tmp;
}
}
if (num_victims < max_victims && peer) {
victims[num_victims] = peer;
num_victims++;
}
}
rhashtable_walk_stop(&peertab->ht_iter);
return num_victims;
}
/**
* homa_peer_gc() - This function is invoked by Homa at regular intervals;
* its job is to ensure that the number of peers stays within limits.
* If the number grows too large, it selectively deletes peers to get
* back under the limit.
* @peertab: Structure whose peers should be considered for garbage
* collection.
*/
void homa_peer_gc(struct homa_peertab *peertab)
{
#define EVICT_BATCH_SIZE 5
struct homa_peer *victims[EVICT_BATCH_SIZE];
int num_victims;
int i;
spin_lock_bh(&peertab->lock);
if (peertab->num_peers < peertab->gc_threshold)
goto done;
num_victims = homa_peer_pick_victims(peertab, victims,
EVICT_BATCH_SIZE);
if (num_victims == 0)
goto done;
for (i = 0; i < num_victims; i++) {
struct homa_peer *peer = victims[i];
if (rhashtable_remove_fast(&peertab->ht, &peer->ht_linkage,
ht_params) == 0) {
homa_peer_release(peer);
peertab->num_peers--;
peer->ht_key.hnet->num_peers--;
tt_record1("homa_peer_gc removed homa_peer 0x%x",
tt_addr(peer->addr));
}
}
done:
spin_unlock_bh(&peertab->lock);
}
/**
* homa_peer_alloc() - Allocate and initialize a new homa_peer object.
* @hsk: Socket for which the peer will be used.
* @addr: Address of the desired host: IPv4 addresses are represented
* as IPv4-mapped IPv6 addresses.
* Return: The peer associated with @addr, or a negative errno if an
* error occurred. On a successful return the reference count
* will be incremented for the returned peer. Sets hsk->error_msg
* on errors.
*/
struct homa_peer *homa_peer_alloc(struct homa_sock *hsk,
const struct in6_addr *addr)
{
struct homa_peer *peer;
int status;
peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
if (!peer) {
INC_METRIC(peer_kmalloc_errors, 1);
hsk->error_msg = "couldn't allocate memory for homa_peer";
return (struct homa_peer *)ERR_PTR(-ENOMEM);
}
peer->ht_key.addr = *addr;
peer->ht_key.hnet = hsk->hnet;
refcount_set(&peer->refs, 1);
peer->access_jiffies = jiffies;
spin_lock_init(&peer->lock);
#ifndef __STRIP__ /* See strip.py */
peer->unsched_cutoffs[HOMA_MAX_PRIORITIES - 1] = 0;
peer->unsched_cutoffs[HOMA_MAX_PRIORITIES - 2] = INT_MAX;
INIT_LIST_HEAD(&peer->grantable_rpcs);
INIT_LIST_HEAD(&peer->grantable_links);
#endif /* See strip.py */
peer->current_ticks = -1;
status = homa_peer_reset_dst(peer, hsk);
if (status != 0) {
hsk->error_msg = "couldn't find route for peer";
kfree(peer);
return ERR_PTR(status);
}
tt_record1("Allocated new homa_peer for node 0x%x",
tt_addr(peer->addr));
INC_METRIC(peer_allocs, 1);
return peer;
}
/**
* homa_peer_free() - Release any resources in a peer and free the homa_peer
* struct. Invoked by the RCU mechanism via homa_peer_release.
* @head: Pointer to the rcu_head field of the peer to free.
*/
void homa_peer_free(struct rcu_head *head)
{
struct homa_peer *peer;
peer = container_of(head, struct homa_peer, rcu_head);
dst_release(rcu_dereference_protected(peer->dst, 1));
kfree(peer);
}
/**
* homa_peer_get() - Returns the peer associated with a given host; creates
* a new homa_peer if one doesn't already exist.
* @hsk: Socket where the peer will be used.
* @addr: Address of the desired host: IPv4 addresses are represented
* as IPv4-mapped IPv6 addresses.
*
* Return: The peer associated with @addr, or a negative errno if an
* error occurred. On a successful return the reference count
* will be incremented for the returned peer. The caller must
* eventually call homa_peer_release to release the reference.
*/
struct homa_peer *homa_peer_get(struct homa_sock *hsk,
const struct in6_addr *addr)
{
struct homa_peertab *peertab = hsk->homa->peertab;
struct homa_peer *peer, *other;
struct homa_peer_key key;
key.addr = *addr;
key.hnet = hsk->hnet;
rcu_read_lock();
peer = rhashtable_lookup(&peertab->ht, &key, ht_params);
if (peer && refcount_inc_not_zero(&peer->refs)) {
peer->access_jiffies = jiffies;
rcu_read_unlock();
return peer;
}
/* No existing entry, so we have to create a new one. */
peer = homa_peer_alloc(hsk, addr);
if (IS_ERR(peer)) {
rcu_read_unlock();
return peer;
}
spin_lock_bh(&peertab->lock);
other = rhashtable_lookup_get_insert_fast(&peertab->ht,
&peer->ht_linkage, ht_params);
if (IS_ERR(other)) {
/* Couldn't insert; return the error info. */
homa_peer_release(peer);
peer = other;
} else if (other) {
/* Someone else already created the desired peer; use that
* one instead of ours.
*/
homa_peer_release(peer);
refcount_inc(&other->refs);
peer = other;
peer->access_jiffies = jiffies;
} else {
refcount_inc(&peer->refs);
peertab->num_peers++;
key.hnet->num_peers++;
}
spin_unlock_bh(&peertab->lock);
rcu_read_unlock();
return peer;
}
/**
* homa_get_dst() - Returns destination information associated with a peer,
* updating it if the cached information is stale.
* @peer: Peer whose destination information is desired.
* @hsk: Homa socket with which the dst will be used; needed by lower-level
* code to recreate the dst.
* Return: Up-to-date destination for peer; a reference has been taken
* on this dst_entry, which the caller must eventually release.
*/
struct dst_entry *homa_get_dst(struct homa_peer *peer, struct homa_sock *hsk)
{
struct dst_entry *dst;
int pass;
rcu_read_lock();
for (pass = 0; ; pass++) {
do {
/* This loop repeats only if we happen to fetch
* the dst right when it is being reset.
*/
dst = rcu_dereference(peer->dst);
} while (!dst_hold_safe(dst));
/* After the first pass it's OK to return an obsolete dst
* (we're basically giving up; continuing could result in
* an infinite loop if homa_dst_refresh can't create a new dst).
*/
if (dst_check(dst, peer->dst_cookie) || pass > 0)
break;
dst_release(dst);
INC_METRIC(peer_dst_refreshes, 1);
homa_peer_reset_dst(peer, hsk);
}
rcu_read_unlock();
return dst;
}
/**
* homa_peer_reset_dst() - Find an appropriate dst_entry for a peer and
* store it in the peer's dst field. If the field is already set, the
* current value is assumed to be stale and will be discarded if a new
* dst_entry can be created.
* @peer: The peer whose dst field should be reset.
* @hsk: Socket that will be used for sending packets.
* Return: Zero for success, or a negative errno if there was an error
* (in which case the existing value for the dst field is left
* in place).
*/
int homa_peer_reset_dst(struct homa_peer *peer, struct homa_sock *hsk)
{
struct dst_entry *dst;
int result = 0;
homa_peer_lock(peer);
memset(&peer->flow, 0, sizeof(peer->flow));
if (hsk->sock.sk_family == AF_INET) {
struct rtable *rt;
flowi4_init_output(&peer->flow.u.ip4, hsk->sock.sk_bound_dev_if,
hsk->sock.sk_mark, hsk->inet.tos,
RT_SCOPE_UNIVERSE, hsk->sock.sk_protocol, 0,
ipv6_to_ipv4(peer->addr),
hsk->inet.inet_saddr, 0, 0,
hsk->sock.sk_uid);
security_sk_classify_flow(&hsk->sock,
&peer->flow.u.__fl_common);
rt = ip_route_output_flow(sock_net(&hsk->sock),
&peer->flow.u.ip4, &hsk->sock);
if (IS_ERR(rt)) {
result = PTR_ERR(rt);
INC_METRIC(peer_route_errors, 1);
goto done;
}
dst = &rt->dst;
peer->dst_cookie = 0;
} else {
/* This code is derived from code in tcp_v6_connect. */
peer->flow.u.ip6.flowi6_proto = hsk->sock.sk_protocol;
peer->flow.u.ip6.daddr = peer->addr;
peer->flow.u.ip6.saddr = hsk->inet.pinet6->saddr;
peer->flow.u.ip6.flowlabel = ip6_make_flowinfo(hsk->inet.tos,
0);
peer->flow.u.ip6.flowi6_oif = hsk->sock.sk_bound_dev_if;
peer->flow.u.ip6.flowi6_mark = hsk->sock.sk_mark;
peer->flow.u.ip6.fl6_dport = 0;
peer->flow.u.ip6.fl6_sport = 0;
peer->flow.u.ip6.flowi6_uid = hsk->sock.sk_uid;
security_sk_classify_flow(&hsk->sock,
&peer->flow.u.__fl_common);
dst = ip6_dst_lookup_flow(sock_net(&hsk->sock), &hsk->sock,
&peer->flow.u.ip6, NULL);
if (IS_ERR(dst)) {
result = PTR_ERR(dst);
INC_METRIC(peer_route_errors, 1);
goto done;
}
peer->dst_cookie = rt6_get_cookie(dst_rt6_info(dst));
}
/* From the standpoint of homa_get_dst, peer->dst is not updated
* atomically with peer->dst_cookie, which means homa_get_dst could
* use a new cookie with an old dest. Fortunately, this is benign; at
* worst, it might cause an obsolete dst to be reused (resulting in
* a lost packet) or a valid dst to be replaced (resulting in
* unnecessary work).
*/
dst_release(rcu_replace_pointer(peer->dst, dst, true));
done:
homa_peer_unlock(peer);
return result;
}
#ifndef __STRIP__ /* See strip.py */
/**
* homa_unsched_priority() - Returns the priority level to use for
* unscheduled packets of a message.
* @homa: Overall data about the Homa protocol implementation.
* @peer: The destination of the message.
* @length: Number of bytes in the message.
*
* Return: A priority level.
*/
int homa_unsched_priority(struct homa *homa, struct homa_peer *peer,
int length)
{
int i;
for (i = homa->num_priorities - 1; ; i--) {
if (peer->unsched_cutoffs[i] >= length)
return i;
}
/* Can't ever get here */
}
/**
* homa_peer_set_cutoffs() - Set the cutoffs for unscheduled priorities in
* a peer object. This is a convenience function used primarily by unit tests.
* @peer: Homa_peer object whose cutoffs should be set.
* @c0: Largest message size that will use priority 0.
* @c1: Largest message size that will use priority 1.
* @c2: Largest message size that will use priority 2.
* @c3: Largest message size that will use priority 3.
* @c4: Largest message size that will use priority 4.
* @c5: Largest message size that will use priority 5.
* @c6: Largest message size that will use priority 6.
* @c7: Largest message size that will use priority 7.
*/
void homa_peer_set_cutoffs(struct homa_peer *peer, int c0, int c1, int c2,
int c3, int c4, int c5, int c6, int c7)
{
peer->unsched_cutoffs[0] = c0;
peer->unsched_cutoffs[1] = c1;
peer->unsched_cutoffs[2] = c2;
peer->unsched_cutoffs[3] = c3;
peer->unsched_cutoffs[4] = c4;
peer->unsched_cutoffs[5] = c5;
peer->unsched_cutoffs[6] = c6;
peer->unsched_cutoffs[7] = c7;
}
/**
* homa_peer_lock_slow() - This function implements the slow path for
* acquiring a peer's @lock. It is invoked when the lock isn't
* immediately available. It waits for the lock, but also records statistics
* about the waiting time.
* @peer: Peer to lock.
*/
void homa_peer_lock_slow(struct homa_peer *peer)
__acquires(peer->lock)
{
u64 start = homa_clock();
tt_record("beginning wait for peer lock");
spin_lock_bh(&peer->lock);
tt_record("ending wait for peer lock");
INC_METRIC(peer_ack_lock_misses, 1);
INC_METRIC(peer_ack_lock_miss_cycles, homa_clock() - start);
}
#endif /* See strip.py */
/**
* homa_peer_add_ack() - Add a given RPC to the list of unacked
* RPCs for its server. Once this method has been invoked, it's safe
* to delete the RPC, since it will eventually be acked to the server.
* @rpc: Client RPC that has now completed.
*/
void homa_peer_add_ack(struct homa_rpc *rpc)
{
struct homa_peer *peer = rpc->peer;
struct homa_ack_hdr ack;
homa_peer_lock(peer);
if (peer->num_acks < HOMA_MAX_ACKS_PER_PKT) {
peer->acks[peer->num_acks].client_id = cpu_to_be64(rpc->id);
peer->acks[peer->num_acks].server_port = htons(rpc->dport);
peer->num_acks++;
homa_peer_unlock(peer);
return;
}
/* The peer has filled up; send an ACK message to empty it. The
* RPC in the message header will also be considered ACKed.
*/
INC_METRIC(ack_overflows, 1);
memcpy(ack.acks, peer->acks, sizeof(peer->acks));
ack.num_acks = htons(peer->num_acks);
peer->num_acks = 0;
homa_peer_unlock(peer);
homa_xmit_control(ACK, &ack, sizeof(ack), rpc);
}
/**
* homa_peer_get_acks() - Copy acks out of a peer, and remove them from the
* peer.
* @peer: Peer to check for possible unacked RPCs.
* @count: Maximum number of acks to return.
* @dst: The acks are copied to this location.
*
* Return: The number of acks extracted from the peer (<= count).
*/
int homa_peer_get_acks(struct homa_peer *peer, int count, struct homa_ack *dst)
{
/* Don't waste time acquiring the lock if there are no ids available. */
if (peer->num_acks == 0)
return 0;
homa_peer_lock(peer);
if (count > peer->num_acks)
count = peer->num_acks;
memcpy(dst, &peer->acks[peer->num_acks - count],
count * sizeof(peer->acks[0]));
peer->num_acks -= count;
homa_peer_unlock(peer);
return count;
}
/**
* homa_peer_update_sysctl_deps() - Update any peertab fields that depend
* on values set by sysctl. This function is invoked anytime a peer sysctl
* value is updated.
* @peertab: Struct to update.
*/
void homa_peer_update_sysctl_deps(struct homa_peertab *peertab)
{
peertab->idle_jiffies_min = peertab->idle_secs_min * HZ;
peertab->idle_jiffies_max = peertab->idle_secs_max * HZ;
}
#ifndef __STRIP__ /* See strip.py */
/**
* homa_peer_dointvec() - This function is a wrapper around proc_dointvec. It
* is invoked to read and write peer-related sysctl values.
* @table: sysctl table describing value to be read or written.
* @write: Nonzero means value is being written, 0 means read.
* @buffer: Address in user space of the input/output data.
* @lenp: Not exactly sure.
* @ppos: Not exactly sure.
*
* Return: 0 for success, nonzero for error.
*/
int homa_peer_dointvec(const struct ctl_table *table, int write,
void *buffer, size_t *lenp, loff_t *ppos)
{
struct homa_peertab *peertab;
struct ctl_table table_copy;
int result;
peertab = homa_net(current->nsproxy->net_ns)->homa->peertab;
/* Generate a new ctl_table that refers to a field in the
* net-specific struct homa.
*/
table_copy = *table;
table_copy.data = ((char *)peertab) + (uintptr_t)table_copy.data;
result = proc_dointvec(&table_copy, write, buffer, lenp, ppos);
homa_peer_update_sysctl_deps(peertab);
return result;
}
#endif /* See strip.py */