-
Notifications
You must be signed in to change notification settings - Fork 171
/
vr_uvhost_msg.c
1172 lines (1006 loc) · 36.1 KB
/
vr_uvhost_msg.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
/*
* vr_uvhost_msg.c - handlers for messages received by the user space
* vhost thread.
*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
#include <sys/poll.h>
#include "vr_dpdk.h"
#include "vr_dpdk_virtio.h"
#include "vr_dpdk_usocket.h"
#include "vr_uvhost_client.h"
#include "vr_uvhost_msg.h"
#include "vr_uvhost_util.h"
#include <fcntl.h>
#include <linux/virtio_net.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <rte_errno.h>
#include <rte_hexdump.h>
typedef int (*vr_uvh_msg_handler_fn)(vr_uvh_client_t *vru_cl);
#define uvhm_client_name(vru_cl) (vru_cl->vruc_path + strlen(vr_socket_dir) \
+ sizeof(VR_UVH_VIF_PFX) - 1)
/*
* Prototypes for user space vhost message handlers
*/
static int vr_uvmh_get_features(vr_uvh_client_t *vru_cl);
static int vr_uvmh_set_features(vr_uvh_client_t *vru_cl);
static int vr_uvmh_get_protocol_features(vr_uvh_client_t *vru_cl);
static int vr_uvmh_set_protocol_features(vr_uvh_client_t *vru_cl);
static int vr_uvhm_set_mem_table(vr_uvh_client_t *vru_cl);
static int vr_uvhm_set_vring_num(vr_uvh_client_t *vru_cl);
static int vr_uvhm_set_vring_addr(vr_uvh_client_t *vru_cl);
static int vr_uvhm_set_vring_base(vr_uvh_client_t *vru_cl);
static int vr_uvhm_get_vring_base(vr_uvh_client_t *vru_cl);
static int vr_uvhm_set_vring_call(vr_uvh_client_t *vru_cl);
static int vr_uvhm_get_queue_num(vr_uvh_client_t *vru_cl);
static int vr_uvhm_set_vring_enable(vr_uvh_client_t *vru_cl);
static vr_uvh_msg_handler_fn vr_uvhost_cl_msg_handlers[] = {
NULL,
vr_uvmh_get_features,
vr_uvmh_set_features,
NULL,
NULL,
vr_uvhm_set_mem_table,
NULL,
NULL,
vr_uvhm_set_vring_num,
vr_uvhm_set_vring_addr,
vr_uvhm_set_vring_base,
vr_uvhm_get_vring_base,
NULL,
vr_uvhm_set_vring_call,
NULL,
vr_uvmh_get_protocol_features,
vr_uvmh_set_protocol_features,
vr_uvhm_get_queue_num,
vr_uvhm_set_vring_enable,
NULL,
};
/*
* uvhm_mem_table_mmap - mmaps guest memory regions.
*
* Returns 0 on success, -1 otherwise.
*/
static int
uvhm_client_mmap(vr_uvh_client_t *vru_cl)
{
int i;
int ret;
vr_uvh_client_mem_region_t *region;
VhostUserMemory *vum_msg;
uint64_t size;
vum_msg = &vru_cl->vruc_msg.memory;
vr_uvhost_log("Client %s: mapping %u memory regions:\n",
uvhm_client_name(vru_cl), vum_msg->nregions);
if (vum_msg->nregions > VHOST_MEMORY_MAX_NREGIONS) {
vr_uvhost_log("Client %s: error mapping guest memory: too many regions"
"(%"PRIu32" > %d)\n",
uvhm_client_name(vru_cl), vum_msg->nregions,
VHOST_MEMORY_MAX_NREGIONS);
return -1;
}
for (i = 0; i < vum_msg->nregions; i++) {
vr_uvhost_log(" %d: FD %d addr 0x%" PRIx64 " size 0x%"
PRIx64 " off 0x%" PRIx64 "\n",
i, vru_cl->vruc_fds_sent[i],
vum_msg->regions[i].guest_phys_addr,
vum_msg->regions[i].memory_size,
vum_msg->regions[i].mmap_offset);
if (vru_cl->vruc_fds_sent[i]) {
region = &vru_cl->vruc_mem_regions[i];
region->vrucmr_phys_addr = vum_msg->regions[i].guest_phys_addr;
region->vrucmr_size = vum_msg->regions[i].memory_size;
region->vrucmr_user_space_addr = vum_msg->regions[i].userspace_addr;
size = vum_msg->regions[i].mmap_offset +
vum_msg->regions[i].memory_size;
region->vrucmr_mmap_addr = (uint64_t)
mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED,
vru_cl->vruc_fds_sent[i], 0);
if (region->vrucmr_mmap_addr == ((uint64_t)MAP_FAILED)) {
vr_uvhost_log("Client %s: error mmaping FD %d size 0x%" PRIx64
": %s (%d)\n",
uvhm_client_name(vru_cl),
vru_cl->vruc_fds_sent[i], size,
rte_strerror(errno), errno);
/*
* The file descriptors will be closed in vr_uvh_cl_msg_handler()
*/
return -1;
}
/* Get block size for the munmap(2). */
ret = vr_dpdk_virtio_uvh_get_blk_size(vru_cl->vruc_fds_sent[i],
®ion->vrucmr_blksize);
if (ret) {
vr_uvhost_log("Client %s: error getting block size for FD %d\n",
uvhm_client_name(vru_cl),
vru_cl->vruc_fds_sent[i]);
return -1;
}
region->vrucmr_mmap_addr_aligned = (void *)(uintptr_t)
RTE_ALIGN_FLOOR(region->vrucmr_mmap_addr,
region->vrucmr_blksize);
region->vrucmr_size_aligned = RTE_ALIGN_CEIL(size,
region->vrucmr_blksize);
/*
* Prevent guest memory from being dumped in vrouter-dpdk core.
*/
if (madvise(region->vrucmr_mmap_addr_aligned,
region->vrucmr_size_aligned, MADV_DONTDUMP)) {
vr_uvhost_log("Client %s: error in madvise at addr 0x%" PRIx64 ", size 0x%"
PRIx64 "for FD %d: %s (%d)\n",
uvhm_client_name(vru_cl),
region->vrucmr_mmap_addr,
size, vru_cl->vruc_fds_sent[i],
rte_strerror(errno), errno);
/*
* Failure is not catastrophic, so continue below.
*/
}
/* The file descriptor is no longer needed. */
close(vru_cl->vruc_fds_sent[i]);
vru_cl->vruc_fds_sent[i] = -1;
region->vrucmr_mmap_addr += vum_msg->regions[i].mmap_offset;
}
}
/* Save the number of regions. */
vru_cl->vruc_num_mem_regions = vum_msg->nregions;
return 0;
}
/*
* uvhm_mem_table_munmap - munmaps guest memory regions.
*/
static void
uvhm_client_munmap(vr_uvh_client_t *vru_cl)
{
int i, ret;
vr_uvh_client_mem_region_t *region;
/* Make sure the device has stopped before the munmap. */
vr_dpdk_virtio_stop(vru_cl->vruc_idx);
vr_uvhost_log("Client %s: unmapping %u memory regions:\n",
uvhm_client_name(vru_cl), vru_cl->vruc_num_mem_regions);
for (i = 0; i < vru_cl->vruc_num_mem_regions; i++) {
region = &vru_cl->vruc_mem_regions[i];
if (region->vrucmr_mmap_addr_aligned) {
vr_uvhost_log(" %d: unmapping addr 0x%"PRIx64" size 0x%"PRIx64
"\n", i, region->vrucmr_phys_addr, region->vrucmr_size);
ret = munmap(region->vrucmr_mmap_addr_aligned,
region->vrucmr_size_aligned);
if (ret) {
vr_uvhost_log(
"Client %s: error unmapping memory region %d: %s (%d)\n",
uvhm_client_name(vru_cl), i, strerror(errno), errno);
}
}
}
/*
* Possible memory leak when munmap fails. At this moment there is no
* solution for that.
*/
memset(vru_cl->vruc_mem_regions, 0, sizeof(vru_cl->vruc_mem_regions));
vru_cl->vruc_num_mem_regions = 0;
return;
}
/*
* vr_uvmh_get_features - handle VHOST_USER_GET_FEATURES message from user space
* vhost client.
*
* Returns 0 on success, -1 otherwise.
*/
static int
vr_uvmh_get_features(vr_uvh_client_t *vru_cl)
{
/* TODO: Implement VHOST_F_LOG_ALL handler */
/* VIRTIO_NET_F_CTRL_VQ is enough for vMX and FreeBSD */
vru_cl->vruc_msg.u64 = (1ULL << VIRTIO_NET_F_CTRL_VQ) |
(1ULL << VIRTIO_NET_F_CSUM) |
(1ULL << VIRTIO_NET_F_GUEST_CSUM) |
(1ULL << VIRTIO_NET_F_MQ) |
(1ULL << VHOST_USER_F_PROTOCOL_FEATURES) |
(1ULL << VHOST_F_LOG_ALL);
vr_uvhost_log(" GET FEATURES: returns 0x%"PRIx64"\n",
vru_cl->vruc_msg.u64);
vru_cl->vruc_msg.size = sizeof(vru_cl->vruc_msg.u64);
return 0;
}
/*
* vr_uvmh_set_features - handle VHOST_USER_SET_FEATURES message from user space
* vhost client.
*
* Returns 0 on success, -1 otherwise.
*/
static int
vr_uvmh_set_features(vr_uvh_client_t *vru_cl)
{
vr_uvhost_log(" SET FEATURES: 0x%"PRIx64"\n",
vru_cl->vruc_msg.u64);
return 0;
}
static int
vr_uvmh_get_protocol_features(vr_uvh_client_t *vru_cl)
{
vru_cl->vruc_msg.u64 = (1ULL << VHOST_USER_PROTOCOL_F_MQ);
vr_uvhost_log(" GET PROTOCOL FEATURES: returns 0x%"PRIx64"\n",
vru_cl->vruc_msg.u64);
vru_cl->vruc_msg.size = sizeof(vru_cl->vruc_msg.u64);
return 0;
}
static int
vr_uvmh_set_protocol_features(vr_uvh_client_t *vru_cl)
{
vr_uvhost_log(" SET PROTOCOL FEATURES: 0x%"PRIx64"\n",
vru_cl->vruc_msg.u64);
return 0;
}
/*
* vr_uvhm_set_mem_table - handles VHOST_USER_SET_MEM_TABLE message from
* user space vhost client to learn the memory map of the guest.
*
* Returns 0 on success, -1 otherwise.
*/
static int
vr_uvhm_set_mem_table(vr_uvh_client_t *vru_cl)
{
vr_uvhost_log(" SET MEM TABLE:\n");
/* Unmap previously mmaped guest memory. */
uvhm_client_munmap(vru_cl);
return uvhm_client_mmap(vru_cl);
}
/*
* vr_uvhm_set_vring_num - handles VHOST_USER_SET_VRING_NUM message from
* the user space vhost client to set the number of descriptors in the virtio
* ring.
*
* Returns 0 on success, -1 otherwise.
*/
static int
vr_uvhm_set_vring_num(vr_uvh_client_t *vru_cl)
{
VhostUserMsg *vum_msg;
unsigned int vring_idx;
vum_msg = &vru_cl->vruc_msg;
vring_idx = vum_msg->state.index;
vr_uvhost_log(" SET VRING NUM: vring %u num %u\n", vring_idx,
vum_msg->state.num);
if (vring_idx >= VHOST_CLIENT_MAX_VRINGS) {
vr_uvhost_log("Client %s: error setting vring %u num: invalid vring index\n",
uvhm_client_name(vru_cl), vring_idx);
return -1;
}
if (vr_dpdk_set_ring_num_desc(vru_cl->vruc_idx, vring_idx,
vum_msg->state.num)) {
vr_uvhost_log("Client %s: error setting vring %u size %u\n",
uvhm_client_name(vru_cl), vring_idx, vum_msg->state.num);
return -1;
}
return 0;
}
/*
* vr_uvhm_map_addr - map a virtual address sent by the vhost client into
* a server virtual address.
*
* Returns a pointer to the corresponding location on success, NULL otherwise.
*/
static void *
vr_uvhm_map_addr(vr_uvh_client_t *vru_cl, uint64_t addr)
{
int i;
uint64_t vmr_addr, vmr_size, ret_addr;
for (i = 0; i < vru_cl->vruc_num_mem_regions; i++) {
vmr_addr = vru_cl->vruc_mem_regions[i].vrucmr_user_space_addr;
vmr_size = vru_cl->vruc_mem_regions[i].vrucmr_size;
if ((vmr_addr <= addr) && (addr < (vmr_addr + vmr_size))) {
ret_addr = vru_cl->vruc_mem_regions[i].vrucmr_mmap_addr +
(addr - vmr_addr);
return (void *) ret_addr;
}
}
return NULL;
}
/*
* uvhm_check_vring_ready - check if virtual queue is ready to use and
* set the ready status.
*
* Returns 1 if vring ready, 0 otherwise.
*/
static int
uvhm_check_vring_ready(vr_uvh_client_t *vru_cl, unsigned int vring_idx)
{
unsigned int vif_idx = vru_cl->vruc_idx;
vr_dpdk_virtioq_t *vq;
if (vif_idx >= VR_MAX_INTERFACES) {
return 0;
}
if (vring_idx & 1) {
vq = &vr_dpdk_virtio_rxqs[vif_idx][vring_idx/2];
} else {
vq = &vr_dpdk_virtio_txqs[vif_idx][vring_idx/2];
}
/* vring is ready when addresses are set. */
if (vq->vdv_desc && vq->vdv_ready_state != VQ_READY) {
/*
* Now the virtio queue is ready for forwarding.
* TODO - need a memory barrier here for non-x86 CPU?
*/
if (vr_dpdk_set_virtq_ready(vru_cl->vruc_idx, vring_idx, VQ_READY)) {
vr_uvhost_log("Client %s: error setting vring %u ready state\n",
uvhm_client_name(vru_cl), vring_idx);
return -1;
}
vr_uvhost_log("Client %s: vring %d is ready\n",
uvhm_client_name(vru_cl), vring_idx);
return 1;
}
return 0;
}
/*
* vr_uvhm_set_vring_addr - handles a VHOST_USER_SET_VRING_ADDR message from
* the user space vhost client to set the address of the virtio rings.
*
* Returns 0 on success, -1 otherwise.
*/
static int
vr_uvhm_set_vring_addr(vr_uvh_client_t *vru_cl)
{
struct vhost_vring_addr *vaddr;
unsigned int vring_idx;
struct vring_desc *vrucv_desc;
struct vring_avail *vrucv_avail;
struct vring_used *vrucv_used;
vaddr = &vru_cl->vruc_msg.addr;
vring_idx = vaddr->index;
vr_uvhost_log(" SET VRING ADDR: vring %u flags 0x%x desc 0x%llx"
" used 0x%llx avail 0x%llx\n",
vring_idx, vaddr->flags, vaddr->desc_user_addr,
vaddr->used_user_addr, vaddr->avail_user_addr);
if (vring_idx >= VHOST_CLIENT_MAX_VRINGS) {
vr_uvhost_log("Client %s: error setting vring %u addr: invalid vring index\n",
uvhm_client_name(vru_cl), vring_idx);
return -1;
}
vrucv_desc = (struct vring_desc *)
vr_uvhm_map_addr(vru_cl, vaddr->desc_user_addr);
vrucv_avail = (struct vring_avail *)
vr_uvhm_map_addr(vru_cl, vaddr->avail_user_addr);
vrucv_used = (struct vring_used *)
vr_uvhm_map_addr(vru_cl, vaddr->used_user_addr);
if (!vrucv_desc || !vrucv_avail || !vrucv_used)
return -1;
if (vr_dpdk_set_vring_addr(vru_cl->vruc_idx, vring_idx, vrucv_desc,
vrucv_avail, vrucv_used)) {
vr_uvhost_log("Client %s: error setting vring %u addresses\n",
uvhm_client_name(vru_cl), vring_idx);
return -1;
}
/* Try to recover from the vRouter crash. */
vr_dpdk_virtio_recover_vring_base(vru_cl->vruc_idx, vring_idx);
uvhm_check_vring_ready(vru_cl, vring_idx);
return 0;
}
/*
* vr_uvhm_set_vring_base - handles a VHOST_USER_SET_VRING_BASE messsage
* from the vhost user client to set the based index of a vring.
*
* Returns 0 on success, -1 otherwise.
*/
static int
vr_uvhm_set_vring_base(vr_uvh_client_t *vru_cl)
{
VhostUserMsg *vum_msg;
unsigned int vring_idx;
vum_msg = &vru_cl->vruc_msg;
vring_idx = vum_msg->state.index;
vr_uvhost_log(" SET VRING BASE: vring %u base %u\n",
vring_idx, vum_msg->state.num);
if (vring_idx >= VHOST_CLIENT_MAX_VRINGS) {
vr_uvhost_log("Client %s: error setting vring %u base: invalid vring index\n",
uvhm_client_name(vru_cl), vring_idx);
return -1;
}
if (vr_dpdk_virtio_set_vring_base(vru_cl->vruc_idx, vring_idx,
vum_msg->state.num)) {
vr_uvhost_log("Client %s: error setting vring %u base %u\n",
uvhm_client_name(vru_cl), vring_idx, vum_msg->state.num);
return -1;
}
return 0;
}
/*
* vr_uvhm_get_vring_base - handles a VHOST_USER_GET_VRING_BASE messsage
* from the vhost user client to get the base index of a vring.
*
* Returns 0 on success, -1 otherwise.
*/
static int
vr_uvhm_get_vring_base(vr_uvh_client_t *vru_cl)
{
VhostUserMsg *vum_msg;
unsigned int vring_idx;
vum_msg = &vru_cl->vruc_msg;
vring_idx = vum_msg->state.index;
vr_uvhost_log(" GET VRING BASE: vring %u\n", vring_idx);
if (vring_idx >= VHOST_CLIENT_MAX_VRINGS) {
vr_uvhost_log("Client %s: error getting vring %u base: invalid vring index\n",
uvhm_client_name(vru_cl), vring_idx);
return -1;
}
if (vr_dpdk_virtio_get_vring_base(vru_cl->vruc_idx, vring_idx,
&vum_msg->state.num)) {
vr_uvhost_log("Client %s: error getting vring %u base index\n",
uvhm_client_name(vru_cl), vring_idx);
return -1;
}
vum_msg->size = sizeof(struct vhost_vring_state);
vr_uvhost_log(" GET VRING BASE: returns %u\n", vum_msg->state.num);
return 0;
}
/*
* vr_uvhm_set_vring_call - handles a VHOST_USER_SET_VRING_CALL message
* from the vhost user client to set the eventfd to be used to interrupt the
* guest, if required.
*
* Returns 0 on success, -1 otherwise.
*/
static int
vr_uvhm_set_vring_call(vr_uvh_client_t *vru_cl)
{
VhostUserMsg *vum_msg;
unsigned int vring_idx;
vum_msg = &vru_cl->vruc_msg;
vring_idx = vum_msg->state.index;
vr_uvhost_log(" SET VRING CALL: vring %u FD %d\n", vring_idx,
vru_cl->vruc_fds_sent[0]);
if (vring_idx >= VHOST_CLIENT_MAX_VRINGS) {
vr_uvhost_log("Client %s: error setting vring %u call: invalid vring index\n",
uvhm_client_name(vru_cl), vring_idx);
return -1;
}
if (vr_dpdk_set_ring_callfd(vru_cl->vruc_idx, vring_idx,
vru_cl->vruc_fds_sent[0])) {
vr_uvhost_log("Client %s: error setting vring %u call FD %d\n",
uvhm_client_name(vru_cl), vring_idx, vru_cl->vruc_fds_sent[0]);
return -1;
}
/* set FD to -1, so we do not close it in vr_uvh_cl_msg_handler() */
vru_cl->vruc_fds_sent[0] = -1;
uvhm_check_vring_ready(vru_cl, vring_idx);
return 0;
}
/*
* Handle the VHOST_USER_SET_VRING_ENABLE vhost-user protocol message.
*/
static int
vr_uvhm_set_vring_enable(vr_uvh_client_t *vru_cl)
{
VhostUserMsg *vum_msg;
unsigned int vring_idx;
unsigned int queue_num;
bool enable;
vum_msg = &vru_cl->vruc_msg;
vring_idx = vum_msg->state.index;
enable = (bool)vum_msg->state.num;
/* QEMU should NEVER send the disable command for queue 0 */
if ((vring_idx == 0 || vring_idx == 1) && !enable) {
RTE_LOG(ERR, UVHOST, "%s: Can not disable RX/TX queue 0\n", __func__);
return -1;
}
/*
* If the queue is higher than the number supported by vrouter, silently
* fail here (as there is no error message returned to qemu).
*/
if ((vring_idx / 2) >= vr_dpdk.nb_fwd_lcores) {
RTE_LOG(ERR, UVHOST, "%s: Can not %s %s queue %d (only %d queues)\n",
__func__, enable ? "enable" : "disable",
(vring_idx & 1) ? "RX" : "TX", vring_idx / 2,
vr_dpdk.nb_fwd_lcores);
return 0;
}
vr_uvhost_log("Client %s: setting vring %u ready state %d\n",
uvhm_client_name(vru_cl), vring_idx, enable);
uvhm_check_vring_ready(vru_cl, vring_idx);
queue_num = vring_idx / 2;
if (vring_idx & 1) {
/* RX queues */
vr_dpdk_virtio_rx_queue_enable_disable(vru_cl->vruc_idx,
vru_cl->vruc_vif_gen, queue_num,
enable);
} else {
/* TX queues */
vr_dpdk_virtio_tx_queue_enable_disable(vru_cl->vruc_idx,
vru_cl->vruc_vif_gen, queue_num,
enable);
}
return 0;
}
/*
* vr_uvh_cl_call_handler - calls message specific handler for messages
* from user space vhost client.
*
* Returns 0 on success, -1 otherwise.
*/
static int
vr_uvh_cl_call_handler(vr_uvh_client_t *vru_cl)
{
VhostUserMsg *msg = &vru_cl->vruc_msg;
if ((msg->request <= VHOST_USER_NONE) ||
(msg->request >= VHOST_USER_MAX)) {
return -1;
}
if (vr_uvhost_cl_msg_handlers[msg->request]) {
vr_uvhost_log("Client %s: handling message %d\n",
uvhm_client_name(vru_cl), msg->request);
return vr_uvhost_cl_msg_handlers[msg->request](vru_cl);
} else {
vr_uvhost_log("Client %s: no handler defined for message %d\n",
uvhm_client_name(vru_cl), msg->request);
}
return 0;
}
static int
vr_uvhm_get_queue_num(vr_uvh_client_t *vru_cl)
{
/* We support up to number of forwarding lcores queues as they are the only
* lcores that handle rx queues. However, this causes a failure when spawning
* the VM if the number of VCPUs in the VM is higher than the number of
* forwarding cores in vrouter. So, return VR_DPDK_VIRTIO_MAX_QUEUES here,
* but siliently fail the enable/disable of queues higher than the number
* of forwarding cores when the message is received from qemu later. The
* expectation is that the VM should not enable more queues that that.
*/
vru_cl->vruc_msg.u64 = VR_DPDK_VIRTIO_MAX_QUEUES;
vr_uvhost_log(" GET QUEUE NUM: returns 0x%"PRIx64"\n",
vru_cl->vruc_msg.u64);
vru_cl->vruc_msg.size = sizeof(vru_cl->vruc_msg.u64);
return 0;
}
/*
* vr_uvh_cl_send_reply - send a reply to the vhost user client if
* required.
*
* Returns 0 on success, -1 otherwise.
*/
static int
vr_uvh_cl_send_reply(int fd, vr_uvh_client_t *vru_cl)
{
int ret;
VhostUserMsg *msg = &vru_cl->vruc_msg;
switch(msg->request) {
case VHOST_USER_GET_FEATURES:
case VHOST_USER_GET_VRING_BASE:
case VHOST_USER_GET_PROTOCOL_FEATURES:
case VHOST_USER_GET_QUEUE_NUM:
/*
* Send reply for these messages only.
*/
msg->flags &= (~VHOST_USER_VERSION_MASK);
msg->flags |= VHOST_USER_VERSION;
msg->flags |= VHOST_USER_REPLY_MASK;
if (vru_cl->vruc_owner != pthread_self()) {
if (vru_cl->vruc_owner)
RTE_LOG(WARNING, UVHOST, "WARNING: thread %lx is trying to write"
" to uvhost client FD %d owned by thread %lx\n",
pthread_self(), fd, vru_cl->vruc_owner);
vru_cl->vruc_owner = pthread_self();
}
ret = send(fd, (void *) msg,
VHOST_USER_HSIZE + msg->size, MSG_DONTWAIT);
if ((ret < 0) || (ret != (VHOST_USER_HSIZE + msg->size))) {
/*
* Socket to qemu should never be full as it sleeps waiting
* for a reply to the previous request.
*/
vr_uvhost_log("Client %s: error sending vhost user reply\n",
uvhm_client_name(vru_cl));
return -1;
}
break;
default:
/*
* No reply needed.
*/
break;
}
return 0;
}
/*
* vr_uvh_cl_msg_handler - handler for messages from user space vhost
* clients. Calls the appropriate handler based on the message type.
*
* Returns 0 on success, -1 on error.
*
* TODO: upon error, this function currently makes the process exit.
* Instead, it should close the socket and continue serving other clients.
*/
static int
vr_uvh_cl_msg_handler(int fd, void *arg)
{
vr_uvh_client_t *vru_cl = (vr_uvh_client_t *) arg;
struct msghdr mhdr;
struct iovec iov;
int i, err, ret = 0, read_len = 0;
struct cmsghdr *cmsg;
memset(&mhdr, 0, sizeof(mhdr));
if (vru_cl->vruc_msg_bytes_read == 0) {
mhdr.msg_control = &vru_cl->vruc_cmsg;
mhdr.msg_controllen = sizeof(vru_cl->vruc_cmsg);
iov.iov_base = (void *) &vru_cl->vruc_msg;
iov.iov_len = VHOST_USER_HSIZE;
mhdr.msg_iov = &iov;
mhdr.msg_iovlen = 1;
ret = recvmsg(fd, &mhdr, MSG_DONTWAIT);
if (ret < 0) {
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
ret = 0;
goto cleanup;
}
vr_uvhost_log("Client %s: error receiving message: %s (%d)\n",
uvhm_client_name(vru_cl), strerror(errno), errno);
ret = -1;
goto cleanup;
} else if (ret > 0) {
if (mhdr.msg_flags & MSG_CTRUNC) {
vr_uvhost_log("Client %s: error receiving message: truncated\n",
uvhm_client_name(vru_cl));
ret = -1;
goto cleanup;
}
cmsg = CMSG_FIRSTHDR(&mhdr);
if (cmsg && (cmsg->cmsg_len > 0) &&
(cmsg->cmsg_level == SOL_SOCKET) &&
(cmsg->cmsg_type == SCM_RIGHTS)) {
vru_cl->vruc_num_fds_sent = (cmsg->cmsg_len - CMSG_LEN(0))/
sizeof(int);
if (vru_cl->vruc_num_fds_sent > VHOST_MEMORY_MAX_NREGIONS) {
vr_uvhost_log("Client %s: error handling FDs: too many FDs (%d > %d)\n",
uvhm_client_name(vru_cl),
vru_cl->vruc_num_fds_sent,
VHOST_MEMORY_MAX_NREGIONS);
vru_cl->vruc_num_fds_sent = VHOST_MEMORY_MAX_NREGIONS;
}
memcpy(vru_cl->vruc_fds_sent, CMSG_DATA(cmsg),
vru_cl->vruc_num_fds_sent*sizeof(int));
}
vru_cl->vruc_msg_bytes_read = ret;
if (ret < VHOST_USER_HSIZE) {
ret = 0;
goto cleanup;
}
read_len = vru_cl->vruc_msg.size;
} else {
/*
* recvmsg returned 0, so return error.
*/
vr_uvhost_log("Client %s: shutdown at message receiving\n",
uvhm_client_name(vru_cl));
ret = -1;
goto cleanup;
}
} else if (vru_cl->vruc_msg_bytes_read < VHOST_USER_HSIZE) {
read_len = VHOST_USER_HSIZE - vru_cl->vruc_msg_bytes_read;
} else {
read_len = vru_cl->vruc_msg.size -
(vru_cl->vruc_msg_bytes_read - VHOST_USER_HSIZE);
}
if (read_len) {
if (vru_cl->vruc_owner != pthread_self()) {
if (vru_cl->vruc_owner)
RTE_LOG(WARNING, UVHOST, "WARNING: thread %lx is trying to read"
" uvhost client FD %d owned by thread %lx\n",
pthread_self(), fd, vru_cl->vruc_owner);
vru_cl->vruc_owner = pthread_self();
}
ret = read(fd, (((char *)&vru_cl->vruc_msg) + vru_cl->vruc_msg_bytes_read),
read_len);
#ifdef VR_DPDK_RX_PKT_DUMP
if (ret > 0) {
RTE_LOG(DEBUG, UVHOST, "%s[%lx]: FD %d read %d bytes\n", __func__,
pthread_self(), fd, ret);
rte_hexdump(stdout, "uvhost full message dump:",
(((char *)&vru_cl->vruc_msg)),
ret + vru_cl->vruc_msg_bytes_read);
} else if (ret < 0) {
RTE_LOG(DEBUG, UVHOST, "%s[%lx]: FD %d read returned error %d: %s (%d)\n", __func__,
pthread_self(), fd, ret, rte_strerror(errno), errno);
}
#endif
if (ret < 0) {
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
ret = 0;
goto cleanup;
}
vr_uvhost_log(
"Client %s: error reading message: %s (%d)\n",
uvhm_client_name(vru_cl), strerror(errno), errno);
ret = -1;
goto cleanup;
} else if (ret == 0) {
vr_uvhost_log("Client %s: shutdown at message reading\n",
uvhm_client_name(vru_cl));
ret = -1;
goto cleanup;
}
vru_cl->vruc_msg_bytes_read += ret;
if (vru_cl->vruc_msg_bytes_read < VHOST_USER_HSIZE) {
ret = 0;
goto cleanup;
}
if (vru_cl->vruc_msg_bytes_read <
(vru_cl->vruc_msg.size + VHOST_USER_HSIZE)) {
ret = 0;
goto cleanup;
}
}
ret = vr_uvh_cl_call_handler(vru_cl);
if (ret < 0) {
vr_uvhost_log("Client %s: error handling message %d\n",
uvhm_client_name(vru_cl), vru_cl->vruc_msg.request);
ret = -1;
goto cleanup;
}
ret = vr_uvh_cl_send_reply(fd, vru_cl);
if (ret < 0) {
vr_uvhost_log("Client %s: error sending reply for message %d\n",
uvhm_client_name(vru_cl), vru_cl->vruc_msg.request);
ret = -1;
goto cleanup;
}
cleanup:
err = errno;
/* close all the FDs received */
for (i = 0; i < vru_cl->vruc_num_fds_sent; i++) {
if (vru_cl->vruc_fds_sent[i] > 0)
close(vru_cl->vruc_fds_sent[i]);
}
if (ret == -1) {
/* We set VQ_NOT_READY state and reset the queues in uvhm_client_munmap() */
uvhm_client_munmap(vru_cl);
}
/* clear state for next message from this client. */
vru_cl->vruc_msg_bytes_read = 0;
memset(&vru_cl->vruc_msg, 0, sizeof(vru_cl->vruc_msg));
memset(vru_cl->vruc_cmsg, 0, sizeof(vru_cl->vruc_cmsg));
memset(vru_cl->vruc_fds_sent, 0, sizeof(vru_cl->vruc_fds_sent));
vru_cl->vruc_num_fds_sent = 0;
errno = err;
return ret;
}
/*
* vr_uvh_cl_listen_handler - handler for connections from user space vhost
* clients. Accepts the connections and sets up a message handler for the
* client in the server.
*
* Returns 0 on success, -1 otherwise.
*/
static int
vr_uvh_cl_listen_handler(int fd, void *arg)
{
int s = 0, err;
struct sockaddr_un sun;
socklen_t len = sizeof(sun);
vr_uvh_client_t *vru_cl = (vr_uvh_client_t *) arg;
vr_uvhost_log("Handling client connection FD %d\n", fd);
s = accept(fd, (struct sockaddr *) &sun, &len);
if (s < 0) {
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
return 0;
}
vr_uvhost_log(" error accepting client connection FD %d\n", fd);
return -1;
}
vr_uvhost_log(" FD %d accepted new client connection FD %d\n", fd, s);
/* We still need to listen for the original socket to support VM
* shut off/restart, since we create the socket at vif --add
* and we get vif --add at the VM spawning, not VM (re)starting
*/
/* Do not set new client FD, since we still need to close parent FD
* on vif delete.
* We will get the client FD in our handler as an argument.
*/
if (vr_uvhost_add_fd(s, UVH_FD_READ, vru_cl, vr_uvh_cl_msg_handler)) {
vr_uvhost_log(" error adding client %s FD %d read handler\n",
sun.sun_path, fd);
goto error;
}
return 0;
error:
err = errno;
if (s) {
close(s);
}
if (vru_cl) {
vr_uvhost_del_client(vru_cl);
}
errno = err;
return -1;
}
/*
* vr_uvh_nl_vif_del_handler - handle a message from the netlink thread
* to delete a vif.
*
* Returns 0 on success, -1 otherwise.
*/
int
vr_uvh_nl_vif_del_handler(vrnu_vif_del_t *msg)
{
unsigned int cidx = msg->vrnu_vif_idx;
vr_uvh_client_t *vru_cl;
vr_uvhost_log("Deleting vif %d virtual device\n", cidx);
if (cidx >= VR_UVH_MAX_CLIENTS) {
vr_uvhost_log(" error deleting vif %u: invalid vif index\n", cidx);
return -1;
}
vr_dpdk_virtio_set_vif_client(cidx, NULL);
vru_cl = vr_uvhost_get_client(cidx);
if (vru_cl == NULL) {
vr_uvhost_log(" error deleting vif %d: no client found\n",
cidx);
return -1;
}
/* Unmmap guest memory. */
uvhm_client_munmap(vru_cl);
vr_uvhost_del_client(vru_cl);
return 0;
}
/*
* vr_uvh_nl_vif_add_handler - handle a vif add message from the netlink
* thread. In response, the vhost server thread starts listening on the
* UNIX domain socket corresponding to this vif.
*
* Returns 0 on success, -1 otherwise.
*/
static int
vr_uvh_nl_vif_add_handler(vrnu_vif_add_t *msg)
{
int s = 0, ret = -1, err;
struct sockaddr_un sun;
int flags;
vr_uvh_client_t *vru_cl = NULL;
mode_t umask_mode;
if (msg == NULL) {
vr_uvhost_log(" error adding vif %u: message is NULL\n",
msg->vrnu_vif_idx);
return -1;