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
|
/*
* interface_conf.c: interfaces XML handling
*
* Copyright (C) 2006-2010, 2013-2015 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "virerror.h"
#include "interface_conf.h"
#include "virxml.h"
#include "virbuffer.h"
#define VIR_FROM_THIS VIR_FROM_INTERFACE
VIR_ENUM_IMPL(virInterface,
VIR_INTERFACE_TYPE_LAST,
"ethernet", "bridge", "bond", "vlan",
);
static int
virInterfaceDefDevFormat(virBuffer *buf, const virInterfaceDef *def,
virInterfaceType parentIfType);
static void
virInterfaceIPDefFree(virInterfaceIPDef *def)
{
if (def == NULL)
return;
g_free(def->address);
g_free(def);
}
static void
virInterfaceProtocolDefFree(virInterfaceProtocolDef *def)
{
size_t i;
if (def == NULL)
return;
for (i = 0; i < def->nips; i++)
virInterfaceIPDefFree(def->ips[i]);
g_free(def->ips);
g_free(def->family);
g_free(def->gateway);
g_free(def);
}
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virInterfaceProtocolDef, virInterfaceProtocolDefFree);
void
virInterfaceDefFree(virInterfaceDef *def)
{
size_t i;
int pp;
if (def == NULL)
return;
g_free(def->name);
g_free(def->mac);
switch (def->type) {
case VIR_INTERFACE_TYPE_BRIDGE:
g_free(def->data.bridge.delay);
for (i = 0; i < def->data.bridge.nbItf; i++) {
if (def->data.bridge.itf[i] == NULL)
break; /* to cope with half parsed data on errors */
virInterfaceDefFree(def->data.bridge.itf[i]);
}
g_free(def->data.bridge.itf);
break;
case VIR_INTERFACE_TYPE_BOND:
g_free(def->data.bond.target);
for (i = 0; i < def->data.bond.nbItf; i++) {
if (def->data.bond.itf[i] == NULL)
break; /* to cope with half parsed data on errors */
virInterfaceDefFree(def->data.bond.itf[i]);
}
g_free(def->data.bond.itf);
break;
case VIR_INTERFACE_TYPE_VLAN:
g_free(def->data.vlan.tag);
g_free(def->data.vlan.dev_name);
break;
}
/* free all protos */
for (pp = 0; pp < def->nprotos; pp++)
virInterfaceProtocolDefFree(def->protos[pp]);
g_free(def->protos);
g_free(def);
}
static int
virInterfaceDefParseMtu(virInterfaceDef *def,
xmlXPathContextPtr ctxt)
{
if (virXPathUInt("string(./mtu/@size)", ctxt, &def->mtu) == -2)
return -1;
if (def->mtu > 100000) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("value of the 'size' attribute of 'mtu' element must be at most 100000"));
return -1;
}
return 0;
}
static int
virInterfaceDefParseStartMode(virInterfaceDef *def,
xmlXPathContextPtr ctxt)
{
g_autofree char *tmp = virXPathString("string(./start/@mode)", ctxt);
if (tmp == NULL) {
def->startmode = VIR_INTERFACE_START_UNSPECIFIED;
} else if (STREQ(tmp, "onboot")) {
def->startmode = VIR_INTERFACE_START_ONBOOT;
} else if (STREQ(tmp, "hotplug")) {
def->startmode = VIR_INTERFACE_START_HOTPLUG;
} else if (STREQ(tmp, "none")) {
def->startmode = VIR_INTERFACE_START_NONE;
} else {
virReportError(VIR_ERR_XML_ERROR,
_("unknown interface startmode %1$s"), tmp);
return -1;
}
return 0;
}
static int
virInterfaceDefParseBondMode(xmlXPathContextPtr ctxt)
{
g_autofree char *tmp = virXPathString("string(./@mode)", ctxt);
if (tmp == NULL)
return VIR_INTERFACE_BOND_NONE;
if (STREQ(tmp, "balance-rr")) {
return VIR_INTERFACE_BOND_BALRR;
} else if (STREQ(tmp, "active-backup")) {
return VIR_INTERFACE_BOND_ABACKUP;
} else if (STREQ(tmp, "balance-xor")) {
return VIR_INTERFACE_BOND_BALXOR;
} else if (STREQ(tmp, "broadcast")) {
return VIR_INTERFACE_BOND_BCAST;
} else if (STREQ(tmp, "802.3ad")) {
return VIR_INTERFACE_BOND_8023AD;
} else if (STREQ(tmp, "balance-tlb")) {
return VIR_INTERFACE_BOND_BALTLB;
} else if (STREQ(tmp, "balance-alb")) {
return VIR_INTERFACE_BOND_BALALB;
}
virReportError(VIR_ERR_XML_ERROR, _("unknown bonding mode %1$s"), tmp);
return -1;
}
static int
virInterfaceDefParseBondMiiCarrier(xmlXPathContextPtr ctxt)
{
g_autofree char *tmp = virXPathString("string(./miimon/@carrier)", ctxt);
if (tmp == NULL)
return VIR_INTERFACE_BOND_MII_NONE;
if (STREQ(tmp, "ioctl")) {
return VIR_INTERFACE_BOND_MII_IOCTL;
} else if (STREQ(tmp, "netif")) {
return VIR_INTERFACE_BOND_MII_NETIF;
}
virReportError(VIR_ERR_XML_ERROR, _("unknown mii bonding carrier %1$s"), tmp);
return -1;
}
static int
virInterfaceDefParseBondArpValid(xmlXPathContextPtr ctxt)
{
g_autofree char *tmp = virXPathString("string(./arpmon/@validate)", ctxt);
if (tmp == NULL)
return VIR_INTERFACE_BOND_ARP_NONE;
if (STREQ(tmp, "active")) {
return VIR_INTERFACE_BOND_ARP_ACTIVE;
} else if (STREQ(tmp, "backup")) {
return VIR_INTERFACE_BOND_ARP_BACKUP;
} else if (STREQ(tmp, "all")) {
return VIR_INTERFACE_BOND_ARP_ALL;
}
virReportError(VIR_ERR_XML_ERROR, _("unknown arp bonding validate %1$s"), tmp);
return -1;
}
static int
virInterfaceDefParseDhcp(virInterfaceProtocolDef *def,
xmlNodePtr dhcp)
{
virTristateBool peerdns;
def->dhcp = 1;
def->peerdns = -1;
if (virXMLPropTristateBool(dhcp, "peerdns", VIR_XML_PROP_NONE, &peerdns) < 0)
return -1;
if (peerdns != VIR_TRISTATE_BOOL_ABSENT) {
def->peerdns = peerdns == VIR_TRISTATE_BOOL_YES ? 1 : 0;
}
return 0;
}
static int
virInterfaceDefParseIP(virInterfaceIPDef *def,
xmlNodePtr node)
{
if (!(def->address = virXMLPropString(node, "address")))
return 0;
if (virXMLPropInt(node, "prefix", 0, VIR_XML_PROP_NONE, &def->prefix, 0) < 0)
return -1;
return 0;
}
static int
virInterfaceDefParseProtoIPv4(virInterfaceProtocolDef *def,
xmlXPathContextPtr ctxt)
{
xmlNodePtr dhcp;
g_autofree xmlNodePtr *ipNodes = NULL;
int nipNodes;
size_t i;
def->gateway = virXPathString("string(./route[1]/@gateway)", ctxt);
dhcp = virXPathNode("./dhcp", ctxt);
if (dhcp != NULL) {
if (virInterfaceDefParseDhcp(def, dhcp) < 0)
return -1;
}
nipNodes = virXPathNodeSet("./ip", ctxt, &ipNodes);
if (nipNodes < 0)
return -1;
if (ipNodes == NULL)
return 0;
def->ips = g_new0(virInterfaceIPDef *, nipNodes);
def->nips = 0;
for (i = 0; i < nipNodes; i++) {
virInterfaceIPDef *ip = g_new0(virInterfaceIPDef, 1);
if (virInterfaceDefParseIP(ip, ipNodes[i]) < 0) {
virInterfaceIPDefFree(ip);
return -1;
}
def->ips[def->nips++] = ip;
}
return 0;
}
static int
virInterfaceDefParseProtoIPv6(virInterfaceProtocolDef *def,
xmlXPathContextPtr ctxt)
{
xmlNodePtr dhcp;
g_autofree xmlNodePtr *ipNodes = NULL;
int nipNodes;
size_t i;
def->gateway = virXPathString("string(./route[1]/@gateway)", ctxt);
if (virXPathNode("./autoconf", ctxt) != NULL)
def->autoconf = 1;
dhcp = virXPathNode("./dhcp", ctxt);
if (dhcp != NULL) {
if (virInterfaceDefParseDhcp(def, dhcp) < 0)
return -1;
}
nipNodes = virXPathNodeSet("./ip", ctxt, &ipNodes);
if (nipNodes < 0)
return -1;
if (ipNodes == NULL)
return 0;
def->ips = g_new0(virInterfaceIPDef *, nipNodes);
def->nips = 0;
for (i = 0; i < nipNodes; i++) {
virInterfaceIPDef *ip = g_new0(virInterfaceIPDef, 1);
if (virInterfaceDefParseIP(ip, ipNodes[i]) < 0) {
virInterfaceIPDefFree(ip);
return -1;
}
def->ips[def->nips++] = ip;
}
return 0;
}
static int
virInterfaceDefParseIfAdressing(virInterfaceDef *def,
xmlXPathContextPtr ctxt)
{
VIR_XPATH_NODE_AUTORESTORE(ctxt)
g_autofree xmlNodePtr *protoNodes = NULL;
int nProtoNodes, pp;
nProtoNodes = virXPathNodeSet("./protocol", ctxt, &protoNodes);
if (nProtoNodes < 0)
return -1;
if (nProtoNodes == 0) {
/* no protocols is an acceptable outcome */
return 0;
}
def->protos = g_new0(virInterfaceProtocolDef *, nProtoNodes);
def->nprotos = 0;
for (pp = 0; pp < nProtoNodes; pp++) {
g_autoptr(virInterfaceProtocolDef) proto = g_new0(virInterfaceProtocolDef, 1);
if (!(proto->family = virXMLPropString(protoNodes[pp], "family"))) {
virReportError(VIR_ERR_XML_ERROR,
"%s", _("protocol misses the family attribute"));
return -1;
}
ctxt->node = protoNodes[pp];
if (STREQ(proto->family, "ipv4")) {
if (virInterfaceDefParseProtoIPv4(proto, ctxt) != 0)
return -1;
} else if (STREQ(proto->family, "ipv6")) {
if (virInterfaceDefParseProtoIPv6(proto, ctxt) != 0)
return -1;
} else {
virReportError(VIR_ERR_XML_ERROR,
_("unsupported protocol family '%1$s'"),
proto->family);
return -1;
}
def->protos[def->nprotos++] = g_steal_pointer(&proto);
}
return 0;
}
static int
virInterfaceDefParseBridge(virInterfaceDef *def,
xmlXPathContextPtr ctxt)
{
VIR_XPATH_NODE_AUTORESTORE(ctxt)
g_autofree xmlNodePtr *interfaces = NULL;
virInterfaceDef *itf;
g_autofree char *tmp = NULL;
int nbItf;
size_t i;
def->data.bridge.stp = -1;
if ((tmp = virXMLPropString(ctxt->node, "stp"))) {
if (STREQ(tmp, "on")) {
def->data.bridge.stp = 1;
} else if (STREQ(tmp, "off")) {
def->data.bridge.stp = 0;
} else {
virReportError(VIR_ERR_XML_ERROR,
_("bridge interface stp should be on or off got %1$s"),
tmp);
return 0;
}
}
def->data.bridge.delay = virXMLPropString(ctxt->node, "delay");
nbItf = virXPathNodeSet("./interface", ctxt, &interfaces);
if (nbItf < 0) {
return -1;
}
if (nbItf > 0) {
def->data.bridge.itf = g_new0(struct _virInterfaceDef *, nbItf);
def->data.bridge.nbItf = nbItf;
for (i = 0; i < nbItf; i++) {
ctxt->node = interfaces[i];
itf = virInterfaceDefParseXML(ctxt, VIR_INTERFACE_TYPE_BRIDGE);
if (itf == NULL) {
def->data.bridge.nbItf = i;
return -1;
}
def->data.bridge.itf[i] = itf;
}
}
return 0;
}
static int
virInterfaceDefParseBondItfs(virInterfaceDef *def,
xmlXPathContextPtr ctxt)
{
VIR_XPATH_NODE_AUTORESTORE(ctxt)
g_autofree xmlNodePtr *interfaces = NULL;
virInterfaceDef *itf;
int nbItf;
size_t i;
nbItf = virXPathNodeSet("./interface", ctxt, &interfaces);
if (nbItf < 0)
return -1;
if (nbItf == 0) {
return 0;
}
def->data.bond.itf = g_new0(struct _virInterfaceDef *, nbItf);
def->data.bond.nbItf = nbItf;
for (i = 0; i < nbItf; i++) {
ctxt->node = interfaces[i];
itf = virInterfaceDefParseXML(ctxt, VIR_INTERFACE_TYPE_BOND);
if (itf == NULL) {
def->data.bond.nbItf = i;
return -1;
}
def->data.bond.itf[i] = itf;
}
return 0;
}
static int
virInterfaceDefParseBond(virInterfaceDef *def,
xmlXPathContextPtr ctxt)
{
int res;
def->data.bond.mode = virInterfaceDefParseBondMode(ctxt);
if (def->data.bond.mode < 0)
return -1;
if (virInterfaceDefParseBondItfs(def, ctxt) != 0)
return -1;
if (virXPathNode("./miimon[1]", ctxt) != NULL) {
def->data.bond.monit = VIR_INTERFACE_BOND_MONIT_MII;
res = virXPathInt("string(./miimon/@freq)", ctxt,
&def->data.bond.frequency);
if ((res == -2) || (res == -1)) {
virReportError(VIR_ERR_XML_ERROR,
"%s", _("bond interface miimon freq missing or invalid"));
return -1;
}
res = virXPathInt("string(./miimon/@downdelay)", ctxt,
&def->data.bond.downdelay);
if (res == -2) {
virReportError(VIR_ERR_XML_ERROR,
"%s", _("bond interface miimon downdelay invalid"));
return -1;
}
res = virXPathInt("string(./miimon/@updelay)", ctxt,
&def->data.bond.updelay);
if (res == -2) {
virReportError(VIR_ERR_XML_ERROR,
"%s", _("bond interface miimon updelay invalid"));
return -1;
}
def->data.bond.carrier = virInterfaceDefParseBondMiiCarrier(ctxt);
if (def->data.bond.carrier < 0)
return -1;
} else if (virXPathNode("./arpmon[1]", ctxt) != NULL) {
def->data.bond.monit = VIR_INTERFACE_BOND_MONIT_ARP;
res = virXPathInt("string(./arpmon/@interval)", ctxt,
&def->data.bond.interval);
if ((res == -2) || (res == -1)) {
virReportError(VIR_ERR_XML_ERROR,
"%s", _("bond interface arpmon interval missing or invalid"));
return -1;
}
def->data.bond.target =
virXPathString("string(./arpmon/@target)", ctxt);
if (def->data.bond.target == NULL) {
virReportError(VIR_ERR_XML_ERROR,
"%s", _("bond interface arpmon target missing"));
return -1;
}
def->data.bond.validate = virInterfaceDefParseBondArpValid(ctxt);
if (def->data.bond.validate < 0)
return -1;
}
return 0;
}
static int
virInterfaceDefParseVlan(virInterfaceDef *def,
xmlXPathContextPtr ctxt)
{
def->data.vlan.tag = virXPathString("string(./@tag)", ctxt);
if (def->data.vlan.tag == NULL) {
virReportError(VIR_ERR_XML_ERROR,
"%s", _("vlan interface misses the tag attribute"));
return -1;
}
def->data.vlan.dev_name =
virXPathString("string(./interface/@name)", ctxt);
if (def->data.vlan.dev_name == NULL) {
virReportError(VIR_ERR_XML_ERROR,
"%s", _("vlan interface misses name attribute"));
return -1;
}
return 0;
}
virInterfaceDef *
virInterfaceDefParseXML(xmlXPathContextPtr ctxt,
int parentIfType)
{
VIR_XPATH_NODE_AUTORESTORE(ctxt)
g_autoptr(virInterfaceDef) def = NULL;
virInterfaceType type;
xmlNodePtr lnk;
if (virXMLPropEnum(ctxt->node, "type", virInterfaceTypeFromString,
VIR_XML_PROP_REQUIRED, &type) < 0)
return NULL;
def = g_new0(virInterfaceDef, 1);
if (((parentIfType == VIR_INTERFACE_TYPE_BOND)
&& (type != VIR_INTERFACE_TYPE_ETHERNET))
|| ((parentIfType == VIR_INTERFACE_TYPE_BRIDGE)
&& (type != VIR_INTERFACE_TYPE_ETHERNET)
&& (type != VIR_INTERFACE_TYPE_BOND)
&& (type != VIR_INTERFACE_TYPE_VLAN))
|| (parentIfType == VIR_INTERFACE_TYPE_ETHERNET)
|| (parentIfType == VIR_INTERFACE_TYPE_VLAN))
{
virReportError(VIR_ERR_XML_ERROR,
_("interface has unsupported type '%1$s'"),
virInterfaceTypeToString(type));
return NULL;
}
def->type = type;
if (!(def->name = virXMLPropString(ctxt->node, "name"))) {
virReportError(VIR_ERR_XML_ERROR, "%s", _("interface has no name"));
return NULL;
}
if (parentIfType == VIR_INTERFACE_TYPE_LAST) {
/* only recognize these in toplevel bond interfaces */
if (virInterfaceDefParseStartMode(def, ctxt) < 0)
return NULL;
if (virInterfaceDefParseMtu(def, ctxt) < 0)
return NULL;
if (virInterfaceDefParseIfAdressing(def, ctxt) < 0)
return NULL;
}
if (type != VIR_INTERFACE_TYPE_BRIDGE) {
/* link status makes no sense for a bridge */
lnk = virXPathNode("./link", ctxt);
if (lnk && virInterfaceLinkParseXML(lnk, &def->lnk) < 0)
return NULL;
}
switch (type) {
case VIR_INTERFACE_TYPE_ETHERNET: {
char *mac = virXPathString("string(./mac/@address)", ctxt);
if (mac != NULL)
def->mac = mac;
break;
}
case VIR_INTERFACE_TYPE_BRIDGE: {
xmlNodePtr bridge;
if (!(bridge = virXPathNode("./bridge[1]", ctxt))) {
virReportError(VIR_ERR_XML_ERROR,
"%s", _("bridge interface misses the bridge element"));
return NULL;
}
ctxt->node = bridge;
if (virInterfaceDefParseBridge(def, ctxt) < 0)
return NULL;
break;
}
case VIR_INTERFACE_TYPE_BOND: {
xmlNodePtr bond;
if (!(bond = virXPathNode("./bond[1]", ctxt))) {
virReportError(VIR_ERR_XML_ERROR,
"%s", _("bond interface misses the bond element"));
return NULL;
}
ctxt->node = bond;
if (virInterfaceDefParseBond(def, ctxt) < 0)
return NULL;
break;
}
case VIR_INTERFACE_TYPE_VLAN: {
xmlNodePtr vlan;
if (!(vlan = virXPathNode("./vlan[1]", ctxt))) {
virReportError(VIR_ERR_XML_ERROR,
"%s", _("vlan interface misses the vlan element"));
return NULL;
}
ctxt->node = vlan;
if (virInterfaceDefParseVlan(def, ctxt) < 0)
return NULL;
break;
}
case VIR_INTERFACE_TYPE_LAST:
return NULL;
}
return g_steal_pointer(&def);
}
virInterfaceDef *
virInterfaceDefParseString(const char *xmlStr,
unsigned int flags)
{
g_autoptr(xmlDoc) xml = NULL;
g_autoptr(xmlXPathContext) ctxt = NULL;
bool validate = flags & VIR_INTERFACE_DEFINE_VALIDATE;
if (!(xml = virXMLParse(NULL, xmlStr, _("(interface_definition)"),
"interface", &ctxt, "interface.rng", validate)))
return NULL;
return virInterfaceDefParseXML(ctxt, VIR_INTERFACE_TYPE_LAST);
}
static int
virInterfaceBridgeDefFormat(virBuffer *buf,
const virInterfaceDef *def)
{
size_t i;
virBufferAddLit(buf, "<bridge");
if (def->data.bridge.stp == 1)
virBufferAddLit(buf, " stp='on'");
else if (def->data.bridge.stp == 0)
virBufferAddLit(buf, " stp='off'");
if (def->data.bridge.delay != NULL)
virBufferAsprintf(buf, " delay='%s'", def->data.bridge.delay);
virBufferAddLit(buf, ">\n");
virBufferAdjustIndent(buf, 2);
for (i = 0; i < def->data.bridge.nbItf; i++) {
if (virInterfaceDefDevFormat(buf, def->data.bridge.itf[i],
VIR_INTERFACE_TYPE_BRIDGE) < 0)
return -1;
}
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</bridge>\n");
return 0;
}
static int
virInterfaceBondDefFormat(virBuffer *buf,
const virInterfaceDef *def)
{
size_t i;
virBufferAddLit(buf, "<bond");
if (def->data.bond.mode == VIR_INTERFACE_BOND_BALRR)
virBufferAddLit(buf, " mode='balance-rr'");
else if (def->data.bond.mode == VIR_INTERFACE_BOND_ABACKUP)
virBufferAddLit(buf, " mode='active-backup'");
else if (def->data.bond.mode == VIR_INTERFACE_BOND_BALXOR)
virBufferAddLit(buf, " mode='balance-xor'");
else if (def->data.bond.mode == VIR_INTERFACE_BOND_BCAST)
virBufferAddLit(buf, " mode='broadcast'");
else if (def->data.bond.mode == VIR_INTERFACE_BOND_8023AD)
virBufferAddLit(buf, " mode='802.3ad'");
else if (def->data.bond.mode == VIR_INTERFACE_BOND_BALTLB)
virBufferAddLit(buf, " mode='balance-tlb'");
else if (def->data.bond.mode == VIR_INTERFACE_BOND_BALALB)
virBufferAddLit(buf, " mode='balance-alb'");
virBufferAddLit(buf, ">\n");
virBufferAdjustIndent(buf, 2);
if (def->data.bond.monit == VIR_INTERFACE_BOND_MONIT_MII) {
virBufferAsprintf(buf, "<miimon freq='%d'",
def->data.bond.frequency);
if (def->data.bond.downdelay > 0)
virBufferAsprintf(buf, " downdelay='%d'", def->data.bond.downdelay);
if (def->data.bond.updelay > 0)
virBufferAsprintf(buf, " updelay='%d'", def->data.bond.updelay);
if (def->data.bond.carrier == VIR_INTERFACE_BOND_MII_IOCTL)
virBufferAddLit(buf, " carrier='ioctl'");
else if (def->data.bond.carrier == VIR_INTERFACE_BOND_MII_NETIF)
virBufferAddLit(buf, " carrier='netif'");
virBufferAddLit(buf, "/>\n");
} else if (def->data.bond.monit == VIR_INTERFACE_BOND_MONIT_ARP) {
if (def->data.bond.target == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("bond arp monitoring has no target"));
return -1;
}
virBufferAsprintf(buf, "<arpmon interval='%d' target='%s'",
def->data.bond.interval, def->data.bond.target);
if (def->data.bond.validate == VIR_INTERFACE_BOND_ARP_ACTIVE)
virBufferAddLit(buf, " validate='active'");
else if (def->data.bond.validate == VIR_INTERFACE_BOND_ARP_BACKUP)
virBufferAddLit(buf, " validate='backup'");
else if (def->data.bond.validate == VIR_INTERFACE_BOND_ARP_ALL)
virBufferAddLit(buf, " validate='all'");
virBufferAddLit(buf, "/>\n");
}
for (i = 0; i < def->data.bond.nbItf; i++) {
if (virInterfaceDefDevFormat(buf, def->data.bond.itf[i],
VIR_INTERFACE_TYPE_BOND) < 0)
return -1;
}
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</bond>\n");
return 0;
}
static int
virInterfaceVlanDefFormat(virBuffer *buf,
const virInterfaceDef *def)
{
if (def->data.vlan.tag == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("vlan misses the tag name"));
return -1;
}
virBufferAsprintf(buf, "<vlan tag='%s'", def->data.vlan.tag);
if (def->data.vlan.dev_name != NULL) {
virBufferAddLit(buf, ">\n");
virBufferAdjustIndent(buf, 2);
virBufferAsprintf(buf, "<interface name='%s'/>\n",
def->data.vlan.dev_name);
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</vlan>\n");
} else {
virBufferAddLit(buf, "/>\n");
}
return 0;
}
static int
virInterfaceProtocolDefFormat(virBuffer *buf,
const virInterfaceDef *def)
{
size_t i, j;
for (i = 0; i < def->nprotos; i++) {
virBufferAsprintf(buf, "<protocol family='%s'>\n",
def->protos[i]->family);
virBufferAdjustIndent(buf, 2);
if (def->protos[i]->autoconf)
virBufferAddLit(buf, "<autoconf/>\n");
if (def->protos[i]->dhcp) {
if (def->protos[i]->peerdns == 0)
virBufferAddLit(buf, "<dhcp peerdns='no'/>\n");
else if (def->protos[i]->peerdns == 1)
virBufferAddLit(buf, "<dhcp peerdns='yes'/>\n");
else
virBufferAddLit(buf, "<dhcp/>\n");
}
for (j = 0; j < def->protos[i]->nips; j++) {
if (def->protos[i]->ips[j]->address != NULL) {
virBufferAsprintf(buf, "<ip address='%s'",
def->protos[i]->ips[j]->address);
if (def->protos[i]->ips[j]->prefix != 0) {
virBufferAsprintf(buf, " prefix='%d'",
def->protos[i]->ips[j]->prefix);
}
virBufferAddLit(buf, "/>\n");
}
}
if (def->protos[i]->gateway != NULL) {
virBufferAsprintf(buf, "<route gateway='%s'/>\n",
def->protos[i]->gateway);
}
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</protocol>\n");
}
return 0;
}
static int
virInterfaceStartmodeDefFormat(virBuffer *buf,
virInterfaceStartMode startmode)
{
const char *mode;
switch (startmode) {
case VIR_INTERFACE_START_UNSPECIFIED:
return 0;
case VIR_INTERFACE_START_NONE:
mode = "none";
break;
case VIR_INTERFACE_START_ONBOOT:
mode = "onboot";
break;
case VIR_INTERFACE_START_HOTPLUG:
mode = "hotplug";
break;
default:
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("virInterfaceDefFormat unknown startmode"));
return -1;
}
virBufferAsprintf(buf, "<start mode='%s'/>\n", mode);
return 0;
}
static int
virInterfaceDefDevFormat(virBuffer *buf,
const virInterfaceDef *def,
virInterfaceType parentIfType)
{
const char *type = NULL;
if (def == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("virInterfaceDefFormat NULL def"));
return -1;
}
if ((def->name == NULL) && (def->type != VIR_INTERFACE_TYPE_VLAN)) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("virInterfaceDefFormat missing interface name"));
return -1;
}
if (!(type = virInterfaceTypeToString(def->type))) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unexpected interface type %1$d"), def->type);
return -1;
}
virBufferAsprintf(buf, "<interface type='%s' ", type);
if (def->name != NULL)
virBufferEscapeString(buf, "name='%s'", def->name);
virBufferAddLit(buf, ">\n");
virBufferAdjustIndent(buf, 2);
if (parentIfType == VIR_INTERFACE_TYPE_LAST) {
/* these elements are only valid on top-level interfaces - IP
* address info ("protocol") only makes sense for the
* top-level, and subordinate interfaces inherit the toplevel
* setting for mtu and start mode, which cannot be overridden.
*/
virInterfaceStartmodeDefFormat(buf, def->startmode);
if (def->mtu)
virBufferAsprintf(buf, "<mtu size='%d'/>\n", def->mtu);
virInterfaceProtocolDefFormat(buf, def);
}
if (def->type != VIR_INTERFACE_TYPE_BRIDGE)
virInterfaceLinkFormat(buf, &def->lnk);
switch (def->type) {
case VIR_INTERFACE_TYPE_ETHERNET:
if (def->mac)
virBufferAsprintf(buf, "<mac address='%s'/>\n", def->mac);
break;
case VIR_INTERFACE_TYPE_BRIDGE:
if (virInterfaceBridgeDefFormat(buf, def) < 0)
return -1;
break;
case VIR_INTERFACE_TYPE_BOND:
if (virInterfaceBondDefFormat(buf, def) < 0)
return -1;
break;
case VIR_INTERFACE_TYPE_VLAN:
if (virInterfaceVlanDefFormat(buf, def) < 0)
return -1;
break;
}
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</interface>\n");
return 0;
}
char *
virInterfaceDefFormat(const virInterfaceDef *def)
{
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
if (virInterfaceDefDevFormat(&buf, def, VIR_INTERFACE_TYPE_LAST) < 0)
return NULL;
return virBufferContentAndReset(&buf);
}
|