-
Notifications
You must be signed in to change notification settings - Fork 0
/
userif.c
1066 lines (928 loc) · 26.7 KB
/
userif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************
* Copyright (C) 1998 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*********************************************************/
#include "driver-config.h"
#define EXPORT_SYMTAB
#define __KERNEL_SYSCALLS__
#include <linux/file.h>
#include <linux/highmem.h>
#include <linux/if_ether.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/mm.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/sockios.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/version.h>
#include <linux/wait.h>
#include <net/checksum.h>
#include <net/sock.h>
#include <asm/io.h>
#include "vnetInt.h"
#include "compat_skbuff.h"
#include "vmnetInt.h"
#include "vm_atomic.h"
#include "vm_assert.h"
#include "monitorAction_exported.h"
typedef struct VNetUserIFStats {
unsigned read;
unsigned written;
unsigned queued;
unsigned droppedDown;
unsigned droppedMismatch;
unsigned droppedOverflow;
unsigned droppedLargePacket;
} VNetUserIFStats;
typedef struct VNetUserIF {
VNetPort port;
struct sk_buff_head packetQueue;
uint32* pollPtr;
MonitorActionIntr *actionIntr;
uint32 pollMask;
MonitorIdemAction actionID;
uint32* recvClusterCount;
wait_queue_head_t waitQueue;
struct page* actPage;
struct page* pollPage;
struct page* recvClusterPage;
VNetUserIFStats stats;
VNetEvent_Sender *eventSender;
} VNetUserIF;
static void VNetUserIfUnsetupNotify(VNetUserIF *userIf);
static int VNetUserIfSetupNotify(VNetUserIF *userIf, VNet_Notify *vn);
static int VNetUserIfSetUplinkState(VNetPort *port, uint8 linkUp);
/*
*-----------------------------------------------------------------------------
*
* UserifLockPage --
*
* Lock in core the physical page associated to a valid virtual
* address.
*
* Results:
* The page structure on success
* NULL on failure: memory pressure. Retry later
*
* Side effects:
* Loads page into memory
*
*-----------------------------------------------------------------------------
*/
static INLINE struct page *
UserifLockPage(VA addr) // IN
{
struct page *page = NULL;
int retval;
down_read(¤t->mm->mmap_sem);
retval = get_user_pages(current, current->mm, addr,
1, 1, 0, &page, NULL);
up_read(¤t->mm->mmap_sem);
if (retval != 1) {
return NULL;
}
return page;
}
/*
*-----------------------------------------------------------------------------
*
* VNetUserIfMapUint32Ptr --
*
* Maps a portion of user-space memory into the kernel.
*
* Results:
* 0 on success
* < 0 on failure: the actual value determines the type of failure
*
* Side effects:
* Might sleep.
*
*-----------------------------------------------------------------------------
*/
static INLINE int
VNetUserIfMapPtr(VA uAddr, // IN: pointer to user memory
size_t size, // IN: size of data
struct page **p, // OUT: locked page
void **ptr) // OUT: kernel mapped pointer
{
if (!access_ok(VERIFY_WRITE, (void *)uAddr, size) ||
(((uAddr + size - 1) & ~(PAGE_SIZE - 1)) !=
(uAddr & ~(PAGE_SIZE - 1)))) {
return -EINVAL;
}
*p = UserifLockPage(uAddr);
if (*p == NULL) {
return -EAGAIN;
}
*ptr = (uint8 *)kmap(*p) + (uAddr & (PAGE_SIZE - 1));
return 0;
}
static INLINE int
VNetUserIfMapUint32Ptr(VA uAddr, // IN: pointer to user memory
struct page **p, // OUT: locked page
uint32 **ptr) // OUT: kernel mapped pointer
{
return VNetUserIfMapPtr(uAddr, sizeof **ptr, p, (void **)ptr);
}
/*
*-----------------------------------------------------------------------------
*
* VNetUserIfSetupNotify --
*
* Sets up notification by filling in pollPtr, actPtr, and recvClusterCount
* fields.
*
* Results:
* 0 on success
* < 0 on failure: the actual value determines the type of failure
*
* Side effects:
* Fields pollPtr, actPtr, recvClusterCount, pollPage, actPage, and
* recvClusterPage are filled in VNetUserIf structure.
*
*-----------------------------------------------------------------------------
*/
static INLINE int
VNetUserIfSetupNotify(VNetUserIF *userIf, // IN
VNet_Notify *vn) // IN
{
int retval;
if (userIf->pollPtr || userIf->actionIntr || userIf->recvClusterCount) {
LOG(0, (KERN_DEBUG "vmnet: Notification mechanism already active\n"));
return -EBUSY;
}
if ((retval = VNetUserIfMapUint32Ptr((VA)vn->pollPtr, &userIf->pollPage,
&userIf->pollPtr)) < 0) {
return retval;
}
if ((retval = VNetUserIfMapPtr((VA)vn->actPtr, sizeof *userIf->actionIntr,
&userIf->actPage,
(void **)&userIf->actionIntr)) < 0) {
VNetUserIfUnsetupNotify(userIf);
return retval;
}
if ((retval = VNetUserIfMapUint32Ptr((VA)vn->recvClusterPtr,
&userIf->recvClusterPage,
&userIf->recvClusterCount)) < 0) {
VNetUserIfUnsetupNotify(userIf);
return retval;
}
userIf->pollMask = vn->pollMask;
userIf->actionID = vn->actionID;
return 0;
}
/*
*----------------------------------------------------------------------
*
* VNetUserIfUnsetupNotify --
*
* Destroys permanent mapping for notify structure provided by user.
*
* Results:
* None.
*
* Side effects:
* Fields pollPtr, actPtr, recvClusterCount, etc. in VNetUserIf
* structure are cleared.
*
*----------------------------------------------------------------------
*/
static void
VNetUserIfUnsetupNotify(VNetUserIF *userIf) // IN
{
if (userIf->pollPage) {
kunmap(userIf->pollPage);
put_page(userIf->pollPage);
} else {
LOG(0, (KERN_DEBUG "vmnet: pollPtr was already deactivated\n"));
}
if (userIf->actPage) {
kunmap(userIf->actPage);
put_page(userIf->actPage);
} else {
LOG(0, (KERN_DEBUG "vmnet: actPtr was already deactivated\n"));
}
if (userIf->recvClusterPage) {
kunmap(userIf->recvClusterPage);
put_page(userIf->recvClusterPage);
} else {
LOG(0, (KERN_DEBUG "vmnet: recvClusterPtr was already deactivated\n"));
}
userIf->pollPtr = NULL;
userIf->pollPage = NULL;
userIf->actionIntr = NULL;
userIf->actPage = NULL;
userIf->recvClusterCount = NULL;
userIf->recvClusterPage = NULL;
userIf->pollMask = 0;
userIf->actionID = -1;
}
/*
*----------------------------------------------------------------------
*
* VNetUserIfFree --
*
* Free the user interface port.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
VNetUserIfFree(VNetJack *this) // IN
{
VNetUserIF *userIf = (VNetUserIF*)this;
struct sk_buff *skb;
for (;;) {
skb = skb_dequeue(&userIf->packetQueue);
if (skb == NULL) {
break;
}
dev_kfree_skb(skb);
}
if (userIf->pollPtr) {
VNetUserIfUnsetupNotify(userIf);
}
if (userIf->eventSender) {
VNetEvent_DestroySender(userIf->eventSender);
}
if (this->procEntry) {
VNetProc_RemoveEntry(this->procEntry);
}
kfree(userIf);
}
/*
*----------------------------------------------------------------------
*
* VNetUserIfReceive --
*
* This jack is receiving a packet. Take appropriate action.
*
* Results:
* None.
*
* Side effects:
* Frees skb.
*
*----------------------------------------------------------------------
*/
static void
VNetUserIfReceive(VNetJack *this, // IN
struct sk_buff *skb) // IN
{
VNetUserIF *userIf = (VNetUserIF*)this->private;
uint8 *dest = SKB_2_DESTMAC(skb);
if (!UP_AND_RUNNING(userIf->port.flags)) {
userIf->stats.droppedDown++;
goto drop_packet;
}
if (!VNetPacketMatch(dest,
userIf->port.paddr,
(const uint8 *)userIf->port.exactFilter,
userIf->port.exactFilterLen,
userIf->port.ladrf,
userIf->port.flags)) {
userIf->stats.droppedMismatch++;
goto drop_packet;
}
if (skb_queue_len(&userIf->packetQueue) >= VNET_MAX_QLEN) {
userIf->stats.droppedOverflow++;
goto drop_packet;
}
if (skb->len > ETHER_MAX_QUEUED_PACKET) {
userIf->stats.droppedLargePacket++;
goto drop_packet;
}
userIf->stats.queued++;
skb_queue_tail(&userIf->packetQueue, skb);
if (userIf->pollPtr) {
*userIf->pollPtr |= userIf->pollMask;
if (skb_queue_len(&userIf->packetQueue) >= (*userIf->recvClusterCount)) {
MonitorAction_SetBits(userIf->actionIntr, userIf->actionID);
}
}
wake_up(&userIf->waitQueue);
return;
drop_packet:
dev_kfree_skb(skb);
}
/*
*----------------------------------------------------------------------
*
* VNetUserIfProcRead --
*
* Callback for read operation on this userif entry in vnets proc fs.
*
* Results:
* Length of read operation.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
VNetUserIfProcRead(char *page, // IN/OUT: buffer to write into
char **start, // OUT: 0 if file < 4k, else offset into
// page
off_t off, // IN: offset of read into the file
int count, // IN: maximum number of bytes to read
int *eof, // OUT: TRUE if there is nothing more to
// read
void *data) // IN: client data - not used
{
VNetUserIF *userIf = (VNetUserIF*)data;
int len = 0;
if (!userIf) {
return len;
}
len += VNetPrintPort(&userIf->port, page+len);
len += sprintf(page+len, "read %u written %u queued %u ",
userIf->stats.read,
userIf->stats.written,
userIf->stats.queued);
len += sprintf(page+len,
"dropped.down %u dropped.mismatch %u "
"dropped.overflow %u dropped.largePacket %u",
userIf->stats.droppedDown,
userIf->stats.droppedMismatch,
userIf->stats.droppedOverflow,
userIf->stats.droppedLargePacket);
len += sprintf(page+len, "\n");
*start = 0;
*eof = 1;
return len;
}
/*
*----------------------------------------------------------------------
*
* VNetCopyDatagram --
*
* Copy part of datagram to userspace.
*
* Results:
* zero on success,
* -EFAULT if buffer is an invalid area
*
* Side effects:
* Data copied to the buffer.
*
*----------------------------------------------------------------------
*/
static int
VNetCopyDatagram(const struct sk_buff *skb, // IN: skb to copy
char *buf, // OUT: where to copy data
int len) // IN: length
{
struct iovec iov = {
.iov_base = buf,
.iov_len = len,
};
return skb_copy_datagram_iovec(skb, 0, &iov, len);
}
/*
*----------------------------------------------------------------------
*
* VNetCsumCopyDatagram --
*
* Copy part of datagram to userspace doing checksum at same time.
*
* Do not mark this function INLINE, it is recursive! With all gcc's
* released up to now (<= gcc-3.3.1) inlining this function just
* consumes 120 more bytes of code and goes completely mad on
* register allocation, storing almost everything in the memory.
*
* Results:
* folded checksum (non-negative value) on success,
* -EINVAL if offset is too big,
* -EFAULT if buffer is an invalid area
*
* Side effects:
* Data copied to the buffer.
*
*----------------------------------------------------------------------
*/
static int
VNetCsumCopyDatagram(const struct sk_buff *skb, // IN: skb to copy
unsigned int offset, // IN: how many bytes skip
char *buf) // OUT: where to copy data
{
unsigned int csum;
int err = 0;
int len = skb_headlen(skb) - offset;
char *curr = buf;
const skb_frag_t *frag;
/*
* Something bad happened. We skip only up to skb->nh.raw, and skb->nh.raw
* must be in the header, otherwise we are in the big troubles.
*/
if (len < 0) {
return -EINVAL;
}
csum = csum_and_copy_to_user(skb->data + offset, curr, len, 0, &err);
if (err) {
return err;
}
curr += len;
for (frag = skb_shinfo(skb)->frags;
frag != skb_shinfo(skb)->frags + skb_shinfo(skb)->nr_frags;
frag++) {
if (frag->size > 0) {
unsigned int tmpCsum;
const void *vaddr;
vaddr = kmap(skb_frag_page(frag));
tmpCsum = csum_and_copy_to_user(vaddr + frag->page_offset,
curr, frag->size, 0, &err);
kunmap(skb_frag_page(frag));
if (err) {
return err;
}
csum = csum_block_add(csum, tmpCsum, curr - buf);
curr += frag->size;
}
}
for (skb = skb_shinfo(skb)->frag_list; skb != NULL; skb = skb->next) {
int tmpCsum;
tmpCsum = VNetCsumCopyDatagram(skb, 0, curr);
if (tmpCsum < 0) {
return tmpCsum;
}
/* Folded checksum must be inverted before we can use it */
csum = csum_block_add(csum, tmpCsum ^ 0xFFFF, curr - buf);
curr += skb->len;
}
return csum_fold(csum);
}
/*
*----------------------------------------------------------------------
*
* VNetCopyDatagramToUser --
*
* Copy complete datagram to the user space. Fill correct checksum
* into the copied datagram if nobody did it yet.
*
* Results:
* On success byte count, on failure -EFAULT.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static INLINE_SINGLE_CALLER int
VNetCopyDatagramToUser(const struct sk_buff *skb, // IN
char *buf, // OUT
size_t count) // IN
{
if (count > skb->len) {
count = skb->len;
}
/*
* If truncation occurs, we do not bother with checksumming - caller cannot
* verify checksum anyway in such case, and copy without checksum is
* faster.
*/
if (skb->pkt_type == PACKET_OUTGOING && /* Packet must be outgoing */
skb->ip_summed == VM_TX_CHECKSUM_PARTIAL && /* Without checksum */
compat_skb_network_header_len(skb) && /* We must know where header is */
skb->len == count) { /* No truncation may occur */
size_t skl;
int csum;
u_int16_t csum16;
skl = compat_skb_csum_start(skb);
if (VNetCopyDatagram(skb, buf, skl)) {
return -EFAULT;
}
csum = VNetCsumCopyDatagram(skb, skl, buf + skl);
if (csum < 0) {
return csum;
}
csum16 = csum;
if (copy_to_user(buf + skl + compat_skb_csum_offset(skb),
&csum16, sizeof csum16)) {
return -EFAULT;
}
} else {
if (VNetCopyDatagram(skb, buf, count)) {
return -EFAULT;
}
}
return count;
}
/*
*----------------------------------------------------------------------
*
* VNetUserIfRead --
*
* The virtual network's read file operation. Reads the next pending
* packet for this network connection.
*
* Results:
* On success the len of the packet received,
* else if no packet waiting and nonblocking 0,
* else -errno.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
VNetUserIfRead(VNetPort *port, // IN
struct file *filp, // IN
char *buf, // OUT
size_t count) // IN
{
VNetUserIF *userIf = (VNetUserIF*)port->jack.private;
struct sk_buff *skb;
int ret;
DECLARE_WAITQUEUE(wait, current);
add_wait_queue(&userIf->waitQueue, &wait);
for (;;) {
set_current_state(TASK_INTERRUPTIBLE);
skb = skb_peek(&userIf->packetQueue);
if (skb && (skb->len > count)) {
skb = NULL;
ret = -EMSGSIZE;
break;
}
ret = -EAGAIN;
skb = skb_dequeue(&userIf->packetQueue);
if (userIf->pollPtr) {
if (skb_queue_empty(&userIf->packetQueue)) {
*userIf->pollPtr &= ~userIf->pollMask;
}
}
if (skb != NULL || filp->f_flags & O_NONBLOCK) {
break;
}
ret = -EINTR;
if (signal_pending(current)) {
break;
}
schedule();
}
__set_current_state(TASK_RUNNING);
remove_wait_queue(&userIf->waitQueue, &wait);
if (! skb) {
return ret;
}
userIf->stats.read++;
count = VNetCopyDatagramToUser(skb, buf, count);
dev_kfree_skb(skb);
return count;
}
/*
*----------------------------------------------------------------------
*
* VNetUserIfWrite --
*
* The virtual network's write file operation. Send the raw packet
* to the network.
*
* Results:
* On success the count of bytes written else errno.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
VNetUserIfWrite(VNetPort *port, // IN
struct file *filp, // IN
const char *buf, // IN
size_t count) // IN
{
VNetUserIF *userIf = (VNetUserIF*)port->jack.private;
struct sk_buff *skb;
/*
* Check size
*/
if (count < sizeof (struct ethhdr) ||
count > ETHER_MAX_QUEUED_PACKET) {
return -EINVAL;
}
/*
* Required to enforce the downWhenAddrMismatch policy in the MAC
* layer. --hpreg
*/
if (!UP_AND_RUNNING(userIf->port.flags)) {
userIf->stats.droppedDown++;
return count;
}
/*
* Allocate an sk_buff.
*/
skb = dev_alloc_skb(count + 7);
if (skb == NULL) {
// XXX obey O_NONBLOCK?
return -ENOBUFS;
}
skb_reserve(skb, 2);
/*
* Copy the data and send it.
*/
userIf->stats.written++;
if (copy_from_user(skb_put(skb, count), buf, count)) {
dev_kfree_skb(skb);
return -EFAULT;
}
VNetSend(&userIf->port.jack, skb);
return count;
}
/*
*-----------------------------------------------------------------------------
*
* VNetUserIfIoctl --
*
* XXX
*
* Results:
* 0 on success
* -errno on failure
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static int
VNetUserIfIoctl(VNetPort *port, // IN
struct file *filp, // IN
unsigned int iocmd, // IN
unsigned long ioarg) // IN or OUT depending on iocmd
{
VNetUserIF *userIf = (VNetUserIF*)port->jack.private;
switch (iocmd) {
case SIOCSETNOTIFY:
return -EINVAL;
case SIOCSETNOTIFY2:
#ifdef VMX86_SERVER
/*
* This ioctl always return failure on ESX since we cannot map pages into
* the console os that are from the VMKernel address space which was the
* only case we used this.
*/
return -EINVAL;
#else // VMX86_SERVER
/*
* ORs pollMask into the integer pointed to by ptr if pending packet. Is
* cleared when all packets are drained.
*/
{
int retval;
VNet_Notify vn;
if (copy_from_user(&vn, (void *)ioarg, sizeof vn)) {
return -EFAULT;
}
ASSERT_ON_COMPILE(VNET_NOTIFY_VERSION == 5);
ASSERT_ON_COMPILE(ACTION_EXPORTED_VERSION == 2);
if (vn.version != VNET_NOTIFY_VERSION ||
vn.actionVersion != ACTION_EXPORTED_VERSION ||
vn.actionID / ACTION_WORD_SIZE >= ACTION_NUM_WORDS) {
return -ENOTTY;
}
retval = VNetUserIfSetupNotify(userIf, &vn);
if (retval < 0) {
return retval;
}
break;
}
#endif // VMX86_SERVER
case SIOCUNSETNOTIFY:
if (!userIf->pollPtr) {
/* This should always happen on ESX. */
return -EINVAL;
}
VNetUserIfUnsetupNotify(userIf);
break;
case SIOCSIFFLAGS:
/*
* Drain queue when interface is no longer active. We drain the queue to
* avoid having old packets delivered to the guest when reneabled.
*/
if (!UP_AND_RUNNING(userIf->port.flags)) {
struct sk_buff *skb;
while ((skb = skb_dequeue(&userIf->packetQueue)) != NULL) {
dev_kfree_skb(skb);
}
if (userIf->pollPtr) {
/* Clear the pending bit as no packets are pending at this point. */
*userIf->pollPtr &= ~userIf->pollMask;
}
}
break;
case SIOCINJECTLINKSTATE:
{
uint8 linkUpFromUser;
if (copy_from_user(&linkUpFromUser, (void *)ioarg,
sizeof linkUpFromUser)) {
return -EFAULT;
}
if (linkUpFromUser != 0 && linkUpFromUser != 1) {
return -EINVAL;
}
return VNetUserIfSetUplinkState(port, linkUpFromUser);
}
break;
default:
return -ENOIOCTLCMD;
break;
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* VNetUserIfPoll --
*
* The virtual network's file poll operation.
*
* Results:
* Return POLLIN if success, else sleep and return 0.
* FIXME: Should not we always return POLLOUT?
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
VNetUserIfPoll(VNetPort *port, // IN
struct file *filp, // IN
poll_table *wait) // IN
{
VNetUserIF *userIf = (VNetUserIF*)port->jack.private;
poll_wait(filp, &userIf->waitQueue, wait);
if (!skb_queue_empty(&userIf->packetQueue)) {
return POLLIN;
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* VNetUserIfSetUplinkState --
*
* Sends link state change event.
*
* Results:
* 0 on success, errno on failure.
*
* Side effects:
* Link state event is sent to all the event listeners
*
*----------------------------------------------------------------------
*/
int
VNetUserIfSetUplinkState(VNetPort *port, uint8 linkUp)
{
VNetUserIF *userIf;
VNetJack *hubJack;
VNet_LinkStateEvent event;
int retval;
userIf = (VNetUserIF *)port->jack.private;
hubJack = port->jack.peer;
if (hubJack == NULL) {
return -EINVAL;
}
if (userIf->eventSender == NULL) {
/* create event sender */
retval = VNetHub_CreateSender(hubJack, &userIf->eventSender);
if (retval != 0) {
return retval;
}
}
event.header.size = sizeof event;
retval = VNetEvent_GetSenderId(userIf->eventSender, &event.header.senderId);
if (retval != 0) {
LOG(1, (KERN_NOTICE "userif-%d: can't send link state event, "
"getSenderId failed (%d)\n", userIf->port.id, retval));
return retval;
}
event.header.eventId = 0;
event.header.classSet = VNET_EVENT_CLASS_UPLINK;
event.header.type = VNET_EVENT_TYPE_LINK_STATE;
/*
* XXX kind of a hack, vmx will coalesce linkup/down if they come from the
* same adapter.
*/
event.adapter = linkUp;
event.up = linkUp;
retval = VNetEvent_Send(userIf->eventSender, &event.header);
if (retval != 0) {
LOG(1, (KERN_NOTICE "userif-%d: can't send link state event, send "
"failed (%d)\n", userIf->port.id, retval));
}
LOG(0, (KERN_NOTICE "userif-%d: sent link %s event.",
userIf->port.id, linkUp?"up":"down"));
return retval;
}
/*
*----------------------------------------------------------------------
*
* VNetUserIf_Create --
*
* Create a user level port to the wonderful world of virtual
* networking.
*
* Results:
* Errno. Also returns an allocated port to connect to,
* NULL on error.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
VNetUserIf_Create(VNetPort **ret) // OUT
{
VNetUserIF *userIf;
static unsigned id = 0;
int retval;
userIf = kmalloc(sizeof *userIf, GFP_USER);
if (!userIf) {
return -ENOMEM;
}
/*
* Initialize fields.
*/
userIf->port.id = id++;