network_conf.h 16.3 KB
Newer Older
1 2 3
/*
 * network_conf.h: network XML handling
 *
4
 * Copyright (C) 2006-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2006-2008 Daniel P. Berrange
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#ifndef __NETWORK_CONF_H__
25
# define __NETWORK_CONF_H__
26

27
# define DNS_RECORD_LENGTH_SRV  (512 - 30)  /* Limit minus overhead as mentioned in RFC-2782 */
28

29 30 31
# include <libxml/parser.h>
# include <libxml/tree.h>
# include <libxml/xpath.h>
32

33
# include "internal.h"
34
# include "virthread.h"
35 36 37
# include "virsocketaddr.h"
# include "virnetdevbandwidth.h"
# include "virnetdevvportprofile.h"
38
# include "virnetdevvlan.h"
39
# include "virmacaddr.h"
40
# include "device_conf.h"
41
# include "virbitmap.h"
42
# include "networkcommon_conf.h"
43

44
typedef enum {
45 46 47
    VIR_NETWORK_FORWARD_NONE   = 0,
    VIR_NETWORK_FORWARD_NAT,
    VIR_NETWORK_FORWARD_ROUTE,
48 49 50 51
    VIR_NETWORK_FORWARD_BRIDGE,
    VIR_NETWORK_FORWARD_PRIVATE,
    VIR_NETWORK_FORWARD_VEPA,
    VIR_NETWORK_FORWARD_PASSTHROUGH,
52
    VIR_NETWORK_FORWARD_HOSTDEV,
53 54

    VIR_NETWORK_FORWARD_LAST,
55
} virNetworkForwardType;
56

57 58 59 60 61 62 63 64 65 66
typedef enum {
   VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_DEFAULT = 0,
   VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_KERNEL,
   VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_LIBVIRT,

   VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_LAST,
} virNetworkBridgeMACTableManagerType;

VIR_ENUM_DECL(virNetworkBridgeMACTableManager)

67
typedef enum {
68 69 70 71 72 73
    VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NONE = 0,
    VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_PCI,
    VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV,
    /* USB Device to be added here when supported */

    VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_LAST,
74
} virNetworkForwardHostdevDeviceType;
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89
/* The backend driver used for devices from the pool. Currently used
 * only for PCI devices (vfio vs. kvm), but could be used for other
 * device types in the future.
 */
typedef enum {
    VIR_NETWORK_FORWARD_DRIVER_NAME_DEFAULT, /* kvm now, could change */
    VIR_NETWORK_FORWARD_DRIVER_NAME_KVM,    /* force legacy kvm style */
    VIR_NETWORK_FORWARD_DRIVER_NAME_VFIO,   /* force vfio */

    VIR_NETWORK_FORWARD_DRIVER_NAME_LAST
} virNetworkForwardDriverNameType;

VIR_ENUM_DECL(virNetworkForwardDriverName)

90 91 92 93
typedef struct _virNetworkDHCPHostDef virNetworkDHCPHostDef;
typedef virNetworkDHCPHostDef *virNetworkDHCPHostDefPtr;
struct _virNetworkDHCPHostDef {
    char *mac;
94
    char *id;
95
    char *name;
96
    virSocketAddr ip;
97 98
};

99 100 101
typedef struct _virNetworkDNSTxtDef virNetworkDNSTxtDef;
typedef virNetworkDNSTxtDef *virNetworkDNSTxtDefPtr;
struct _virNetworkDNSTxtDef {
102 103 104 105
    char *name;
    char *value;
};

106 107 108
typedef struct _virNetworkDNSSrvDef virNetworkDNSSrvDef;
typedef virNetworkDNSSrvDef *virNetworkDNSSrvDefPtr;
struct _virNetworkDNSSrvDef {
109 110 111 112
    char *domain;
    char *service;
    char *protocol;
    char *target;
113 114 115
    unsigned int port;
    unsigned int priority;
    unsigned int weight;
116 117
};

118 119 120
typedef struct _virNetworkDNSHostDef virNetworkDNSHostDef;
typedef virNetworkDNSHostDef *virNetworkDNSHostDefPtr;
struct _virNetworkDNSHostDef {
121
    virSocketAddr ip;
122
    size_t nnames;
123
    char **names;
124
};
125

126 127
typedef struct _virNetworkDNSDef virNetworkDNSDef;
typedef virNetworkDNSDef *virNetworkDNSDefPtr;
128
struct _virNetworkDNSDef {
J
Ján Tomko 已提交
129
    int forwardPlainNames; /* enum virTristateBool */
130 131 132 133 134 135
    size_t ntxts;
    virNetworkDNSTxtDefPtr txts;
    size_t nhosts;
    virNetworkDNSHostDefPtr hosts;
    size_t nsrvs;
    virNetworkDNSSrvDefPtr srvs;
136 137
    size_t nfwds;
    char **forwarders;
138
};
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
typedef struct _virNetworkIpDef virNetworkIpDef;
typedef virNetworkIpDef *virNetworkIpDefPtr;
struct _virNetworkIpDef {
    char *family;               /* ipv4 or ipv6 - default is ipv4 */
    virSocketAddr address;      /* Bridge IP address */

    /* One or the other of the following two will be used for a given
     * IP address, but never both. The parser guarantees this.
     * Use virNetworkIpDefPrefix/virNetworkIpDefNetmask rather
     * than accessing the data directly - these utility functions
     * will convert one into the other as necessary.
     */
    unsigned int prefix;        /* ipv6 - only prefix allowed */
    virSocketAddr netmask;      /* ipv4 - either netmask or prefix specified */

155
    size_t nranges;             /* Zero or more dhcp ranges */
156
    virSocketAddrRangePtr ranges;
157

158
    size_t nhosts;              /* Zero or more dhcp hosts */
159 160 161 162 163 164 165
    virNetworkDHCPHostDefPtr hosts;

    char *tftproot;
    char *bootfile;
    virSocketAddr bootserver;
   };

166 167 168
typedef struct _virNetworkForwardIfDef virNetworkForwardIfDef;
typedef virNetworkForwardIfDef *virNetworkForwardIfDefPtr;
struct _virNetworkForwardIfDef {
169 170 171 172 173 174 175
    int type;
    union {
        virDevicePCIAddress pci; /*PCI Address of device */
        /* when USB devices are supported a new variable to be added here */
        char *dev;      /* name of device */
    }device;
    int connections; /* how many guest interfaces are connected to this device? */
176 177
};

178 179 180 181
typedef struct _virNetworkForwardPfDef virNetworkForwardPfDef;
typedef virNetworkForwardPfDef *virNetworkForwardPfDefPtr;
struct _virNetworkForwardPfDef {
    char *dev;      /* name of device */
182
    int connections; /* how many guest interfaces are connected to this device? */
183 184
};

185 186 187 188 189
typedef struct _virNetworkForwardDef virNetworkForwardDef;
typedef virNetworkForwardDef *virNetworkForwardDefPtr;
struct _virNetworkForwardDef {
    int type;     /* One of virNetworkForwardType constants */
    bool managed;  /* managed attribute for hostdev mode */
190
    int driverName; /* enum virNetworkForwardDriverNameType */
191 192 193 194 195 196 197 198 199

    /* If there are multiple forward devices (i.e. a pool of
     * interfaces), they will be listed here.
     */
    size_t npfs;
    virNetworkForwardPfDefPtr pfs;

    size_t nifs;
    virNetworkForwardIfDefPtr ifs;
200

201
    /* ranges for NAT */
202 203
    virSocketAddrRange addr;
    virPortRange port;
204 205
};

206 207 208 209 210
typedef struct _virPortGroupDef virPortGroupDef;
typedef virPortGroupDef *virPortGroupDefPtr;
struct _virPortGroupDef {
    char *name;
    bool isDefault;
211
    virNetDevVPortProfilePtr virtPortProfile;
212
    virNetDevBandwidthPtr bandwidth;
213
    virNetDevVlan vlan;
214
    int trustGuestRxFilters; /* enum virTristateBool */
215 216
};

217 218 219 220
typedef struct _virNetworkDef virNetworkDef;
typedef virNetworkDef *virNetworkDefPtr;
struct _virNetworkDef {
    unsigned char uuid[VIR_UUID_BUFLEN];
M
Matthias Bolte 已提交
221
    bool uuid_specified;
222
    char *name;
223
    int   connections; /* # of guest interfaces connected to this network */
224 225

    char *bridge;       /* Name of bridge device */
226
    int  macTableManager; /* enum virNetworkBridgeMACTableManager */
227
    char *domain;
228
    int domainLocalOnly; /* enum virTristateBool: yes disables dns forwarding */
229
    unsigned long delay;   /* Bridge forward delay (ms) */
230
    bool stp; /* Spanning tree protocol */
231
    virMacAddr mac; /* mac address of bridge device */
232
    bool mac_specified;
233

234 235 236 237 238
    /* specified if ip6tables rules added
     * when no ipv6 gateway addresses specified.
     */
    bool ipv6nogw;

239
    virNetworkForwardDef forward;
240

241 242
    size_t nips;
    virNetworkIpDefPtr ips; /* ptr to array of IP addresses on this network */
243

244
    size_t nroutes;
245
    virNetworkRouteDefPtr *routes; /* ptr to array of static routes on this interface */
246

247
    virNetworkDNSDef dns;   /* dns related configuration */
248
    virNetDevVPortProfilePtr virtPortProfile;
249 250 251

    size_t nPortGroups;
    virPortGroupDefPtr portGroups;
252
    virNetDevBandwidthPtr bandwidth;
253
    virNetDevVlan vlan;
254
    int trustGuestRxFilters; /* enum virTristateBool */
255 256 257 258 259
};

typedef struct _virNetworkObj virNetworkObj;
typedef virNetworkObj *virNetworkObjPtr;
struct _virNetworkObj {
260
    virMutex lock;
261

262
    pid_t dnsmasqPid;
263
    pid_t radvdPid;
264 265 266 267 268 269
    unsigned int active : 1;
    unsigned int autostart : 1;
    unsigned int persistent : 1;

    virNetworkDefPtr def; /* The current definition */
    virNetworkDefPtr newDef; /* New definition to activate at shutdown */
270 271 272

    virBitmapPtr class_id; /* bitmap of class IDs for QoS */
    unsigned long long floor_sum; /* sum of all 'floor'-s of attached NICs */
273 274

    unsigned int taint;
275
};
276

277 278 279
typedef struct _virNetworkObjList virNetworkObjList;
typedef virNetworkObjList *virNetworkObjListPtr;
struct _virNetworkObjList {
280
    size_t count;
281
    virNetworkObjPtr *objs;
282 283
};

284
typedef enum {
285 286 287 288 289 290 291 292
    VIR_NETWORK_TAINT_HOOK,                 /* Hook script was executed over
                                               network. We can't guarantee
                                               connectivity or other settings
                                               as the script may have played
                                               with iptables, tc, you name it.
                                             */

    VIR_NETWORK_TAINT_LAST
293
} virNetworkTaintFlags;
294

295
static inline int
E
Eric Blake 已提交
296
virNetworkObjIsActive(const virNetworkObj *net)
297 298 299 300
{
    return net->active;
}

301
bool virNetworkObjTaint(virNetworkObjPtr obj,
302
                        virNetworkTaintFlags taint);
303

304 305
virNetworkObjPtr virNetworkObjFindByUUID(virNetworkObjListPtr nets,
                                         const unsigned char *uuid);
306 307
virNetworkObjPtr virNetworkObjFindByName(virNetworkObjListPtr nets,
                                         const char *name);
308 309 310 311


void virNetworkDefFree(virNetworkDefPtr def);
void virNetworkObjFree(virNetworkObjPtr net);
312
void virNetworkObjListFree(virNetworkObjListPtr nets);
313

314 315 316 317

typedef bool (*virNetworkObjListFilter)(virConnectPtr conn,
                                        virNetworkDefPtr def);

318
virNetworkObjPtr virNetworkAssignDef(virNetworkObjListPtr nets,
E
Eric Blake 已提交
319
                                     virNetworkDefPtr def,
320
                                     bool live);
321 322 323
void virNetworkObjAssignDef(virNetworkObjPtr network,
                            virNetworkDefPtr def,
                            bool live);
324
int virNetworkObjSetDefTransient(virNetworkObjPtr network, bool live);
325
void virNetworkObjUnsetDefTransient(virNetworkObjPtr network);
326 327 328 329 330 331
virNetworkDefPtr virNetworkObjGetPersistentDef(virNetworkObjPtr network);
int virNetworkObjReplacePersistentDef(virNetworkObjPtr network,
                                      virNetworkDefPtr def);
virNetworkDefPtr virNetworkDefCopy(virNetworkDefPtr def, unsigned int flags);
int virNetworkConfigChangeSetup(virNetworkObjPtr dom, unsigned int flags);

332
void virNetworkRemoveInactive(virNetworkObjListPtr nets,
E
Eric Blake 已提交
333
                              virNetworkObjPtr net);
334

335 336 337
virNetworkDefPtr virNetworkDefParseString(const char *xmlStr);
virNetworkDefPtr virNetworkDefParseFile(const char *filename);
virNetworkDefPtr virNetworkDefParseNode(xmlDocPtr xml,
338
                                        xmlNodePtr root);
E
Eric Blake 已提交
339
char *virNetworkDefFormat(const virNetworkDef *def, unsigned int flags);
340 341 342
int virNetworkDefFormatBuf(virBufferPtr buf,
                           const virNetworkDef *def,
                           unsigned int flags);
343

344
static inline const char *
E
Eric Blake 已提交
345
virNetworkDefForwardIf(const virNetworkDef *def, size_t n)
346
{
347 348 349
    return ((def->forward.ifs && (def->forward.nifs > n) &&
             def->forward.ifs[n].type == VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV)
            ? def->forward.ifs[n].device.dev : NULL);
350 351 352 353 354
}

virPortGroupDefPtr virPortGroupFindByName(virNetworkDefPtr net,
                                          const char *portgroup);

355
virNetworkIpDefPtr
E
Eric Blake 已提交
356
virNetworkDefGetIpByIndex(const virNetworkDef *def,
357
                          int family, size_t n);
E
Eric Blake 已提交
358 359
int virNetworkIpDefPrefix(const virNetworkIpDef *def);
int virNetworkIpDefNetmask(const virNetworkIpDef *def,
360
                           virSocketAddrPtr netmask);
361

362
int virNetworkSaveXML(const char *configDir,
363 364 365
                      virNetworkDefPtr def,
                      const char *xml);

366
int virNetworkSaveConfig(const char *configDir,
367
                         virNetworkDefPtr def);
368

369 370 371
int virNetworkSaveStatus(const char *statusDir,
                         virNetworkObjPtr net) ATTRIBUTE_RETURN_CHECK;

372
virNetworkObjPtr virNetworkLoadConfig(virNetworkObjListPtr nets,
373 374 375 376
                                      const char *configDir,
                                      const char *autostartDir,
                                      const char *file);

377 378 379 380
virNetworkObjPtr virNetworkLoadState(virNetworkObjListPtr nets,
                                     const char *stateDir,
                                     const char *name);

381
int virNetworkLoadAllConfigs(virNetworkObjListPtr nets,
382 383 384
                             const char *configDir,
                             const char *autostartDir);

385 386 387
int virNetworkLoadAllState(virNetworkObjListPtr nets,
                           const char *stateDir);

388
int virNetworkDeleteConfig(const char *configDir,
389
                           const char *autostartDir,
390 391
                           virNetworkObjPtr net);

392
char *virNetworkConfigFile(const char *dir,
393 394
                           const char *name);

E
Eric Blake 已提交
395
int virNetworkBridgeInUse(virNetworkObjListPtr nets,
396 397 398
                          const char *bridge,
                          const char *skipname);

E
Eric Blake 已提交
399
char *virNetworkAllocateBridge(virNetworkObjListPtr nets,
400
                               const char *template);
401

E
Eric Blake 已提交
402
int virNetworkSetBridgeName(virNetworkObjListPtr nets,
403 404
                            virNetworkDefPtr def,
                            int check_collision);
405

406 407
void virNetworkSetBridgeMacAddr(virNetworkDefPtr def);

408 409 410 411 412 413 414 415
int
virNetworkObjUpdate(virNetworkObjPtr obj,
                    unsigned int command, /* virNetworkUpdateCommand */
                    unsigned int section, /* virNetworkUpdateSection */
                    int parentIndex,
                    const char *xml,
                    unsigned int flags);  /* virNetworkUpdateFlags */

416
int virNetworkObjIsDuplicate(virNetworkObjListPtr nets,
417
                             virNetworkDefPtr def,
418
                             bool check_active);
419

D
Daniel P. Berrange 已提交
420 421 422
void virNetworkObjLock(virNetworkObjPtr obj);
void virNetworkObjUnlock(virNetworkObjPtr obj);

M
Matthias Bolte 已提交
423 424
VIR_ENUM_DECL(virNetworkForward)

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
# define VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE   \
                (VIR_CONNECT_LIST_NETWORKS_ACTIVE | \
                 VIR_CONNECT_LIST_NETWORKS_INACTIVE)

# define VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT   \
                (VIR_CONNECT_LIST_NETWORKS_PERSISTENT | \
                 VIR_CONNECT_LIST_NETWORKS_TRANSIENT)

# define VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART    \
                (VIR_CONNECT_LIST_NETWORKS_AUTOSTART |  \
                 VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART)

# define VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL                  \
                (VIR_CONNECT_LIST_NETWORKS_FILTERS_ACTIVE     | \
                 VIR_CONNECT_LIST_NETWORKS_FILTERS_PERSISTENT | \
                 VIR_CONNECT_LIST_NETWORKS_FILTERS_AUTOSTART)

442
int virNetworkObjListExport(virConnectPtr conn,
443
                            virNetworkObjListPtr netobjs,
444 445 446
                            virNetworkPtr **nets,
                            virNetworkObjListFilter filter,
                            unsigned int flags);
447

448 449 450 451 452 453
typedef int (*virNetworkObjListIterator)(virNetworkObjPtr net,
                                         void *opaque);

int virNetworkObjListForEach(virNetworkObjListPtr nets,
                             virNetworkObjListIterator callback,
                             void *opaque);
454 455 456 457 458 459 460 461

int virNetworkObjListGetNames(virNetworkObjListPtr nets,
                              bool active,
                              char **names,
                              int nnames,
                              virNetworkObjListFilter filter,
                              virConnectPtr conn);

J
Ján Tomko 已提交
462 463 464 465 466 467 468 469 470
/* for testing */
int
virNetworkDefUpdateSection(virNetworkDefPtr def,
                           unsigned int command, /* virNetworkUpdateCommand */
                           unsigned int section, /* virNetworkUpdateSection */
                           int parentIndex,
                           const char *xml,
                           unsigned int flags);  /* virNetworkUpdateFlags */

471
VIR_ENUM_DECL(virNetworkTaint)
472
#endif /* __NETWORK_CONF_H__ */