network_conf.h 16.6 KB
Newer Older
1 2 3
/*
 * network_conf.h: network XML handling
 *
4
 * Copyright (C) 2006-2015 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
# include "virobject.h"
44

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

    VIR_NETWORK_FORWARD_LAST,
56
} virNetworkForwardType;
57

58 59 60 61 62 63 64 65 66 67
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)

68
typedef enum {
69 70 71 72 73 74
    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,
75
} virNetworkForwardHostdevDeviceType;
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90
/* 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)

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

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

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

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

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

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
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 */

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

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

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

167 168 169
typedef struct _virNetworkForwardIfDef virNetworkForwardIfDef;
typedef virNetworkForwardIfDef *virNetworkForwardIfDefPtr;
struct _virNetworkForwardIfDef {
170 171 172 173 174 175 176
    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? */
177 178
};

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

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

    /* 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;
201

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

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

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

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

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

240
    virNetworkForwardDef forward;
241

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

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

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

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

typedef struct _virNetworkObj virNetworkObj;
typedef virNetworkObj *virNetworkObjPtr;
struct _virNetworkObj {
261
    virObjectLockable parent;
262

263
    pid_t dnsmasqPid;
264
    pid_t radvdPid;
265 266 267 268 269 270
    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 */
271 272 273

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

    unsigned int taint;
276
};
277

278
virNetworkObjPtr virNetworkObjNew(void);
279
void virNetworkObjEndAPI(virNetworkObjPtr *net);
280

281 282
typedef struct _virNetworkObjList virNetworkObjList;
typedef virNetworkObjList *virNetworkObjListPtr;
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
virNetworkObjListPtr virNetworkObjListNew(void);
302

303 304
virNetworkObjPtr virNetworkObjFindByUUIDLocked(virNetworkObjListPtr nets,
                                               const unsigned char *uuid);
305 306
virNetworkObjPtr virNetworkObjFindByUUID(virNetworkObjListPtr nets,
                                         const unsigned char *uuid);
307 308
virNetworkObjPtr virNetworkObjFindByNameLocked(virNetworkObjListPtr nets,
                                               const char *name);
309 310
virNetworkObjPtr virNetworkObjFindByName(virNetworkObjListPtr nets,
                                         const char *name);
311 312
bool virNetworkObjTaint(virNetworkObjPtr obj,
                        virNetworkTaintFlags taint);
313 314

void virNetworkDefFree(virNetworkDefPtr def);
315 316 317 318

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

319 320 321 322
enum {
    VIR_NETWORK_OBJ_LIST_ADD_LIVE = (1 << 0),
    VIR_NETWORK_OBJ_LIST_ADD_CHECK_LIVE = (1 << 1),
};
323
virNetworkObjPtr virNetworkAssignDef(virNetworkObjListPtr nets,
E
Eric Blake 已提交
324
                                     virNetworkDefPtr def,
325
                                     unsigned int flags);
326 327 328
void virNetworkObjAssignDef(virNetworkObjPtr network,
                            virNetworkDefPtr def,
                            bool live);
329
int virNetworkObjSetDefTransient(virNetworkObjPtr network, bool live);
330
void virNetworkObjUnsetDefTransient(virNetworkObjPtr network);
331 332 333 334 335 336
virNetworkDefPtr virNetworkObjGetPersistentDef(virNetworkObjPtr network);
int virNetworkObjReplacePersistentDef(virNetworkObjPtr network,
                                      virNetworkDefPtr def);
virNetworkDefPtr virNetworkDefCopy(virNetworkDefPtr def, unsigned int flags);
int virNetworkConfigChangeSetup(virNetworkObjPtr dom, unsigned int flags);

337
void virNetworkRemoveInactive(virNetworkObjListPtr nets,
E
Eric Blake 已提交
338
                              virNetworkObjPtr net);
339

340 341 342
virNetworkDefPtr virNetworkDefParseString(const char *xmlStr);
virNetworkDefPtr virNetworkDefParseFile(const char *filename);
virNetworkDefPtr virNetworkDefParseNode(xmlDocPtr xml,
343
                                        xmlNodePtr root);
E
Eric Blake 已提交
344
char *virNetworkDefFormat(const virNetworkDef *def, unsigned int flags);
345 346 347
int virNetworkDefFormatBuf(virBufferPtr buf,
                           const virNetworkDef *def,
                           unsigned int flags);
348

349
static inline const char *
E
Eric Blake 已提交
350
virNetworkDefForwardIf(const virNetworkDef *def, size_t n)
351
{
352 353 354
    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);
355 356 357 358 359
}

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

360
virNetworkIpDefPtr
E
Eric Blake 已提交
361
virNetworkDefGetIpByIndex(const virNetworkDef *def,
362
                          int family, size_t n);
363 364 365
virNetworkRouteDefPtr
virNetworkDefGetRouteByIndex(const virNetworkDef *def,
                             int family, size_t n);
E
Eric Blake 已提交
366 367
int virNetworkIpDefPrefix(const virNetworkIpDef *def);
int virNetworkIpDefNetmask(const virNetworkIpDef *def,
368
                           virSocketAddrPtr netmask);
369

370
int virNetworkSaveXML(const char *configDir,
371 372 373
                      virNetworkDefPtr def,
                      const char *xml);

374
int virNetworkSaveConfig(const char *configDir,
375
                         virNetworkDefPtr def);
376

377 378 379
int virNetworkSaveStatus(const char *statusDir,
                         virNetworkObjPtr net) ATTRIBUTE_RETURN_CHECK;

380
virNetworkObjPtr virNetworkLoadConfig(virNetworkObjListPtr nets,
381 382 383 384
                                      const char *configDir,
                                      const char *autostartDir,
                                      const char *file);

385 386 387 388
virNetworkObjPtr virNetworkLoadState(virNetworkObjListPtr nets,
                                     const char *stateDir,
                                     const char *name);

389
int virNetworkLoadAllConfigs(virNetworkObjListPtr nets,
390 391 392
                             const char *configDir,
                             const char *autostartDir);

393 394 395
int virNetworkLoadAllState(virNetworkObjListPtr nets,
                           const char *stateDir);

396
int virNetworkDeleteConfig(const char *configDir,
397
                           const char *autostartDir,
398 399
                           virNetworkObjPtr net);

400
char *virNetworkConfigFile(const char *dir,
401 402
                           const char *name);

E
Eric Blake 已提交
403
int virNetworkBridgeInUse(virNetworkObjListPtr nets,
404 405 406
                          const char *bridge,
                          const char *skipname);

407 408
void virNetworkSetBridgeMacAddr(virNetworkDefPtr def);

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

M
Matthias Bolte 已提交
417 418
VIR_ENUM_DECL(virNetworkForward)

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
# 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)

436
int virNetworkObjListExport(virConnectPtr conn,
437
                            virNetworkObjListPtr netobjs,
438 439 440
                            virNetworkPtr **nets,
                            virNetworkObjListFilter filter,
                            unsigned int flags);
441

442 443 444 445 446 447
typedef int (*virNetworkObjListIterator)(virNetworkObjPtr net,
                                         void *opaque);

int virNetworkObjListForEach(virNetworkObjListPtr nets,
                             virNetworkObjListIterator callback,
                             void *opaque);
448 449 450 451 452 453 454 455

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

456 457 458 459 460
int virNetworkObjListNumOfNetworks(virNetworkObjListPtr nets,
                                   bool active,
                                   virNetworkObjListFilter filter,
                                   virConnectPtr conn);

461 462 463
void virNetworkObjListPrune(virNetworkObjListPtr nets,
                            unsigned int flags);

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

473
VIR_ENUM_DECL(virNetworkTaint)
474
#endif /* __NETWORK_CONF_H__ */