-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathargo-module.c
3930 lines (3233 loc) · 96.9 KB
/
argo-module.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
/******************************************************************************
* drivers/xen/argo/argo.c
*
* Argo: Hypervisor-Mediated data eXchange
*
* Derived from v4v, the version 2 of v2v.
*
* Copyright (c) 2009 Ross Philipson
* Copyright (c) 2009 James McKenzie
* Copyright (c) 2009 Citrix Systems, Inc.
* Modifications by Christopher Clark are Copyright (c) 2018 BAE Systems
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation; or, when distributed
* separately from the Linux kernel or incorporated into other
* software packages, subject to the following license:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this source file (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <linux/version.h>
#ifndef CONFIG_PARAVIRT
#define CONFIG_PARAVIRT
#endif
#ifdef XC_DKMS
#include "xen-dkms.h"
#else /* ! XC_DKMS */
#define xc_bind_virq_to_irqhandler bind_virq_to_irqhandler
#define xc_unbind_from_irqhandler unbind_from_irqhandler
#endif /* XC_DKMS */
#include <linux/version.h>
#include <linux/compat.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/vmalloc.h>
#include <linux/interrupt.h>
#include <linux/rwsem.h>
#include <linux/list.h>
#include <linux/socket.h>
#include <linux/workqueue.h>
#ifdef XC_KERNEL
#include <asm/hypercall.h>
#include <xen/hypercall.h>
#else /* ! XC_KERNEL */
#ifdef XC_DKMS
#include <xen/xen.h>
#endif /* XC_DKMS */
#include <linux/sched.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,11,0)
#include <linux/cred.h>
#include <linux/sched/signal.h>
#endif
#endif /* XC_KERNEL */
#include "argo_hypercall.h"
#include <xen/evtchn.h>
#include <xen/argo.h>
#include <linux/argo_dev.h>
#include <linux/argo.h>
#include <linux/fs.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/major.h>
#include <linux/proc_fs.h>
#include <linux/poll.h>
#include <linux/random.h>
#include <linux/wait.h>
#include <linux/file.h>
#include <linux/mount.h>
#include <linux/dcache.h>
#include <linux/slab.h>
#if ( LINUX_VERSION_CODE >= KERNEL_VERSION(5,3,0) )
#include <linux/pseudo_fs.h>
#endif
#if ( LINUX_VERSION_CODE >= KERNEL_VERSION(5,0,0) )
# define access_ok_wrapper(type, addr, size) access_ok((addr), (size))
#else
# define access_ok_wrapper(type, addr, size) access_ok((type), (addr), (size))
#endif
#if (LINUX_VERSION_CODE > KERNEL_VERSION(3,9,0))
# define argo_random32 prandom_u32
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0) */
# define argo_random32 random32
#endif
#define XEN_ARGO_ROUNDUP(x) roundup((x), XEN_ARGO_MSG_SLOT_SIZE)
#define DEFAULT_RING_SIZE (XEN_ARGO_ROUNDUP((((PAGE_SIZE)*32) - ARGO_RING_OVERHEAD)))
/*The type of a ring*/
typedef enum
{
ARGO_RTYPE_IDLE = 0,
ARGO_RTYPE_DGRAM,
ARGO_RTYPE_LISTENER,
ARGO_RTYPE_CONNECTOR,
} argo_rtype;
/*the state of an argo_private*/
typedef enum
{
ARGO_STATE_IDLE = 0,
ARGO_STATE_BOUND, /*this can only be held by the ring sponsor */
ARGO_STATE_LISTENING, /*this can only be held by the ring sponsor */
ARGO_STATE_ACCEPTED,
ARGO_STATE_CONNECTING, /*this can only be held by the ring sponsor */
ARGO_STATE_CONNECTED, /*this can only be held by the ring sponsor */
ARGO_STATE_DISCONNECTED
} argo_state;
static struct rw_semaphore list_sem;
static struct list_head ring_list;
/*----------------- message formatting ---------------------*/
/* FIXME: does this belong here? */
#define ARGO_SHF_SYN (1 << 0)
#define ARGO_SHF_ACK (1 << 1)
#define ARGO_SHF_RST (1 << 2)
#define ARGO_SHF_PING (1 << 8)
#define ARGO_SHF_PONG (1 << 9)
#define ARGO_PROTO_DGRAM 0x6447724d
#define ARGO_PROTO_STREAM 0x3574526d
struct argo_stream_header
{
uint32_t flags;
uint32_t conid;
};
static uint32_t
argo_ring_bytes_to_read(volatile struct xen_argo_ring *r, uint32_t ring_size)
{
int32_t ret;
ret = r->tx_ptr - r->rx_ptr;
if ( ret >= 0 )
return ret;
return (uint32_t) (ring_size + ret);
}
/*
* _argo_copy_out :
* Copy at most t bytes of the next message in the ring, into the buffer,
* setting from and protocol if they are not NULL.
*
* This will copy to user_buf if non-NULL, otherwise _buf if non-NULL.
* When both user_buf and _buf are NULL, the ring contents are discarded.
*
* Returns actual length of the message or -1 if there is nothing to read.
*/
static ssize_t
_argo_copy_out(struct xen_argo_ring *r, uint32_t ring_size,
struct xen_argo_addr *from, uint32_t * protocol,
void *_buf, void __user *user_buf, size_t t, int consume)
{
volatile struct xen_argo_ring_message_header *mh;
/* unnecessary cast from void * required by MSVC compiler */
uint8_t *buf = (uint8_t *) _buf;
uint32_t btr = argo_ring_bytes_to_read(r, ring_size);
uint32_t rxp = r->rx_ptr;
uint32_t len, discard_len = 0;
ssize_t ret;
size_t copy_len;
if ( btr < sizeof (*mh) )
return -1;
/*
* Since the message_header is 128 bits long and the ring is
* 128 bit aligned, we are guaranteed never to wrap.
*/
mh = (volatile struct xen_argo_ring_message_header *) &r->ring[r->rx_ptr];
len = mh->len;
if ( btr < len )
return -1;
#if defined(__GNUC__)
if ( from )
*from = mh->source;
#else
/* MSVC can't do the above */
if ( from )
memcpy((void *) from, (void *) &(mh->source),
sizeof(struct xen_argo_addr));
#endif
if ( protocol )
*protocol = mh->message_type;
rxp += sizeof(*mh);
if ( rxp == ring_size )
rxp = 0;
len -= sizeof(*mh);
ret = len;
if (t < len) {
discard_len = len - t;
len = t;
}
while (len) {
copy_len = (rxp + len) > ring_size ? ring_size - rxp
: len;
if (user_buf) {
if (copy_to_user(user_buf, &r->ring[rxp], copy_len))
return -EFAULT;
user_buf += copy_len;
} else if (buf) {
memcpy (buf, &r->ring[rxp], copy_len);
buf += copy_len;
} else {
/* discarding ring contents. */
}
len -= copy_len;
rxp += copy_len;
if (rxp == ring_size)
rxp = 0;
}
/* rxp has been advanced with the copied data, but needs updating for
* skipped data and alignment before writing. */
rxp += discard_len;
rxp = XEN_ARGO_ROUNDUP(rxp);
if ( rxp >= ring_size )
rxp -= ring_size;
mb();
if ( consume )
r->rx_ptr = rxp;
return ret;
}
static ssize_t
argo_copy_out(struct xen_argo_ring *r, uint32_t ring_size,
struct xen_argo_addr *from, uint32_t * protocol,
void *buf, size_t t, int consume)
{
return _argo_copy_out(r, ring_size, from, protocol, buf, NULL, t, consume);
}
static ssize_t
argo_copy_out_user(struct xen_argo_ring *r, uint32_t ring_size,
struct xen_argo_addr *from, uint32_t * protocol,
void __user *buf, size_t t, int consume)
{
return _argo_copy_out(r, ring_size, from, protocol, NULL, buf, t, consume);
}
/*----------------- ---------------------*/
struct argo_private;
/* Ring pointer itself is protected by the refcnt, the lists its in by list_sem.
* It's permittable to decrement the refcnt whilst holding the read lock,
* and then clean up refcnt=0 rings later.
* If a ring has (refcnt != 0) we expect ->ring to be non NULL, and for the ring to
* be registered with Xen.
*/
struct ring
{
struct list_head node;
atomic_t refcnt;
/*Protects the data in the xen_argo_ring_t also privates and sponsor */
struct mutex lock;
struct list_head privates; /* Protected by lock */
struct argo_private *sponsor; /* Protected by lock */
argo_rtype type;
/*Ring */
xen_argo_ring_t *ring;
struct argo_ring_id id;
uint32_t len;
xen_argo_gfn_t *gfn_array;
int npfns;
int order;
};
struct argo_private
{
struct list_head node;
argo_state state;
argo_ptype ptype;
uint32_t desired_ring_size;
struct ring *r;
wait_queue_head_t readq;
wait_queue_head_t writeq;
xen_argo_addr_t peer;
uint32_t conid;
/* Protects pending messages, and pending_error */
struct mutex pending_recv_lock;
struct list_head pending_recv_list; /*For LISTENER contains only ... */
atomic_t pending_recv_count;
int pending_error;
int full;
int send_blocked;
int rx;
struct timer_list to;
};
struct pending_recv
{
struct list_head node;
xen_argo_addr_t from;
size_t data_len, data_ptr;
struct argo_stream_header sh;
uint8_t data[0];
} ARGO_PACKED;
static struct mutex pending_xmit_lock;
static struct list_head pending_xmit_list;
static atomic_t pending_xmit_count;
enum argo_pending_xmit_type
{
/*Send the inline xmit */
ARGO_PENDING_XMIT_INLINE = 1,
/*Wake up writeq of sponsor of the ringid from */
ARGO_PENDING_XMIT_WAITQ_MATCH_SPONSOR,
/*Wake up writeq of a private of ringid from with conid conid */
ARGO_PENDING_XMIT_WAITQ_MATCH_PRIVATES,
};
struct pending_xmit
{
struct list_head node;
enum argo_pending_xmit_type type;
uint32_t conid;
struct argo_ring_id from;
xen_argo_addr_t to;
size_t len;
uint32_t protocol;
uint8_t data[0];
};
#define MAX_PENDING_RECVS 2
/************************debugging **********************************/
/****************** hypercall ops *************************************/
static int
H_argo_register_ring(xen_argo_register_ring_t *r,
xen_argo_gfn_t *arr,
uint32_t len, uint32_t flags)
{
(void)(*(volatile int*)r);
return HYPERVISOR_argo_op(XEN_ARGO_OP_register_ring, r, arr, len, flags);
}
static int
H_argo_unregister_ring (xen_argo_unregister_ring_t *r)
{
(void)(*(volatile int*)r);
return HYPERVISOR_argo_op(XEN_ARGO_OP_unregister_ring, r, NULL, 0, 0);
}
static int
H_argo_sendv(xen_argo_addr_t *s, xen_argo_addr_t *d,
const xen_argo_iov_t *iovs, uint32_t niov,
uint32_t protocol)
{
xen_argo_send_addr_t send;
send.dst = *d;
send.src = *s;
send.src.pad = 0;
send.dst.pad = 0;
return HYPERVISOR_argo_op(XEN_ARGO_OP_sendv,
&send, (void *)iovs, niov, protocol);
}
static int
H_argo_notify(xen_argo_ring_data_t *rd)
{
return HYPERVISOR_argo_op(XEN_ARGO_OP_notify, rd, NULL, 0, 0);
}
static int
H_viptables_add(xen_argo_viptables_rule_t* rule, int position)
{
return HYPERVISOR_argo_op(XEN_ARGO_OP_viptables_add, rule, NULL, 0,
position);
}
static int
H_viptables_del(xen_argo_viptables_rule_t* rule, int position)
{
return HYPERVISOR_argo_op(XEN_ARGO_OP_viptables_del, rule, NULL, 0,
position);
}
static int
H_viptables_list(xen_argo_viptables_list_t *rules_list)
{
return HYPERVISOR_argo_op(XEN_ARGO_OP_viptables_list, rules_list, NULL, 0,
0);
}
/*********************port/ring uniqueness **********/
/*Need to hold write lock for all of these*/
static int
argo_id_in_use(struct argo_ring_id *id)
{
struct ring *r;
list_for_each_entry (r, &ring_list, node)
{
if ( (r->id.aport == id->aport) &&
(r->id.partner_id == id->partner_id) )
return 1;
}
return 0;
}
static xen_argo_port_t
argo_port_in_use(xen_argo_port_t aport, xen_argo_port_t *max)
{
xen_argo_port_t ret = 0;
struct ring *r;
list_for_each_entry (r, &ring_list, node)
{
if ( r->id.aport == aport )
ret++;
if ( max && (r->id.aport > *max) )
*max = r->id.aport;
}
return ret;
}
static xen_argo_port_t
argo_random_port(void)
{
xen_argo_port_t port;
port = argo_random32();
port |= 0x80000000U;
if ( port > 0xf0000000U )
port -= 0x10000000;
return port;
}
static const xen_argo_port_t ARGO_PORTS_EXHAUSTED = 0xffffffffU;
/*caller needs to hold lock*/
static xen_argo_port_t
argo_find_spare_port_number (void)
{
xen_argo_port_t port, max = 0x80000000U;
port = argo_random_port();
if ( !argo_port_in_use(port, &max) )
return port;
else
port = max + 1;
return port;
}
/******************************ring goo ***************/
static int
register_ring(struct ring *r)
{
xen_argo_register_ring_t reg;
/* flags are zero: allow reregistration of an existing ring */
pr_debug("Registering Ring: aport=%u domain_id=%d partner_id=%d\n",
(unsigned int) r->id.aport,
(int) r->id.domain_id, (int) r->id.partner_id);
reg.aport = r->id.aport;
reg.partner_id = r->id.partner_id;
reg.len = r->len;
reg.pad = 0;
return H_argo_register_ring((void *) ®, r->gfn_array,
r->npfns, 0);
}
static int
unregister_ring(struct ring *r)
{
xen_argo_unregister_ring_t reg;
reg.aport = r->id.aport;
reg.partner_id = r->id.partner_id;
reg.pad = 0;
/* FIXME: void * : hmm... */
return H_argo_unregister_ring((void *) ®);
}
static void
refresh_gfn_array(struct ring *r)
{
uint8_t *b = (void *)r->ring;
unsigned int i;
for ( i = 0; i < r->npfns; ++i )
{
#if defined(CONFIG_X86_64) || defined(CONFIG_X86_32)
r->gfn_array[i] = pfn_to_mfn(vmalloc_to_pfn(b));
#else
r->gfn_array[i] = pfn_to_gfn(vmalloc_to_pfn(b));
#endif
b += PAGE_SIZE;
}
}
static int
allocate_gfn_array(struct ring *r)
{
uint32_t n = (r->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
size_t len = n * sizeof(xen_argo_gfn_t);
r->gfn_array = kmalloc(len, GFP_KERNEL);
if ( !r->gfn_array )
return -ENOMEM;
memset(r->gfn_array, 0, len);
r->npfns = n;
refresh_gfn_array(r);
return 0;
}
static int
allocate_ring(struct ring *r, int ring_len)
{
int len;
int ret = 0;
do
{
if ( (ring_len > XEN_ARGO_MAX_RING_SIZE) ||
(ring_len != XEN_ARGO_ROUNDUP(ring_len)) )
{
pr_debug("ring_len=%d\n", ring_len);
ret = -EINVAL;
break;
}
r->ring = NULL;
r->gfn_array = NULL;
r->order = 0;
len = ring_len + sizeof(xen_argo_ring_t);
r->order = get_order(len);
r->ring = vmalloc(len);
if ( !r->ring )
{
ret = -ENOMEM;
break;
}
// If this was exported it would be the perfect solution..
// vmalloc_sync_all();
memset((void *) r->ring, 0, len);
r->len = ring_len;
r->ring->rx_ptr = r->ring->tx_ptr = 0;
memset((void *) r->ring->ring, 0x5a, ring_len);
ret = allocate_gfn_array(r);
if ( ret )
{
break;
}
return 0;
}
while (1 == 0);
/* Error exit, tidy up */
if ( r->ring )
vfree(r->ring);
r->ring = NULL;
if (r->gfn_array)
kfree (r->gfn_array);
r->gfn_array = NULL;
return ret;
}
/*Caller must hold lock*/
static void
recover_ring(struct ring *r)
{
/*It's all gone horribly wrong*/
pr_warn("Something went horribly wrong in a ring - dumping and attempting a recovery\n");
/* Ring summary w/ dump start*/
pr_debug("ring at %p:\n", r);
pr_debug(" xen_pfn_array_t at %p for %d:\n",
r->gfn_array, r->npfns);
pr_debug(" xen_argo_ring_t at %p:\n", r->ring);
pr_debug(" r->rx_ptr=%d r->tx_ptr=%d r->len=%d\n",
r->ring->rx_ptr, r->ring->tx_ptr, r->len);
print_hex_dump_bytes("ring: ", DUMP_PREFIX_OFFSET, r->ring->ring, r->len);
/* Ring summary w/ dump end*/
/* Xen updates tx_ptr atomically to always be pointing somewhere sensible */
r->ring->rx_ptr = r->ring->tx_ptr;
}
/* Caller must hold no locks. Ring is allocated with a refcnt of 1. */
static int
new_ring(struct argo_private *sponsor, struct argo_ring_id *pid)
{
struct argo_ring_id id = *pid;
struct ring *r;
int ret;
if ( id.domain_id != XEN_ARGO_DOMID_ANY )
return -EINVAL;
r = kmalloc(sizeof(struct ring), GFP_KERNEL);
if ( !r )
return -ENOMEM;
memset (r, 0, sizeof(struct ring));
pr_debug("Requested size: %d\n", sponsor->desired_ring_size);
ret = allocate_ring(r, sponsor->desired_ring_size);
pr_debug("Allocate_ring returned: %d\n", ret);
if ( ret )
{
kfree(r);
return ret;
}
INIT_LIST_HEAD(&r->privates);
mutex_init(&r->lock);
atomic_set(&r->refcnt, 1);
do
{
/* ret = -EINVAL; kfree(r); return ret; DISABLE */
down_write(&list_sem);
if ( sponsor->state != ARGO_STATE_IDLE )
{
ret = -EINVAL;
break;
}
pr_debug("Requested aport: %u\n", id.aport);
if ( !id.aport )
{
id.aport = argo_find_spare_port_number ();
if ( id.aport == ARGO_PORTS_EXHAUSTED )
{
ret = -ENOSPC;
break;
}
}
else if ( argo_id_in_use (&id) )
{
ret = -EADDRINUSE;
break;
}
r->id.domain_id = id.domain_id;
r->id.aport = id.aport;
r->id.partner_id = id.partner_id;
r->len = sponsor->desired_ring_size;
r->sponsor = sponsor;
sponsor->r = r;
sponsor->state = ARGO_STATE_BOUND;
ret = register_ring(r);
if ( ret )
break;
list_add(&r->node, &ring_list);
up_write(&list_sem);
return 0;
}
while (0);
up_write(&list_sem);
vfree(r->ring);
kfree(r->gfn_array);
kfree(r);
sponsor->r = NULL;
sponsor->state = ARGO_STATE_IDLE;
return ret;
}
static void
free_ring (struct ring *r)
{
vfree(r->ring);
kfree(r->gfn_array);
kfree(r);
}
/* Cleans up old rings */
static void
delete_ring(struct ring *r)
{
int ret;
list_del (&r->node);
if ( (ret = unregister_ring(r)) )
pr_err("unregister_ring hypercall failed: %d.\n", ret);
}
/* Returns !0 if you sucessfully got a reference to the ring */
static int
get_ring(struct ring *r)
{
return atomic_add_unless(&r->refcnt, 1, 0);
}
/* must be called with DEBUG_WRITELOCK; argo_write_lock */
static int
put_ring(struct ring *r)
{
if ( !r )
return 0;
if ( atomic_dec_and_test(&r->refcnt) )
{
delete_ring(r);
return 1;
}
return 0;
}
/* caller must hold ring_lock */
static struct ring *
find_ring_by_id(struct argo_ring_id *id)
{
struct ring *r;
list_for_each_entry(r, &ring_list, node)
{
if ( (r->id.domain_id == id->domain_id) &&
(r->id.aport == id->aport) &&
(r->id.partner_id == id->partner_id) )
return r;
}
return NULL;
}
/* caller must hold ring_lock */
struct ring *
find_ring_by_id_type(struct argo_ring_id *id, argo_rtype t)
{
struct ring *r;
list_for_each_entry (r, &ring_list, node)
{
if ( r->type != t )
continue;
if ( (r->id.domain_id == id->domain_id) &&
(r->id.aport == id->aport) &&
(r->id.partner_id == id->partner_id) )
return r;
}
return NULL;
}
/************************ pending xmits ********************/
/*caller must hold pending_xmit_lock*/
static void
xmit_queue_wakeup_private(struct argo_ring_id *from,
uint32_t conid, xen_argo_addr_t *to, int len,
int delete)
{
struct pending_xmit *p;
list_for_each_entry(p, &pending_xmit_list, node)
{
if ( (p->type != ARGO_PENDING_XMIT_WAITQ_MATCH_PRIVATES) ||
(p->conid != conid) )
continue;
if ( (from->domain_id == p->from.domain_id) &&
(from->aport == p->from.aport) &&
(from->partner_id == p->from.partner_id)
&& (to->domain_id == p->to.domain_id) &&
(to->aport == p->to.aport) )
{
if ( delete )
{
atomic_dec (&pending_xmit_count);
list_del (&p->node);
}
else
p->len = len;
return;
}
}
if ( delete )
return;
p = kmalloc( sizeof(struct pending_xmit), GFP_KERNEL );
if ( !p )
{
pr_err("Out of memory trying to queue an xmit private wakeup\n");
return;
}
p->type = ARGO_PENDING_XMIT_WAITQ_MATCH_PRIVATES;
p->conid = conid;
p->from = *from;
p->to = *to;
p->len = len;
atomic_inc(&pending_xmit_count);
list_add_tail(&p->node, &pending_xmit_list);
}
/*caller must hold pending_xmit_lock*/
static void
xmit_queue_wakeup_sponsor(struct argo_ring_id *from, xen_argo_addr_t * to, int len,
int delete)
{
struct pending_xmit *p;
list_for_each_entry(p, &pending_xmit_list, node)
{
if ( p->type != ARGO_PENDING_XMIT_WAITQ_MATCH_SPONSOR )
continue;
if ( (from->domain_id == p->from.domain_id) &&
(from->aport == p->from.aport) &&
(from->partner_id == p->from.partner_id)
&& (to->domain_id == p->to.domain_id) &&
(to->aport == p->to.aport) )
{
if (delete)
{
atomic_dec(&pending_xmit_count);
list_del(&p->node);
}
else
p->len = len;
return;
}
}
if ( delete )
return;
p = kmalloc(sizeof(struct pending_xmit), GFP_KERNEL);
if ( !p )
{
pr_err("Out of memory trying to queue an xmit sponsor wakeup\n");
return;
}
p->type = ARGO_PENDING_XMIT_WAITQ_MATCH_SPONSOR;
p->from = *from;
p->to = *to;
p->len = len;
atomic_inc(&pending_xmit_count);
list_add_tail(&p->node, &pending_xmit_list);
}
static int
xmit_queue_inline(struct argo_ring_id *from, xen_argo_addr_t *to,
void *buf, size_t len, uint32_t protocol)
{
ssize_t ret;
xen_argo_iov_t iov;
struct pending_xmit *p;
xen_argo_addr_t addr;
mutex_lock(&pending_xmit_lock);
iov.iov_hnd = buf;
#ifdef CONFIG_ARM
iov.pad2 = 0;
#endif
iov.iov_len = len;
iov.pad = 0;
addr.aport = from->aport;
addr.domain_id = from->domain_id;
addr.pad = 0;
ret = H_argo_sendv(&addr, to, &iov, 1, protocol);
if (ret != -EAGAIN)
{
mutex_unlock(&pending_xmit_lock);
return ret;
}
p = kmalloc(sizeof(struct pending_xmit) + len, GFP_KERNEL);
if ( !p )
{
mutex_unlock(&pending_xmit_lock);
pr_err("Out of memory trying to queue an xmit of %zu bytes\n", len);
return -ENOMEM;
}
p->type = ARGO_PENDING_XMIT_INLINE;
p->from = *from;
p->to = *to;
p->len = len;
p->protocol = protocol;
if ( len )
memcpy(p->data, buf, len);
list_add_tail (&p->node, &pending_xmit_list);
atomic_inc (&pending_xmit_count);
mutex_unlock(&pending_xmit_lock);
return len;
}
static void
xmit_queue_rst_to(struct argo_ring_id *from, uint32_t conid, xen_argo_addr_t * to)
{
struct argo_stream_header sh;
if ( !to )
return;