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
|
/*-
* Copyright (c) 2009-2016 Microsoft Corp.
* Copyright (c) 2012 NetApp Inc.
* Copyright (c) 2012 Citrix Inc.
* Copyright (c) 2016 Mike Belopuhov <mike@esdenera.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* The OpenBSD port was done under funding by Esdenera Networks GmbH.
*/
#include <sys/param.h>
/* Hyperv requires locked atomic operations */
#ifndef MULTIPROCESSOR
#define _HYPERVMPATOMICS
#define MULTIPROCESSOR
#endif
#include <sys/atomic.h>
#ifdef _HYPERVMPATOMICS
#undef MULTIPROCESSOR
#undef _HYPERVMPATOMICS
#endif
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/signal.h>
#include <sys/signalvar.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/pool.h>
#include <sys/timetc.h>
#include <sys/task.h>
#include <sys/syslog.h>
#include <machine/bus.h>
#include <machine/cpu.h>
#include <machine/cpufunc.h>
#include <machine/i82489var.h>
#include <dev/rndvar.h>
#include <dev/pv/pvvar.h>
#include <dev/pv/pvreg.h>
#include <dev/pv/hypervreg.h>
#include <dev/pv/hypervvar.h>
#include <dev/pv/hypervicreg.h>
struct hv_ic_dev;
#define NKVPPOOLS 4
#define MAXPOOLENTS 1023
struct kvp_entry {
int kpe_index;
uint32_t kpe_valtype;
uint8_t kpe_key[HV_KVP_MAX_KEY_SIZE / 2];
uint8_t kpe_val[HV_KVP_MAX_VAL_SIZE / 2];
TAILQ_ENTRY(kvp_entry) kpe_entry;
};
TAILQ_HEAD(kvp_list, kvp_entry);
struct kvp_pool {
struct kvp_list kvp_entries;
u_int kvp_index;
};
struct pool kvp_entry_pool;
struct hv_kvp {
struct kvp_pool kvp_pool[NKVPPOOLS];
};
int hv_heartbeat_attach(struct hv_ic_dev *);
void hv_heartbeat(void *);
int hv_kvp_attach(struct hv_ic_dev *);
void hv_kvp(void *);
int hv_kvop(void *, int, char *, char *, size_t);
int hv_shutdown_attach(struct hv_ic_dev *);
void hv_shutdown(void *);
int hv_timesync_attach(struct hv_ic_dev *);
void hv_timesync(void *);
static struct hv_ic_dev {
const char *dv_name;
const struct hv_guid *dv_type;
int (*dv_attach)(struct hv_ic_dev *);
void (*dv_handler)(void *);
struct hv_channel *dv_ch;
uint8_t *dv_buf;
void *dv_priv;
} hv_ic_devs[] = {
{
"heartbeat",
&hv_guid_heartbeat,
hv_heartbeat_attach,
hv_heartbeat
},
{
"kvp",
&hv_guid_kvp,
hv_kvp_attach,
hv_kvp
},
{
"shutdown",
&hv_guid_shutdown,
hv_shutdown_attach,
hv_shutdown
},
{
"timesync",
&hv_guid_timesync,
hv_timesync_attach,
hv_timesync
}
};
static const struct {
enum hv_kvp_pool poolidx;
const char *poolname;
size_t poolnamelen;
} kvp_pools[] = {
{ HV_KVP_POOL_EXTERNAL, "External", sizeof("External") },
{ HV_KVP_POOL_GUEST, "Guest", sizeof("Guest") },
{ HV_KVP_POOL_AUTO, "Auto", sizeof("Auto") },
{ HV_KVP_POOL_AUTO_EXTERNAL, "Guest/Parameters",
sizeof("Guest/Parameters") }
};
/* As specified in SYSTEM_INFO.wProcessorArchitecture */
#ifdef __amd64__
static const char march[] = "9";
#else
static const char march[] = "0";
#endif
static const struct {
int keyidx;
const char *keyname;
const char *value;
} kvp_pool_auto[] = {
{ 0, "FullyQualifiedDomainName", hostname },
{ 1, "IntegrationServicesVersion", "6.6.6" },
{ 2, "NetworkAddressIPv4", "127.0.0.1" },
{ 3, "NetworkAddressIPv6", "::1" },
{ 4, "OSBuildNumber", osversion },
{ 5, "OSName", ostype },
{ 6, "OSMajorVersion", "6" }, /* free commit for mike */
{ 7, "OSMinorVersion", &osrelease[2] },
{ 8, "OSVersion", osrelease },
{ 9, "ProcessorArchitecture", march }
};
void
hv_attach_icdevs(struct hv_softc *sc)
{
struct hv_ic_dev *dv;
struct hv_channel *ch;
int i, header = 0;
for (i = 0; i < nitems(hv_ic_devs); i++) {
dv = &hv_ic_devs[i];
TAILQ_FOREACH(ch, &sc->sc_channels, ch_entry) {
if (ch->ch_state != HV_CHANSTATE_OFFERED)
continue;
if (ch->ch_flags & CHF_MONITOR)
continue;
if (memcmp(dv->dv_type, &ch->ch_type,
sizeof(ch->ch_type)) == 0)
break;
}
if (ch == NULL)
continue;
dv->dv_ch = ch;
/*
* These services are not performance critical and
* do not need batched reading. Furthermore, some
* services such as KVP can only handle one message
* from the host at a time.
*/
dv->dv_ch->ch_flags &= ~CHF_BATCHED;
if (dv->dv_attach && dv->dv_attach(dv) != 0)
continue;
if (hv_channel_open(ch, VMBUS_IC_BUFRINGSIZE, NULL, 0,
dv->dv_handler, dv)) {
printf("%s: failed to open channel for %s\n",
sc->sc_dev.dv_xname, dv->dv_name);
continue;
}
evcount_attach(&ch->ch_evcnt, dv->dv_name, &sc->sc_idtvec);
if (!header) {
printf("%s: %s", sc->sc_dev.dv_xname, dv->dv_name);
header = 1;
} else
printf(", %s", dv->dv_name);
}
if (header)
printf("\n");
}
static inline void
hv_ic_negotiate(struct vmbus_icmsg_hdr *hdr, uint32_t *rlen, uint32_t fwver,
uint32_t msgver)
{
struct vmbus_icmsg_negotiate *msg;
uint16_t propmin, propmaj, chosenmaj, chosenmin;
int i;
msg = (struct vmbus_icmsg_negotiate *)hdr;
chosenmaj = chosenmin = 0;
for (i = 0; i < msg->ic_fwver_cnt; i++) {
propmaj = VMBUS_ICVER_MAJOR(msg->ic_ver[i]);
propmin = VMBUS_ICVER_MINOR(msg->ic_ver[i]);
if (propmaj > chosenmaj &&
propmaj <= VMBUS_ICVER_MAJOR(fwver) &&
propmin >= chosenmin &&
propmin <= VMBUS_ICVER_MINOR(fwver)) {
chosenmaj = propmaj;
chosenmin = propmin;
}
}
fwver = VMBUS_IC_VERSION(chosenmaj, chosenmin);
chosenmaj = chosenmin = 0;
for (; i < msg->ic_fwver_cnt + msg->ic_msgver_cnt; i++) {
propmaj = VMBUS_ICVER_MAJOR(msg->ic_ver[i]);
propmin = VMBUS_ICVER_MINOR(msg->ic_ver[i]);
if (propmaj > chosenmaj &&
propmaj <= VMBUS_ICVER_MAJOR(msgver) &&
propmin >= chosenmin &&
propmin <= VMBUS_ICVER_MINOR(msgver)) {
chosenmaj = propmaj;
chosenmin = propmin;
}
}
msgver = VMBUS_IC_VERSION(chosenmaj, chosenmin);
msg->ic_fwver_cnt = 1;
msg->ic_ver[0] = fwver;
msg->ic_msgver_cnt = 1;
msg->ic_ver[1] = msgver;
hdr->ic_dsize = sizeof(*msg) + 2 * sizeof(uint32_t) -
sizeof(struct vmbus_icmsg_hdr);
if (*rlen < sizeof(*msg) + 2 * sizeof(uint32_t))
*rlen = sizeof(*msg) + 2 * sizeof(uint32_t);
}
int
hv_heartbeat_attach(struct hv_ic_dev *dv)
{
struct hv_channel *ch = dv->dv_ch;
struct hv_softc *sc = ch->ch_sc;
dv->dv_buf = malloc(PAGE_SIZE, M_DEVBUF, M_ZERO |
(cold ? M_NOWAIT : M_WAITOK));
if (dv->dv_buf == NULL) {
printf("%s: failed to allocate receive buffer\n",
sc->sc_dev.dv_xname);
return (-1);
}
return (0);
}
void
hv_heartbeat(void *arg)
{
struct hv_ic_dev *dv = arg;
struct hv_channel *ch = dv->dv_ch;
struct hv_softc *sc = ch->ch_sc;
struct vmbus_icmsg_hdr *hdr;
struct vmbus_icmsg_heartbeat *msg;
uint64_t rid;
uint32_t rlen;
int rv;
rv = hv_channel_recv(ch, dv->dv_buf, PAGE_SIZE, &rlen, &rid, 0);
if (rv || rlen == 0) {
if (rv != EAGAIN)
DPRINTF("%s: heartbeat rv=%d rlen=%u\n",
sc->sc_dev.dv_xname, rv, rlen);
return;
}
if (rlen < sizeof(struct vmbus_icmsg_hdr)) {
DPRINTF("%s: heartbeat short read rlen=%u\n",
sc->sc_dev.dv_xname, rlen);
return;
}
hdr = (struct vmbus_icmsg_hdr *)dv->dv_buf;
switch (hdr->ic_type) {
case VMBUS_ICMSG_TYPE_NEGOTIATE:
hv_ic_negotiate(hdr, &rlen, VMBUS_IC_VERSION(3, 0),
VMBUS_IC_VERSION(3, 0));
break;
case VMBUS_ICMSG_TYPE_HEARTBEAT:
msg = (struct vmbus_icmsg_heartbeat *)hdr;
msg->ic_seq += 1;
break;
default:
printf("%s: unhandled heartbeat message type %u\n",
sc->sc_dev.dv_xname, hdr->ic_type);
return;
}
hdr->ic_flags = VMBUS_ICMSG_FLAG_TRANSACTION | VMBUS_ICMSG_FLAG_RESPONSE;
hv_channel_send(ch, dv->dv_buf, rlen, rid, VMBUS_CHANPKT_TYPE_INBAND, 0);
}
static void
hv_shutdown_task(void *arg)
{
extern int allowpowerdown;
if (allowpowerdown == 0)
return;
suspend_randomness();
log(LOG_KERN | LOG_NOTICE, "Shutting down in response to "
"request from Hyper-V host\n");
prsignal(initprocess, SIGUSR2);
}
int
hv_shutdown_attach(struct hv_ic_dev *dv)
{
struct hv_channel *ch = dv->dv_ch;
struct hv_softc *sc = ch->ch_sc;
dv->dv_buf = malloc(PAGE_SIZE, M_DEVBUF, M_ZERO |
(cold ? M_NOWAIT : M_WAITOK));
if (dv->dv_buf == NULL) {
printf("%s: failed to allocate receive buffer\n",
sc->sc_dev.dv_xname);
return (-1);
}
task_set(&sc->sc_sdtask, hv_shutdown_task, sc);
return (0);
}
void
hv_shutdown(void *arg)
{
struct hv_ic_dev *dv = arg;
struct hv_channel *ch = dv->dv_ch;
struct hv_softc *sc = ch->ch_sc;
struct vmbus_icmsg_hdr *hdr;
struct vmbus_icmsg_shutdown *msg;
uint64_t rid;
uint32_t rlen;
int rv, shutdown = 0;
rv = hv_channel_recv(ch, dv->dv_buf, PAGE_SIZE, &rlen, &rid, 0);
if (rv || rlen == 0) {
if (rv != EAGAIN)
DPRINTF("%s: shutdown rv=%d rlen=%u\n",
sc->sc_dev.dv_xname, rv, rlen);
return;
}
if (rlen < sizeof(struct vmbus_icmsg_hdr)) {
DPRINTF("%s: shutdown short read rlen=%u\n",
sc->sc_dev.dv_xname, rlen);
return;
}
hdr = (struct vmbus_icmsg_hdr *)dv->dv_buf;
switch (hdr->ic_type) {
case VMBUS_ICMSG_TYPE_NEGOTIATE:
hv_ic_negotiate(hdr, &rlen, VMBUS_IC_VERSION(3, 0),
VMBUS_IC_VERSION(3, 0));
break;
case VMBUS_ICMSG_TYPE_SHUTDOWN:
msg = (struct vmbus_icmsg_shutdown *)hdr;
if (msg->ic_haltflags == 0 || msg->ic_haltflags == 1) {
shutdown = 1;
hdr->ic_status = VMBUS_ICMSG_STATUS_OK;
} else
hdr->ic_status = VMBUS_ICMSG_STATUS_FAIL;
break;
default:
printf("%s: unhandled shutdown message type %u\n",
sc->sc_dev.dv_xname, hdr->ic_type);
return;
}
hdr->ic_flags = VMBUS_ICMSG_FLAG_TRANSACTION | VMBUS_ICMSG_FLAG_RESPONSE;
hv_channel_send(ch, dv->dv_buf, rlen, rid, VMBUS_CHANPKT_TYPE_INBAND, 0);
if (shutdown)
task_add(systq, &sc->sc_sdtask);
}
int
hv_timesync_attach(struct hv_ic_dev *dv)
{
struct hv_channel *ch = dv->dv_ch;
struct hv_softc *sc = ch->ch_sc;
dv->dv_buf = malloc(PAGE_SIZE, M_DEVBUF, M_ZERO |
(cold ? M_NOWAIT : M_WAITOK));
if (dv->dv_buf == NULL) {
printf("%s: failed to allocate receive buffer\n",
sc->sc_dev.dv_xname);
return (-1);
}
strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
sizeof(sc->sc_sensordev.xname));
sc->sc_sensor.type = SENSOR_TIMEDELTA;
sc->sc_sensor.status = SENSOR_S_UNKNOWN;
sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
sensordev_install(&sc->sc_sensordev);
return (0);
}
void
hv_timesync(void *arg)
{
struct hv_ic_dev *dv = arg;
struct hv_channel *ch = dv->dv_ch;
struct hv_softc *sc = ch->ch_sc;
struct vmbus_icmsg_hdr *hdr;
struct vmbus_icmsg_timesync *msg;
struct timespec guest, host, diff;
uint64_t tns;
uint64_t rid;
uint32_t rlen;
int rv;
rv = hv_channel_recv(ch, dv->dv_buf, PAGE_SIZE, &rlen, &rid, 0);
if (rv || rlen == 0) {
if (rv != EAGAIN)
DPRINTF("%s: timesync rv=%d rlen=%u\n",
sc->sc_dev.dv_xname, rv, rlen);
return;
}
if (rlen < sizeof(struct vmbus_icmsg_hdr)) {
DPRINTF("%s: timesync short read rlen=%u\n",
sc->sc_dev.dv_xname, rlen);
return;
}
hdr = (struct vmbus_icmsg_hdr *)dv->dv_buf;
switch (hdr->ic_type) {
case VMBUS_ICMSG_TYPE_NEGOTIATE:
hv_ic_negotiate(hdr, &rlen, VMBUS_IC_VERSION(3, 0),
VMBUS_IC_VERSION(3, 0));
break;
case VMBUS_ICMSG_TYPE_TIMESYNC:
msg = (struct vmbus_icmsg_timesync *)hdr;
if (msg->ic_tsflags == VMBUS_ICMSG_TS_FLAG_SYNC ||
msg->ic_tsflags == VMBUS_ICMSG_TS_FLAG_SAMPLE) {
microtime(&sc->sc_sensor.tv);
nanotime(&guest);
tns = (msg->ic_hvtime - 116444736000000000LL) * 100;
host.tv_sec = tns / 1000000000LL;
host.tv_nsec = tns % 1000000000LL;
timespecsub(&guest, &host, &diff);
sc->sc_sensor.value = (int64_t)diff.tv_sec *
1000000000LL + diff.tv_nsec;
sc->sc_sensor.status = SENSOR_S_OK;
}
break;
default:
printf("%s: unhandled timesync message type %u\n",
sc->sc_dev.dv_xname, hdr->ic_type);
return;
}
hdr->ic_flags = VMBUS_ICMSG_FLAG_TRANSACTION | VMBUS_ICMSG_FLAG_RESPONSE;
hv_channel_send(ch, dv->dv_buf, rlen, rid, VMBUS_CHANPKT_TYPE_INBAND, 0);
}
static inline int
copyout_utf16le(void *dst, const void *src, size_t dlen, size_t slen)
{
const uint8_t *sp = src;
uint8_t *dp = dst;
int i, j;
KASSERT(dlen >= slen * 2);
for (i = j = 0; i < slen; i++, j += 2) {
dp[j] = sp[i];
dp[j + 1] = '\0';
}
return (j);
}
static inline int
copyin_utf16le(void *dst, const void *src, size_t dlen, size_t slen)
{
const uint8_t *sp = src;
uint8_t *dp = dst;
int i, j;
KASSERT(dlen >= slen / 2);
for (i = j = 0; i < slen; i += 2, j++)
dp[j] = sp[i];
return (j);
}
static void
kvp_pool_init(struct kvp_pool *kvpl)
{
TAILQ_INIT(&kvpl->kvp_entries);
kvpl->kvp_index = 0xffffffffU;
}
static int
kvp_pool_insert(struct kvp_pool *kvpl, const char *key, const char *val,
uint32_t vallen, uint32_t valtype)
{
struct kvp_entry *kpe, *nkpe;
int keylen = strlen(key);
if (keylen > HV_KVP_MAX_KEY_SIZE / 2)
return (ERANGE);
TAILQ_FOREACH(kpe, &kvpl->kvp_entries, kpe_entry) {
if (strcmp(kpe->kpe_key, key) == 0)
return (EEXIST);
}
nkpe = pool_get(&kvp_entry_pool, PR_ZERO | PR_NOWAIT);
if (nkpe == NULL)
return (ENOMEM);
strlcpy(nkpe->kpe_key, key, HV_KVP_MAX_KEY_SIZE / 2);
if ((nkpe->kpe_valtype = valtype) == HV_KVP_REG_SZ)
strlcpy(nkpe->kpe_val, val, HV_KVP_MAX_KEY_SIZE / 2);
else
memcpy(nkpe->kpe_val, val, vallen);
nkpe->kpe_index = atomic_inc_int_nv(&kvpl->kvp_index) & MAXPOOLENTS;
TAILQ_INSERT_TAIL(&kvpl->kvp_entries, nkpe, kpe_entry);
return (0);
}
static int
kvp_pool_update(struct kvp_pool *kvpl, const char *key, const char *val,
uint32_t vallen, uint32_t valtype)
{
struct kvp_entry *kpe;
int keylen = strlen(key);
if (keylen > HV_KVP_MAX_KEY_SIZE / 2)
return (ERANGE);
TAILQ_FOREACH(kpe, &kvpl->kvp_entries, kpe_entry) {
if (strcmp(kpe->kpe_key, key) == 0)
break;
}
if (kpe == NULL)
return (ENOENT);
if ((kpe->kpe_valtype = valtype) == HV_KVP_REG_SZ)
strlcpy(kpe->kpe_val, val, HV_KVP_MAX_KEY_SIZE / 2);
else
memcpy(kpe->kpe_val, val, vallen);
return (0);
}
static int
kvp_pool_import(struct kvp_pool *kvpl, const char *key, uint32_t keylen,
const char *val, uint32_t vallen, uint32_t valtype)
{
struct kvp_entry *kpe, *nkpe;
if (keylen > HV_KVP_MAX_KEY_SIZE ||
vallen > HV_KVP_MAX_VAL_SIZE)
return (ERANGE);
nkpe = pool_get(&kvp_entry_pool, PR_ZERO | PR_NOWAIT);
if (nkpe == NULL)
return (ENOMEM);
copyin_utf16le(nkpe->kpe_key, key, HV_KVP_MAX_KEY_SIZE / 2, keylen);
TAILQ_FOREACH(kpe, &kvpl->kvp_entries, kpe_entry) {
if (strcmp(kpe->kpe_key, nkpe->kpe_key) == 0) {
pool_put(&kvp_entry_pool, nkpe);
return (EEXIST);
}
}
copyin_utf16le(nkpe->kpe_val, val, HV_KVP_MAX_VAL_SIZE / 2, vallen);
nkpe->kpe_valtype = valtype;
nkpe->kpe_index = atomic_inc_int_nv(&kvpl->kvp_index) & MAXPOOLENTS;
TAILQ_INSERT_TAIL(&kvpl->kvp_entries, nkpe, kpe_entry);
return (0);
}
static int
kvp_pool_export(struct kvp_pool *kvpl, uint32_t index, char *key,
uint32_t *keylen, char *val, uint32_t *vallen, uint32_t *valtype)
{
struct kvp_entry *kpe;
TAILQ_FOREACH(kpe, &kvpl->kvp_entries, kpe_entry) {
if (kpe->kpe_index == index)
break;
}
if (kpe == NULL)
return (ENOENT);
*keylen = copyout_utf16le(key, kpe->kpe_key, HV_KVP_MAX_KEY_SIZE,
strlen(kpe->kpe_key) + 1);
*vallen = copyout_utf16le(val, kpe->kpe_val, HV_KVP_MAX_VAL_SIZE,
strlen(kpe->kpe_val) + 1);
*valtype = kpe->kpe_valtype;
return (0);
}
static int
kvp_pool_extract(struct kvp_pool *kvpl, const char *key, char *val,
uint32_t vallen)
{
struct kvp_entry *kpe;
if (vallen < HV_KVP_MAX_VAL_SIZE / 2)
return (ERANGE);
TAILQ_FOREACH(kpe, &kvpl->kvp_entries, kpe_entry) {
if (strcmp(kpe->kpe_key, key) == 0)
break;
}
if (kpe == NULL)
return (ENOENT);
switch (kpe->kpe_valtype) {
case HV_KVP_REG_SZ:
strlcpy(val, kpe->kpe_val, HV_KVP_MAX_VAL_SIZE / 2);
break;
case HV_KVP_REG_U32:
snprintf(val, HV_KVP_MAX_VAL_SIZE / 2, "%u",
*(uint32_t *)kpe->kpe_val);
break;
case HV_KVP_REG_U64:
snprintf(val, HV_KVP_MAX_VAL_SIZE / 2, "%llu",
*(uint64_t *)kpe->kpe_val);
break;
}
return (0);
}
static int
kvp_pool_keys(struct kvp_pool *kvpl, int next, char *key, size_t *keylen)
{
struct kvp_entry *kpe;
int iter = 0;
TAILQ_FOREACH(kpe, &kvpl->kvp_entries, kpe_entry) {
if (iter++ < next)
continue;
*keylen = strlen(kpe->kpe_key) + 1;
strlcpy(key, kpe->kpe_key, *keylen);
return (0);
}
return (-1);
}
int
hv_kvp_attach(struct hv_ic_dev *dv)
{
struct hv_channel *ch = dv->dv_ch;
struct hv_softc *sc = ch->ch_sc;
struct hv_kvp *kvp;
int i;
dv->dv_buf = malloc(2 * PAGE_SIZE, M_DEVBUF, M_ZERO |
(cold ? M_NOWAIT : M_WAITOK));
if (dv->dv_buf == NULL) {
printf("%s: failed to allocate receive buffer\n",
sc->sc_dev.dv_xname);
return (-1);
}
dv->dv_priv = malloc(sizeof(struct hv_kvp), M_DEVBUF, M_ZERO |
(cold ? M_NOWAIT : M_WAITOK));
if (dv->dv_priv == NULL) {
free(dv->dv_buf, M_DEVBUF, 2 * PAGE_SIZE);
printf("%s: failed to allocate KVP private data\n",
sc->sc_dev.dv_xname);
return (-1);
}
kvp = dv->dv_priv;
pool_init(&kvp_entry_pool, sizeof(struct kvp_entry), 0, IPL_NET, 0,
"hvkvpl", NULL);
for (i = 0; i < NKVPPOOLS; i++)
kvp_pool_init(&kvp->kvp_pool[i]);
/* Initialize GuestIntrinsicExchangeItems (pool 3) */
for (i = 0; i < nitems(kvp_pool_auto); i++) {
if (kvp_pool_insert(&kvp->kvp_pool[HV_KVP_POOL_AUTO],
kvp_pool_auto[i].keyname, kvp_pool_auto[i].value,
strlen(kvp_pool_auto[i].value), HV_KVP_REG_SZ))
printf("%s: failed to insert key '%s'\n",
sc->sc_dev.dv_xname, kvp_pool_auto[i].keyname);
}
sc->sc_pvbus->hv_kvop = hv_kvop;
sc->sc_pvbus->hv_arg = dv;
return (0);
}
static void
hv_kvp_process(struct hv_kvp *kvp, struct vmbus_icmsg_kvp *msg)
{
union hv_kvp_hdr *kvh = &msg->ic_kvh;
union hv_kvp_msg *kvm = &msg->ic_kvm;
switch (kvh->kvh_op) {
case HV_KVP_OP_SET:
if (kvh->kvh_pool == HV_KVP_POOL_AUTO_EXTERNAL &&
kvp_pool_import(&kvp->kvp_pool[HV_KVP_POOL_AUTO_EXTERNAL],
kvm->kvm_val.kvm_key, kvm->kvm_val.kvm_keylen,
kvm->kvm_val.kvm_val, kvm->kvm_val.kvm_vallen,
kvm->kvm_val.kvm_valtype)) {
DPRINTF("%s: failed to import the value\n", __func__);
kvh->kvh_err = HV_KVP_S_CONT;
} else if (kvh->kvh_pool == HV_KVP_POOL_EXTERNAL &&
kvp_pool_import(&kvp->kvp_pool[HV_KVP_POOL_EXTERNAL],
kvm->kvm_val.kvm_key, kvm->kvm_val.kvm_keylen,
kvm->kvm_val.kvm_val, kvm->kvm_val.kvm_vallen,
kvm->kvm_val.kvm_valtype)) {
DPRINTF("%s: failed to import the value\n", __func__);
kvh->kvh_err = HV_KVP_S_CONT;
} else if (kvh->kvh_pool != HV_KVP_POOL_AUTO_EXTERNAL &&
kvh->kvh_pool != HV_KVP_POOL_EXTERNAL) {
kvh->kvh_err = HV_KVP_S_CONT;
} else
kvh->kvh_err = HV_KVP_S_OK;
break;
case HV_KVP_OP_ENUMERATE:
if (kvh->kvh_pool == HV_KVP_POOL_AUTO &&
kvp_pool_export(&kvp->kvp_pool[HV_KVP_POOL_AUTO],
kvm->kvm_enum.kvm_index, kvm->kvm_enum.kvm_key,
&kvm->kvm_enum.kvm_keylen, kvm->kvm_enum.kvm_val,
&kvm->kvm_enum.kvm_vallen, &kvm->kvm_enum.kvm_valtype))
kvh->kvh_err = HV_KVP_S_CONT;
else if (kvh->kvh_pool == HV_KVP_POOL_GUEST &&
kvp_pool_export(&kvp->kvp_pool[HV_KVP_POOL_GUEST],
kvm->kvm_enum.kvm_index, kvm->kvm_enum.kvm_key,
&kvm->kvm_enum.kvm_keylen, kvm->kvm_enum.kvm_val,
&kvm->kvm_enum.kvm_vallen, &kvm->kvm_enum.kvm_valtype))
kvh->kvh_err = HV_KVP_S_CONT;
else
kvh->kvh_err = HV_KVP_S_OK;
break;
default:
DPRINTF("KVP message op %u pool %u\n", kvh->kvh_op,
kvh->kvh_pool);
kvh->kvh_err = HV_KVP_S_CONT;
}
}
void
hv_kvp(void *arg)
{
struct hv_ic_dev *dv = arg;
struct hv_channel *ch = dv->dv_ch;
struct hv_softc *sc = ch->ch_sc;
struct hv_kvp *kvp = dv->dv_priv;
struct vmbus_icmsg_hdr *hdr;
uint64_t rid;
uint32_t fwver, msgver, rlen;
int rv;
for (;;) {
rv = hv_channel_recv(ch, dv->dv_buf, 2 * PAGE_SIZE,
&rlen, &rid, 0);
if (rv || rlen == 0) {
if (rv != EAGAIN)
DPRINTF("%s: kvp rv=%d rlen=%u\n",
sc->sc_dev.dv_xname, rv, rlen);
return;
}
if (rlen < sizeof(struct vmbus_icmsg_hdr)) {
DPRINTF("%s: kvp short read rlen=%u\n",
sc->sc_dev.dv_xname, rlen);
return;
}
hdr = (struct vmbus_icmsg_hdr *)dv->dv_buf;
switch (hdr->ic_type) {
case VMBUS_ICMSG_TYPE_NEGOTIATE:
switch (sc->sc_proto) {
case VMBUS_VERSION_WS2008:
fwver = VMBUS_IC_VERSION(1, 0);
msgver = VMBUS_IC_VERSION(1, 0);
break;
case VMBUS_VERSION_WIN7:
fwver = VMBUS_IC_VERSION(3, 0);
msgver = VMBUS_IC_VERSION(3, 0);
break;
default:
fwver = VMBUS_IC_VERSION(3, 0);
msgver = VMBUS_IC_VERSION(5, 0);
}
hv_ic_negotiate(hdr, &rlen, fwver, msgver);
break;
case VMBUS_ICMSG_TYPE_KVP:
if (hdr->ic_dsize >= sizeof(union hv_kvp_hdr))
hv_kvp_process(kvp,
(struct vmbus_icmsg_kvp *)hdr);
else
printf("%s: message too short: %u\n",
sc->sc_dev.dv_xname, hdr->ic_dsize);
break;
default:
printf("%s: unhandled kvp message type %u\n",
sc->sc_dev.dv_xname, hdr->ic_type);
continue;
}
hdr->ic_flags = VMBUS_ICMSG_FLAG_TRANSACTION |
VMBUS_ICMSG_FLAG_RESPONSE;
hv_channel_send(ch, dv->dv_buf, rlen, rid,
VMBUS_CHANPKT_TYPE_INBAND, 0);
}
}
static int
kvp_poolname(char **key)
{
char *p;
int i, rv = -1;
if ((p = strrchr(*key, '/')) == NULL)
return (rv);
*p = '\0';
for (i = 0; i < nitems(kvp_pools); i++) {
if (strncasecmp(*key, kvp_pools[i].poolname,
kvp_pools[i].poolnamelen) == 0) {
rv = kvp_pools[i].poolidx;
break;
}
}
if (rv >= 0)
*key = ++p;
return (rv);
}
int
hv_kvop(void *arg, int op, char *key, char *val, size_t vallen)
{
struct hv_ic_dev *dv = arg;
struct hv_kvp *kvp = dv->dv_priv;
struct kvp_pool *kvpl;
int next, pool, error = 0;
char *vp = val;
size_t keylen;
pool = kvp_poolname(&key);
if (pool == -1)
return (EINVAL);
kvpl = &kvp->kvp_pool[pool];
if (strlen(key) == 0) {
for (next = 0; next < MAXPOOLENTS; next++) {
if ((val + vallen < vp + HV_KVP_MAX_KEY_SIZE / 2) ||
kvp_pool_keys(kvpl, next, vp, &keylen))
goto out;
if (strlcat(val, "\n", vallen) >= vallen)
goto out;
vp += keylen;
}
out:
if (vp > val)
*(vp - 1) = '\0';
return (0);
}
if (op == PVBUS_KVWRITE) {
if (pool == HV_KVP_POOL_AUTO)
error = kvp_pool_update(kvpl, key, val, vallen,
HV_KVP_REG_SZ);
else if (pool == HV_KVP_POOL_GUEST)
error = kvp_pool_insert(kvpl, key, val, vallen,
HV_KVP_REG_SZ);
else
error = EINVAL;
} else
error = kvp_pool_extract(kvpl, key, val, vallen);
return (error);
}
|