Repository navigation
Expand file tree
/
Copy pathmock.c
More file actions
2857 lines (2470 loc) · 69.4 KB
/
Copy pathmock.c
File metadata and controls
2857 lines (2470 loc) · 69.4 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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0+
/* This file provides simplified substitutes for many Linux variables and
* functions in order to allow Homa unit tests to be run outside a Linux
* kernel.
*/
#include "homa_impl.h"
#include "homa_pool.h"
#include "homa_tx_pool.h"
#ifndef __STRIP__ /* See strip.py */
#include "homa_qdisc.h"
#endif /* See strip.py */
#include "ccutils.h"
#include "utils.h"
#include <linux/rhashtable.h>
/* It isn't safe to include some header files, such as stdlib, because
* they conflict with kernel header files. The explicit declarations
* below replace those header files.
*/
extern void free(void *ptr);
extern void *malloc(size_t size);
#ifdef memcpy
#undef memcpy
#endif
extern void *memcpy(void *dest, const void *src, size_t n);
/* The variables below can be set to non-zero values by unit tests in order
* to simulate error returns from various functions. If bit 0 is set to 1,
* the next call to the function will fail; bit 1 corresponds to the next
* call after that, and so on.
*/
int mock_alloc_page_errors;
int mock_alloc_skb_errors;
int mock_cmpxchg_errors;
int mock_copy_data_errors;
int mock_copy_to_frags_errors;
int mock_copy_to_iter_errors;
int mock_copy_to_user_errors;
int mock_cpu_idle;
int mock_dst_check_errors;
int mock_ethtool_ksettings_errors;
int mock_import_ubuf_errors;
int mock_import_iovec_errors;
int mock_ip6_xmit_errors;
int mock_ip_queue_xmit_errors;
int mock_kmalloc_errors;
int mock_kthread_create_errors;
int mock_prepare_to_wait_errors;
int mock_register_protosw_errors;
int mock_register_qdisc_errors;
int mock_register_sysctl_errors;
int mock_rht_init_errors;
int mock_rht_insert_errors;
int mock_route_errors;
int mock_trylock_errors;
int mock_vmalloc_errors;
int mock_wait_intr_irq_errors;
/* The value that prepare_to_wait_event should return when
* mock_prepare_to_wait_errors is nonzero.
*/
int mock_prepare_to_wait_status = -ERESTARTSYS;
/* The return value from calls to signal_pending(). */
int mock_signal_pending;
/* Used as current task during tests. Also returned by kthread_run. */
struct task_struct mock_task;
/* If a test sets this variable to nonzero, ip*xmit will log
* outgoing packets using the long format rather than short.
*/
int mock_xmit_log_verbose;
/* If a test sets this variable to nonzero, ip*xmit will log hijacking
* information from outgoing packets.
*/
int mock_xmit_log_hijack;
/* If a test sets this variable to nonzero, calls to wake_up and
* wake_up_all will be logged.
*/
int mock_log_wakeups;
/* If a test sets this variable to nonzero, call_rcu_sched will log
* whenever it is invoked.
*/
int mock_log_rcu_sched;
/* True means copy_from_iter should not log anything; false means
* it will log info about what it has copied.
*/
bool mock_copy_from_iter_no_log;
/* A zero value means that copy_to_user will actually copy bytes to
* the destination address; if nonzero, then 0 bits determine which
* copies actually occur (bit 0 for the first copy, etc., just like
* error masks).
*/
int mock_copy_to_user_dont_copy;
/* HOMA_BPAGE_SIZE will evaluate to this. */
int mock_bpage_size = 0x10000;
/* HOMA_BPAGE_SHIFT will evaluate to this. */
int mock_bpage_shift = 16;
/* True means that mock_alloc_pages will refuse any allocation request
* for pages larger than PAGE_SIZE.
*/
bool mock_no_high_order_pages = false;
/* Keeps track of all the spinlocks that have been locked but not unlocked.
* Reset for each test.
*/
static struct unit_hash *spinlocks_held;
/* Keeps track of all the blocks of memory that have been allocated by
* kmalloc but not yet freed by kfree. Reset for each test.
*/
static struct unit_hash *kmallocs_in_use;
/* Keeps track of all the results returned by proc_create that have not
* yet been closed by calling proc_remove. Reset for each test.
*/
static struct unit_hash *proc_files_in_use;
/* Keeps track of all the results returned by alloc_pages that have
* not yet been released by calling put_page. The value of each entry is
* a (char *) giving the reference count for the page. Reset for each test.
*/
static struct unit_hash *pages_in_use;
/* Number of qdiscs that have been registered but not yet unregistered
* during the current test. Reset for each test.
*/
static int registered_qdiscs;
/* Registered by most recent call to register_qdisc. */
static struct Qdisc_ops *qdisc_ops;
/* Keeps track of all the results returned by ip_route_output_flow that
* have not yet been freed. Reset for each test.
*/
static struct unit_hash *routes_in_use;
/* Keeps track of all sk_buffs that are alive in the current test.
* Reset for each test.
*/
static struct unit_hash *skbs_in_use;
/* Keeps track of all the blocks of memory that have been allocated by
* vmalloc but not yet freed by vfree. Reset for each test.
*/
static struct unit_hash *vmallocs_in_use;
/* The number of locks (other than spin locks) that have been acquired
* but not yet released. Should be 0 at the end of each test.
*/
static int mock_active_locks;
/* Total number of successful spinlock acquisitions during current test. */
int mock_total_spin_locks;
/* The number of times rcu_read_lock has been called minus the number
* of times rcu_read_unlock has been called.
* Should be 0 at the end of each test.
*/
static int mock_active_rcu_locks;
/* Pointers to memory blocks that need to be freed when mock_active_rcu_locks
* becomes zero.
*/
#define MAX_RCU_FREES 100
static void *rcu_frees[MAX_RCU_FREES];
/* The number of entries in rcu_frees that are currently occupied. */
static int num_rcu_frees;
/* Number of calls to sock_hold that haven't been matched with calls
* to sock_put.
*/
int mock_sock_holds;
/* Number of calls to homa_rpc_hold that haven't been matched with calls
* to homa_rpc_put.
*/
int mock_rpc_holds;
/* The number of times local_bh_disable() has been invoked, minus the
* number of times local_bh_enable has been invoked.
*/
static int mock_local_bh_disables;
/* The number of times preempt_disable() has been invoked, minus the
* number of times preempt_enable has been invoked.
*/
static int mock_preempt_disables;
/* Used as the return value for calls to homa_clock. */
u64 mock_clock;
/* Add this value to mock_clock every time homa_clock is invoked. */
u64 mock_clock_tick;
/* If values are present here, use them as the return values from
* homa_clock, without considering mock_clock or mock_clock_tick.
*/
#define MAX_CLOCK_VALS 10
u64 mock_clock_vals[MAX_CLOCK_VALS];
int mock_next_clock_val = 0;
int mock_num_clock_vals = 0;
/* Used as the return value for tt_get_cycles. */
u64 mock_tt_cycles;
unsigned int tsc_khz = 1000000;
/* True means that kthread_stop has been invoked for some thread,
* so kthread_should_stop should return true.
*/
bool mock_exit_thread;
/* Indicates whether we should be simulating IPv6 or IPv4 in the
* current test. Can be overridden by a test.
*/
bool mock_ipv6 = true;
/* The value to use for mock_ipv6 in each test unless overridden. */
bool mock_ipv6_default;
/* List of priorities for all outbound packets. */
char mock_xmit_prios[1000];
int mock_xmit_prios_offset;
/* Maximum packet size allowed by "network" (see homa_tx_copy_from_user);
* chosen so that data packets will have UNIT_TEST_DATA_PER_PACKET bytes
* of payload. The variable can be modified if useful in some tests.
* Set by mock_sock_init.
*/
int mock_mtu;
/* Used instead of MAX_SKB_FRAGS when running some unit tests. */
int mock_max_skb_frags = 10;
/* Each bit gives the NUMA node (0 or 1) for a particular core.*/
int mock_numa_mask = 5;
/* Bits determine the result of successive calls to compound order, starting
* at the lowest bit. 0 means return HOMA_SKB_PAGE_ORDER, 1 means return 0.
*/
int mock_compound_order_mask;
/* Bits specify the NUMA node number that will be returned by the next
* calls to mock_page_to_nid, starting with the low-order bit.
*/
int mock_page_nid_mask;
/* Used to collect printk output. */
char mock_printk_output [5000];
/* Used as the return values from rhashtable_walk_next calls. */
void **mock_rht_walk_results;
int mock_rht_num_walk_results;
/* Used instead of HOMA_MIN_DEFAULT_PORT by homa_skb.c. */
__u16 mock_min_default_port = 0x8000;
/* Used as sk_socket for all sockets created by mock_sock_init.
* Its sk field points to the most recently created Homa socket. */
static struct socket mock_socket;
/* Each of the entries in mock_hnets below is associated with the
* corresonding entry in mock_nets.
*/
#define MOCK_MAX_NETS 10
struct net mock_nets[MOCK_MAX_NETS];
struct homa_net *mock_hnets[MOCK_MAX_NETS];
struct net_device mock_devices[MOCK_MAX_NETS];
/* Nonzero means don't generate a unit test failure when freeing peers
* if the reference count isn't zero (log a message instead).
*/
int mock_peer_free_no_fail;
/* Link speed to return from mock_get_link_ksettings. */
int mock_link_mbps = 10000;
struct ethtool_ops mock_ethtool_ops =
{.get_link_ksettings = mock_get_link_ksettings};
struct dst_ops mock_dst_ops = {
.mtu = mock_get_mtu,
.check = mock_dst_check};
struct netdev_queue mock_net_queue = {.state = 0};
/* Use this as the dev queue index in new skbs. */
int mock_queue_index = 0;
/* Number of invocations of netif_schedule_queue. */
int mock_netif_schedule_calls;
/* True means that failures should be logged if a test ends with buffer
* space still allocated in a homa_pool.
*/
bool mock_check_bpool_leaks = true;
/* Keeps track of all of the sockets created during the current test.
* Used to attribute calls to refcount_inc_not_zero to a socket (or not).
*/
#define MOCK_MAX_SOCKS 100
struct homa_sock *mock_socks[MOCK_MAX_SOCKS];
int mock_num_socks;
/* Keeps track of the reasons given for freeing skbs. */
#define MAX_DROP_REASONS 10
enum skb_drop_reason mock_drop_reasons[MAX_DROP_REASONS];
int mock_num_drop_reasons;
const struct net_offload *inet_offloads[MAX_INET_PROTOS];
const struct net_offload *inet6_offloads[MAX_INET_PROTOS];
struct net_offload tcp_offload;
struct net_offload tcp_v6_offload;
static struct hrtimer_clock_base clock_base;
struct task_struct *current_task = &mock_task;
unsigned long ex_handler_refcount;
struct net init_net;
unsigned long volatile jiffies = 1100;
unsigned int nr_cpu_ids = 8;
unsigned long page_offset_base;
unsigned long phys_base;
unsigned long vmemmap_base;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
kmem_buckets kmalloc_caches[NR_KMALLOC_TYPES];
#endif
int __preempt_count;
int cpu_number = 1;
char sock_flow_table[RPS_SOCK_FLOW_TABLE_SIZE(1024)];
struct net_hotdata net_hotdata = {
.rps_cpu_mask = 0x1f,
.rps_sock_flow_table = (struct rps_sock_flow_table *) sock_flow_table
};
int debug_locks;
struct static_call_key __SCK__cond_resched;
struct static_call_key __SCK__might_resched;
struct static_call_key __SCK__preempt_schedule;
struct paravirt_patch_template pv_ops;
struct workqueue_struct *system_wq;
struct static_key_true validate_usercopy_range;
unsigned long __per_cpu_offset[NR_CPUS];
struct tracepoint __tracepoint_sched_set_state_tp;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
struct lockdep_map rcu_lock_map;
#endif /* CONFIG_DEBUG_LOCK_ALLOC */
extern void add_wait_queue(struct wait_queue_head *wq_head,
struct wait_queue_entry *wq_entry)
{}
struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags,
int node)
{
struct sk_buff *skb;
int shinfo_size;
if (mock_check_error(&mock_alloc_skb_errors))
return NULL;
skb = malloc(sizeof(struct sk_buff));
if (skb == NULL)
FAIL(" skb malloc failed in %s", __func__);
memset(skb, 0, sizeof(*skb));
if (!skbs_in_use)
skbs_in_use = unit_hash_new();
unit_hash_set(skbs_in_use, skb, "used");
shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
skb->head = malloc(size + shinfo_size);
memset(skb->head, 0, size + shinfo_size);
if (skb->head == NULL)
FAIL(" data malloc failed in %s", __func__);
skb->data = skb->head;
skb_reset_tail_pointer(skb);
skb->end = skb->tail + size;
skb->network_header = 0;
skb->transport_header = 0;
skb->data_len = 0;
skb->len = 0;
skb->users.refs.counter = 1;
skb->_skb_refdst = 0;
ip_hdr(skb)->saddr = 0;
skb->truesize = SKB_TRUESIZE(size);
skb->dev = &mock_devices[0];
return skb;
}
int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode,
int sync, void *key)
{
return 0;
}
void BUG_func(void)
{}
void call_rcu(struct rcu_head *head, void free_func(struct rcu_head *head))
{
unit_log_printf("; ", "call_rcu invoked");
free_func(head);
}
bool cancel_work_sync(struct work_struct *work)
{
return false;
}
void __check_object_size(const void *ptr, unsigned long n, bool to_user) {}
void consume_skb(struct sk_buff *skb) {
kfree_skb(skb);
}
size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *iter)
{
size_t bytes_left = bytes;
if (mock_check_error(&mock_copy_data_errors))
return false;
if (bytes > iter->count) {
unit_log_printf("; ", "copy_from_iter needs %lu bytes, but iov_iter has only %lu", bytes,
iter->count);
return 0;
}
while (bytes_left > 0) {
struct iovec *iov = (struct iovec *) iter_iov(iter);
uintptr_t int_base = (uintptr_t)iov->iov_base;
size_t chunk_bytes = iov->iov_len;
if (chunk_bytes > bytes_left)
chunk_bytes = bytes_left;
if (!mock_copy_from_iter_no_log)
unit_log_printf("; ",
"_copy_from_iter %lu bytes at %lu",
chunk_bytes, int_base);
/* Copy actual data unless it the iterator address is
* obviously bogus (e.g. created by passing NULL to
* unit_iov_iter).
*/
if (int_base > 1000000)
memcpy(addr, iov->iov_base, chunk_bytes);
addr = (u8 *)addr + chunk_bytes;
bytes_left -= chunk_bytes;
iter->count -= chunk_bytes;
iov->iov_base = (void *) (int_base + chunk_bytes);
iov->iov_len -= chunk_bytes;
if (iov->iov_len == 0)
iter->__iov++;
}
return bytes;
}
bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
{
if (mock_check_error(&mock_copy_data_errors))
return false;
unit_log_printf("; ", "_copy_from_iter_full copied %lu bytes", bytes);
return true;
}
bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
{
if (mock_check_error(&mock_copy_data_errors))
return false;
unit_log_printf("; ", "_copy_from_iter_full_nocache copid %lu bytes",
bytes);
return true;
}
size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
{
if (mock_check_error(&mock_copy_to_iter_errors))
return 0;
unit_log_printf("; ", "_copy_to_iter: %.*s", (int) bytes,
(char *) addr);
return bytes;
}
unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
{
if (mock_check_error(&mock_copy_to_user_errors))
return -1;
if (!mock_check_error(&mock_copy_to_user_dont_copy))
memcpy(to, from, n);
unit_log_printf("; ", "_copy_to_user copied %lu bytes to %p", n, to);
return 0;
}
unsigned long _copy_from_user(void *to, const void __user *from,
unsigned long n)
{
u64 int_from = (u64) from;
if (mock_check_error(&mock_copy_data_errors))
return 1;
if (int_from > 200000)
memcpy(to, from, n);
unit_log_printf("; ", "_copy_from_user %lu bytes at %llu", n, int_from);
return 0;
}
void __copy_overflow(int size, unsigned long count)
{
abort();
}
__sum16 csum_ipv6_magic(const struct in6_addr *saddr,
const struct in6_addr *daddr,
__u32 len, __u8 proto, __wsum csum)
{
return 0;
}
#ifdef CONFIG_DEBUG_LOCK_ALLOC
int debug_lockdep_rcu_enabled(void)
{
return 0;
}
#endif
int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
{
struct homa_data_hdr *h;
h = (struct homa_data_hdr *)skb_transport_header(skb);
unit_log_printf("; ", "__dev_queue_xmit invoked for id %llu, offset %d",
be64_to_cpu(h->common.sender_id), ntohl(h->seg.offset));
kfree_skb(skb);
return 0;
}
int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *)
{
UNIT_HOOK("do_wait_intr_irq");
if (mock_check_error(&mock_wait_intr_irq_errors))
return -ERESTARTSYS;
return 0;
}
void dst_release(struct dst_entry *dst)
{
if (!dst)
return;
if (!rcuref_put(&dst->__rcuref))
return;
if (!routes_in_use || unit_hash_get(routes_in_use, dst) == NULL) {
FAIL(" %s on unknown route", __func__);
return;
}
unit_hash_erase(routes_in_use, dst);
free(dst);
}
void finish_wait(struct wait_queue_head *wq_head,
struct wait_queue_entry *wq_entry)
{}
void get_random_bytes(void *buf, size_t nbytes)
{
memset(buf, 0, nbytes);
}
u32 get_random_u32(void)
{
return 0;
}
int hrtimer_cancel(struct hrtimer *timer)
{
return 0;
}
u64 hrtimer_forward(struct hrtimer *timer, ktime_t now,
ktime_t interval)
{
return 0;
}
ktime_t hrtimer_get_time(void)
{
return 0;
}
void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
enum hrtimer_mode mode)
{
timer->base = &clock_base;
clock_base.get_time = &hrtimer_get_time;
}
void hrtimer_setup(struct hrtimer *timer,
enum hrtimer_restart (*function)(struct hrtimer *),
clockid_t clock_id, enum hrtimer_mode mode)
{
timer->base = &clock_base;
clock_base.get_time = &hrtimer_get_time;
timer->function = function;
}
void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
u64 range_ns, const enum hrtimer_mode mode)
{}
void __icmp_send(struct sk_buff *skb, int type, int code, __be32 info,
const struct ip_options *opt)
{
unit_log_printf("; ", "icmp_send type %d, code %d", type, code);
}
void icmp6_send(struct sk_buff *skb, u8 type, u8 code, u32 info,
const struct in6_addr *force_saddr,
const struct inet6_skb_parm *parm)
{
unit_log_printf("; ", "icmp6_send type %d, code %d", type, code);
}
int idle_cpu(int cpu)
{
return mock_check_error(&mock_cpu_idle);
}
ssize_t import_iovec(int type, const struct iovec __user *uvector,
unsigned int nr_segs, unsigned int fast_segs,
struct iovec **iov, struct iov_iter *iter)
{
ssize_t size;
unsigned int i;
*iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
if (mock_check_error(&mock_import_iovec_errors))
return -EINVAL;
size = 0;
for (i = 0; i < nr_segs; i++) {
size += uvector[i].iov_len;
(*iov)[i] = uvector[i];
}
iov_iter_init(iter, type, *iov, nr_segs, size);
return size;
}
int import_ubuf(int rw, void __user *buf, size_t len, struct iov_iter *i)
{
if (mock_check_error(&mock_import_ubuf_errors))
return -EACCES;
iov_iter_ubuf(i, rw, buf, len);
return 0;
}
int inet6_add_offload(const struct net_offload *prot, unsigned char protocol)
{
return 0;
}
int inet6_add_protocol(const struct inet6_protocol *prot, unsigned char num)
{
return 0;
}
int inet6_del_offload(const struct net_offload *prot, unsigned char protocol)
{
return 0;
}
int inet6_del_protocol(const struct inet6_protocol *prot, unsigned char num)
{
return 0;
}
int inet6_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
{
return 0;
}
int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
return 0;
}
int inet6_register_protosw(struct inet_protosw *p)
{
if (mock_check_error(&mock_register_protosw_errors))
return -EINVAL;
return 0;
}
int inet6_release(struct socket *sock)
{
return 0;
}
void inet6_unregister_protosw(struct inet_protosw *p) {}
int inet_add_offload(const struct net_offload *prot, unsigned char protocol)
{
return 0;
}
int inet_add_protocol(const struct net_protocol *prot, unsigned char num)
{
return 0;
}
int inet_del_offload(const struct net_offload *prot, unsigned char protocol)
{
return 0;
}
int inet_del_protocol(const struct net_protocol *prot, unsigned char num)
{
return 0;
}
int inet_dgram_connect(struct socket *sock, struct sockaddr *uaddr,
int addr_len, int flags)
{
return 0;
}
int inet_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
{
return 0;
}
int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
return 0;
}
int inet_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
int flags)
{
return 0;
}
void inet_register_protosw(struct inet_protosw *p)
{}
int inet_release(struct socket *sock)
{
return 0;
}
int inet_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
{
return 0;
}
void inet_unregister_protosw(struct inet_protosw *p)
{}
void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
struct lock_class_key *key)
{}
void init_wait_entry(struct wait_queue_entry *wq_entry, int flags)
{}
void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name,
struct lock_class_key *)
{}
void iov_iter_init(struct iov_iter *i, unsigned int direction,
const struct iovec *iov, unsigned long nr_segs,
size_t count)
{
direction &= READ | WRITE;
i->iter_type = ITER_IOVEC | direction;
i->__iov = iov;
i->nr_segs = nr_segs;
i->iov_offset = 0;
i->count = count;
}
void iov_iter_revert(struct iov_iter *i, size_t bytes)
{
unit_log_printf("; ", "iov_iter_revert %lu", bytes);
}
int ip6_datagram_connect(struct sock *sk, struct sockaddr *addr, int addr_len)
{
return 0;
}
struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
{
if (mock_check_error(&mock_dst_check_errors))
return NULL;
return dst;
}
struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk,
struct flowi6 *fl6, const struct in6_addr *final_dst)
{
struct rtable *route;
if (mock_check_error(&mock_route_errors))
return ERR_PTR(-EHOSTUNREACH);
route = malloc(sizeof(struct rtable));
if (!route) {
FAIL(" malloc failed");
return ERR_PTR(-ENOMEM);
}
rcuref_init(&route->dst.__rcuref, 1);
route->dst.ops = &mock_dst_ops;
route->dst.dev = &mock_devices[0];
route->dst.obsolete = 0;
if (!routes_in_use)
routes_in_use = unit_hash_new();
unit_hash_set(routes_in_use, route, "used");
return &route->dst;
}
unsigned int ip6_mtu(const struct dst_entry *dst)
{
return mock_mtu;
}
int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority)
{
char buffer[200];
const char *prefix = " ";
if (unit_hash_size(spinlocks_held) > 0)
FAIL("ip6_xmit invoked with %d spinlocks held; this isn't safe because homa_qdisc_enqueue may acquire an RPC lock",
unit_hash_size(spinlocks_held));
if (mock_check_error(&mock_ip6_xmit_errors)) {
kfree_skb(skb);
return -ENETDOWN;
}
if (mock_xmit_prios_offset == 0)
prefix = "";
mock_xmit_prios_offset += snprintf(
mock_xmit_prios + mock_xmit_prios_offset,
sizeof(mock_xmit_prios) - mock_xmit_prios_offset,
"%s%d", prefix, tclass >> 5);
if (mock_xmit_log_verbose)
homa_print_packet(skb, buffer, sizeof(buffer));
else
homa_print_packet_short(skb, buffer, sizeof(buffer));
unit_log_printf("; ", "xmit %s", buffer);
#ifndef __STRIP__ /* See strip.py */
if (mock_xmit_log_hijack) {
struct homa_common_hdr *h;
h = (struct homa_common_hdr *)skb_transport_header(skb);
unit_log_printf("; ", "hijack checksum %d, flags 0x%x",
h->checksum, h->flags);
}
#endif /* See strip.py */
kfree_skb(skb);
return 0;
}
int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)
{
const char *prefix = " ";
char buffer[200];
if (unit_hash_size(spinlocks_held) > 0)
FAIL("ip_queue_xmit invoked with %d spinlocks held; this isn't safe because homa_qdisc_enqueue may acquire an RPC lock",
unit_hash_size(spinlocks_held));
if (mock_check_error(&mock_ip_queue_xmit_errors)) {
/* Latest data (as of 1/2019) suggests that ip_queue_xmit
* frees packets after errors.
*/
kfree_skb(skb);
return -ENETDOWN;
}
if (mock_xmit_prios_offset == 0)
prefix = "";
mock_xmit_prios_offset += snprintf(
mock_xmit_prios + mock_xmit_prios_offset,
sizeof(mock_xmit_prios) - mock_xmit_prios_offset,
"%s%d", prefix, ((struct inet_sock *) sk)->tos>>5);
if (mock_xmit_log_verbose)
homa_print_packet(skb, buffer, sizeof(buffer));
else
homa_print_packet_short(skb, buffer, sizeof(buffer));
unit_log_printf("; ", "xmit %s", buffer);
#ifndef __STRIP__ /* See strip.py */
if (mock_xmit_log_hijack) {
struct homa_common_hdr *h;
h = (struct homa_common_hdr *)skb_transport_header(skb);
unit_log_printf("; ", "hijack checksum %d, flags 0x%x",
h->checksum, h->flags);
}
#endif /* See strip.py */
kfree_skb(skb);
return 0;
}
struct dst_entry *ipv4_dst_check(struct dst_entry *dst, u32 cookie)
{
if (mock_check_error(&mock_dst_check_errors))
return NULL;
return dst;
}
unsigned int ipv4_mtu(const struct dst_entry *dst)
{
return mock_mtu;
}
struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4,
const struct sock *sk)
{
struct rtable *route;
if (mock_check_error(&mock_route_errors))
return ERR_PTR(-EHOSTUNREACH);
route = malloc(sizeof(struct rtable));
if (!route) {
FAIL(" malloc failed");
return ERR_PTR(-ENOMEM);
}
rcuref_init(&route->dst.__rcuref, 1);
route->dst.ops = &mock_dst_ops;
route->dst.dev = &mock_devices[0];
route->dst.obsolete = 0;
if (!routes_in_use)
routes_in_use = unit_hash_new();
unit_hash_set(routes_in_use, route, "used");
return route;
}
int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
int addr_len)
{
return 0;
}
void device_set_wakeup_capable(struct device *dev, bool capable)
{}
void device_wakeup_disable(struct device *dev)
{}
int device_wakeup_enable(struct device *dev)
{
return 0;
}
int filp_close(struct file *, fl_owner_t id)
{
return 0;
}
struct file *filp_open(const char *, int, umode_t)
{
return NULL;
}
void __fortify_panic(const u8 reason, const size_t avail, const size_t size)
{
FAIL(" __fortify_panic invoked");
/* API prohibits return. */
while (1) ;
}
ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
{
return 0;
}
ssize_t kernel_write(struct file *file, const void *buf, size_t count,
loff_t *pos)
{
return 0;
}
void kfree(const void *block)
{
if (block == NULL)
return;
UNIT_HOOK("kfree");
if (!kmallocs_in_use || unit_hash_get(kmallocs_in_use, block) == NULL) {
FAIL(" %s on unknown block %p", __func__, block);
return;
}
unit_hash_erase(kmallocs_in_use, block);
free((void *) block);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 12, 0)