Repository navigation
Expand file tree
/
Copy pathutils.c
More file actions
499 lines (458 loc) · 14 KB
/
Copy pathutils.c
File metadata and controls
499 lines (458 loc) · 14 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
// SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0+
/* This file various utility functions for unit testing; this file
* is implemented entirely in C, and accesses Homa and kernel internals.
*/
#include "homa_impl.h"
#include "homa_grant.h"
#include "homa_peer.h"
#include "homa_rpc.h"
#include "ccutils.h"
#define KSELFTEST_NOT_MAIN 1
#include "kselftest_harness.h"
#include "mock.h"
#include "utils.h"
#ifndef __STRIP__ /* See strip.py */
#include "homa_qdisc.h"
#endif /* See strip.py */
/**
* unit_client_rpc() - Create a homa_client_rpc and arrange for it to be
* in a given state.
* @hsk: Socket that will receive the incoming RPC.
* @state: Desired state for the RPC.
* @client_ip: Client's IP address.
* @server_ip: Server's IP address.
* @server_port: Port number on the server.
* @id: Id for the RPC (0 means use default).
* @req_length: Amount of data in the request.
* @resp_length: Amount of data in the response.
*
* Return: The properly initialized homa_client_rpc, or NULL if
* there was an error. The RPC is not locked.
*/
struct homa_rpc *unit_client_rpc(struct homa_sock *hsk,
enum unit_rpc_state state, struct in6_addr *client_ip,
struct in6_addr *server_ip, int server_port, int id,
int req_length, int resp_length)
{
int saved_id = atomic64_read(&hsk->homa->next_outgoing_id);
union sockaddr_in_union server_addr;
int bytes_received, this_size;
struct homa_rpc *crpc;
u8 *tx_msg;
server_addr.in6.sin6_family = AF_INET6;
server_addr.in6.sin6_addr = *server_ip;
server_addr.in6.sin6_port = htons(server_port);
if (id != 0)
atomic64_set(&hsk->homa->next_outgoing_id, id);
crpc = homa_rpc_alloc_client(hsk, &server_addr);
if (IS_ERR(crpc))
return NULL;
tx_msg = kmalloc(req_length, GFP_ATOMIC);
unit_fill_data(tx_msg, req_length, 0);
if (homa_tx_copy_from_user(crpc, unit_iov_iter(tx_msg, req_length),
false) != 0) {
homa_rpc_end(crpc);
return NULL;
}
kfree(tx_msg);
homa_rpc_unlock(crpc);
if (id != 0)
atomic64_set(&hsk->homa->next_outgoing_id, saved_id);
EXPECT_EQ(RPC_OUTGOING, crpc->state);
if (state == UNIT_OUTGOING)
return crpc;
crpc->msgout.next_xmit_offset = crpc->msgout.length;
struct homa_data_hdr h;
memset(&h, 0, sizeof(h));
h.common = (struct homa_common_hdr){
.sport = htons(server_port),
.dport = htons(hsk->port),
.type = DATA,
.sender_id = cpu_to_be64(id ^ 1)
};
h.message_length = htonl(resp_length);
#ifndef __STRIP__ /* See strip.py */
h.incoming = htonl(10000);
#endif /* See strip.py */
this_size = (resp_length > UNIT_TEST_DATA_PER_PACKET)
? UNIT_TEST_DATA_PER_PACKET : resp_length;
homa_dispatch_pkts(mock_skb_alloc(server_ip, client_ip, &h.common,
this_size, 0));
if (state == UNIT_RCVD_ONE_PKT)
return crpc;
for (bytes_received = UNIT_TEST_DATA_PER_PACKET;
bytes_received < resp_length;
bytes_received += UNIT_TEST_DATA_PER_PACKET) {
this_size = resp_length - bytes_received;
if (this_size > UNIT_TEST_DATA_PER_PACKET)
this_size = UNIT_TEST_DATA_PER_PACKET;
h.seg.offset = htonl(bytes_received);
homa_dispatch_pkts(mock_skb_alloc(server_ip, client_ip,
&h.common, this_size, 0));
}
if (state == UNIT_RCVD_MSG)
return crpc;
FAIL("%s received unexpected state %d", __func__, state);
homa_rpc_end(crpc);
return NULL;
}
/**
* unit_get_in_addr - Parse a string into an IPv6 host addresss
* @s: IPV4 host specification such as 192.168.0.1, or IPv6
* spec that can be parsed by inet_pton.
*
* Return: The in_addr (in network order) corresponding to @s. If
* s couldn't be parsed properly then this function aborts.
*
*/
struct in6_addr unit_get_in_addr(char *s)
{
struct in6_addr ret = {};
unsigned int a, b, c, d;
if (sscanf(s, "%u.%u.%u.%u", &a, &b, &c, &d) == 4) {
ret.s6_addr32[3] = htonl((a<<24) + (b<<16) + (c<<8) + d);
ret.s6_addr32[2] = htonl(0x0000ffff);
} else {
int res = inet_pton(AF_INET6, s, &ret);
if (res <= 0)
abort();
}
return ret;
}
/**
* unit_list_length() - Return the number of entries in a list (not including
* the list header.
* @head: Header for the list (or any entry in the list, for that matter).
*/
int unit_list_length(struct list_head *head)
{
struct list_head *pos;
int count = 0;
list_for_each(pos, head) {
count++;
}
return count;
}
/**
* unit_log_active_ids() - Append to the test log a list of the active
* RPC ids for a given socket, in order.
* @hsk: Socket whose active RPC ids should be logged.
*/
void unit_log_active_ids(struct homa_sock *hsk)
{
struct homa_rpc *rpc;
list_for_each_entry_rcu(rpc, &hsk->active_rpcs, active_links)
unit_log_printf(" ", "%llu", rpc->id);
}
/**
* unit_log_hashed_rpcs() - And to the test log a list of the RPC ids
* all RPCs present in the hash table for a socket.
* @hsk: Socket whose hashed RPCs should be logged.
*/
void unit_log_hashed_rpcs(struct homa_sock *hsk)
{
struct homa_rpc *rpc;
int i;
for (i = 0; i < HOMA_CLIENT_RPC_BUCKETS; i++) {
hlist_for_each_entry_rcu(rpc, &hsk->client_rpc_buckets[i].rpcs,
hash_links) {
unit_log_printf(" ", "%llu", rpc->id);
}
}
for (i = 0; i < HOMA_SERVER_RPC_BUCKETS; i++) {
hlist_for_each_entry_rcu(rpc, &hsk->server_rpc_buckets[i].rpcs,
hash_links) {
unit_log_printf(" ", "%llu", rpc->id);
}
}
}
/**
* unit_log_frag_list() - Append to the test log a human-readable description
* of all of the packets on a given skb's frag_list.
* @skb: Packet whose frag_list is of interest.
* @verbose: If non-zero, use homa_print_packet for each packet;
* otherwise use homa_print_packet_short.
*/
void unit_log_frag_list(struct sk_buff *skb, int verbose)
{
struct sk_buff *frag;
char buffer[200];
for (frag = skb_shinfo(skb)->frag_list; frag != NULL;
frag = frag->next) {
if (verbose)
homa_print_packet(frag, buffer, sizeof(buffer));
else
homa_print_packet_short(frag, buffer, sizeof(buffer));
unit_log_printf("; ", "%s", buffer);
}
}
#ifndef __STRIP__ /* See strip.py */
/**
* unit_log_grantables() - Clear the test log and append information about
* all of the messages under grant->grantable_peers.
* @homa: Homa's overall state.
* Return: The value of unit_log_get().
*/
const char *unit_log_grantables(struct homa *homa)
{
struct homa_peer *peer;
struct homa_rpc *rpc;
int i;
unit_log_clear();
for (i = 0; i < HOMA_MAX_GRANTS; i++) {
rpc = homa->grant->active_rpcs[i].rpc;
if (!rpc)
continue;
unit_log_printf("; ", "active[%d]: id %llu remaining %d",
i, rpc->id, rpc->msgin.bytes_remaining);
if (rpc->msgin.active_ix != i) {
unit_log_printf(" ", "bad rank %d", rpc->msgin.active_ix);
}
}
list_for_each_entry(peer, &homa->grant->grantable_peers,
grantable_links) {
unit_log_printf("; ", "peer %s:",
homa_print_ipv6_addr(&peer->addr));
list_for_each_entry(rpc, &peer->grantable_rpcs,
grantable_links) {
unit_log_printf(" ", "id %llu remaining %d",
rpc->id, rpc->msgin.bytes_remaining);
}
}
return unit_log_get();
}
#endif /* See strip.py */
/**
* unit_log_skb_list() - Append to the test log a human-readable description
* of a list of packet buffers.
* @packets: Header for list of sk_buffs to print.
* @verbose: If non-zero, use homa_print_packet for each packet;
* otherwise use homa_print_packet_short.
*/
void unit_log_skb_list(struct sk_buff_head *packets, int verbose)
{
struct sk_buff *skb;
char buffer[200];
skb_queue_walk(packets, skb) {
if (verbose)
homa_print_packet(skb, buffer, sizeof(buffer));
else
homa_print_packet_short(skb, buffer, sizeof(buffer));
unit_log_printf("; ", "%s", buffer);
}
}
/**
* unit_print_gaps() - Returns a static string describing the gaps in an RPC.
* @rpc: Log the gaps in this RPC.
*/
const char *unit_print_gaps(struct homa_rpc *rpc)
{
struct homa_gap *gap;
static char buffer[1000];
int used = 0;
buffer[0] = 0;
list_for_each_entry(gap, &rpc->msgin.gaps, links) {
if (used != 0)
used += snprintf(buffer + used, sizeof(buffer) - used,
"; ");
used += snprintf(buffer + used, sizeof(buffer) - used,
"start %d, end %d", gap->start, gap->end);
if (gap->time != 0)
used += snprintf(buffer + used, sizeof(buffer) - used,
", time %llu", gap->time);
}
return buffer;
}
/**
* unit_server_rpc() - Create a homa_server_rpc and arrange for it to be
* in a given state.
* @hsk: Socket that will receive the incoming RPC.
* @state: Desired state for the RPC.
* @client_ip: Client's IP address.
* @server_ip: Server's IP address.
* @client_port: Port number that the client used.
* @id: Id for the RPC.
* @req_length: Amount of data in the request.
* @resp_length: Amount of data in the response.
*
* Return: The properly initialized homa_server_rpc, or NULL if
* there was an error. The RPC is not locked.
*/
struct homa_rpc *unit_server_rpc(struct homa_sock *hsk,
enum unit_rpc_state state, struct in6_addr *client_ip,
struct in6_addr *server_ip, int client_port, int id,
int req_length, int resp_length)
{
int bytes_received;
struct homa_data_hdr h;
int status;
memset(&h, 0, sizeof(h));
h.common = (struct homa_common_hdr){
.sport = htons(client_port),
.dport = htons(hsk->port),
.type = DATA,
.sender_id = cpu_to_be64(id ^ 1)
};
h.message_length = htonl(req_length);
#ifndef __STRIP__ /* See strip.py */
h.incoming = htonl(10000);
#endif /* See strip.py */
struct homa_rpc *srpc = homa_rpc_alloc_server(hsk, client_ip, &h);
if (IS_ERR(srpc))
return NULL;
EXPECT_EQ(srpc->completion_cookie, 0);
homa_rpc_unlock(srpc);
homa_dispatch_pkts(mock_skb_alloc(client_ip, server_ip, &h.common,
(req_length > UNIT_TEST_DATA_PER_PACKET)
? UNIT_TEST_DATA_PER_PACKET : req_length, 0));
if (state == UNIT_RCVD_ONE_PKT)
return srpc;
for (bytes_received = UNIT_TEST_DATA_PER_PACKET;
bytes_received < req_length;
bytes_received += UNIT_TEST_DATA_PER_PACKET) {
int this_size = req_length - bytes_received;
if (this_size > UNIT_TEST_DATA_PER_PACKET)
this_size = UNIT_TEST_DATA_PER_PACKET;
h.seg.offset = htonl(bytes_received);
homa_dispatch_pkts(mock_skb_alloc(client_ip, server_ip,
&h.common, this_size, 0));
}
if (state == UNIT_RCVD_MSG)
return srpc;
srpc->state = RPC_IN_SERVICE;
if (state == UNIT_IN_SERVICE)
return srpc;
homa_rpc_lock(srpc);
status = homa_tx_copy_from_user(srpc, unit_iov_iter((void *) 2000,
resp_length), false);
homa_rpc_unlock(srpc);
if (status != 0)
goto error;
srpc->state = RPC_OUTGOING;
if (state == UNIT_OUTGOING)
return srpc;
FAIL("%s received unexpected state %d", __func__, state);
error:
homa_rpc_end(srpc);
return NULL;
}
/**
* unit_teardown() - This function should be invoked at the end of every test.
* It performs various cleanup operations, and it also performs a set of
* consistency checks, such as checking for memory leaks or lost sk_buffs.
*/
void unit_teardown(void)
{
mock_teardown();
unit_log_clear();
}
/**
* unit_iov_iter() - Return an iov_iter corresponding to the arguments.
* @buffer: First byte of data.
* @length: Number of bytes of data.
*/
struct iov_iter *unit_iov_iter(void *buffer, size_t length)
{
static struct iovec iovec;
static struct iov_iter iter;
iovec.iov_base = buffer;
iovec.iov_len = length;
iov_iter_init(&iter, WRITE, &iovec, 1, length);
return &iter;
}
/**
* unit_ack_string() - Returns a human-readable description of the fields
* in an ack.
* @ack: The ack to stringify.
*/
char *unit_ack_string(struct homa_ack *ack)
{
static char buffer[1000];
snprintf(buffer, sizeof(buffer),
"server_port %d, client_id %llu",
ntohs(ack->server_port), be64_to_cpu(ack->client_id));
return buffer;
}
/**
* unit_sock_destroy() - Invoked by unit tests to cleanup and destroy
* a socket.
* @hsk: Socket to destroy.
*/
void unit_sock_destroy(struct homa_sock *hsk)
{
homa_sock_shutdown(hsk);
homa_sock_destroy(&hsk->sock);
}
/**
* unit_log_peers() - Return a count of the number of peers in the
* homa_peertab for @homa (could also include peers from other homas).
* @homa: Used to locate homa_peertab to count.
*/
int unit_count_peers(struct homa *homa)
{
struct rhashtable_iter iter;
struct homa_peer *peer;
int count = 0;
rhashtable_walk_enter(&homa->peertab->ht, &iter);
rhashtable_walk_start(&iter);
while (1) {
peer = rhashtable_walk_next(&iter);
if (!peer)
break;
if (IS_ERR(peer))
continue;
count++;
}
rhashtable_walk_stop(&iter);
rhashtable_walk_exit(&iter);
return count;
}
#ifndef __STRIP__ /* See strip.py */
/**
* unit_log_deferred() - Clear the unit test log and append information
* about all packets currently queued in homa_qdisc.
* @qdev: Log packets queued here.
* Return: The contents of the unit test log after any appends.
*/
const char *unit_log_deferred(struct homa_qdisc_dev *qdev)
{
struct homa_data_hdr *h;
struct rb_node *node;
struct homa_rpc *rpc;
struct sk_buff *skb;
unit_log_clear();
for (node = rb_first_cached(&qdev->deferred_rpcs); node;
node = rb_next(node)) {
rpc = container_of(node, struct homa_rpc, qrpc.rb_node);
unit_log_printf("; ", "[id %llu, offsets", rpc->id);
skb_queue_walk(&rpc->qrpc.packets, skb) {
h = (struct homa_data_hdr *)skb_transport_header(skb);
unit_log_printf(" ", "%d", ntohl(h->seg.offset));
}
unit_log_printf("", "]");
}
return unit_log_get();
}
#endif /* See strip.py */
/**
* unit_alloc_frags() - Fill in an array of fragments with allocated
* storage for use in unit tests.
* @num_frags: Number of fragments to create.
* @frags: Array of fragments to fill in. Each fragment will contain
* part of one page.
* Following @frags are 2*@num_frags integer arguments, giving offset and
* length for each of the frags within its page.
*/
void unit_alloc_frags(int num_frags, skb_frag_t *frags, ...)
{
int i, length, offset;
va_list args;
va_start(args, frags);
for (i = 0; i < num_frags; i++) {
offset = va_arg(args, int);
length = va_arg(args, int);
frags[i].netmem = page_to_netmem(alloc_page(GFP_KERNEL));
frags[i].offset = offset;
skb_frag_size_set(&frags[i], length);
}
}