network_conf.h 13.0 KB
Newer Older
1 2 3
/*
 * network_conf.h: network XML handling
 *
4
 * Copyright (C) 2006-2008, 2012 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 43 44 45 46

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

    VIR_NETWORK_FORWARD_LAST,
};

56 57 58 59 60 61 62 63 64
enum virNetworkForwardHostdevDeviceType {
    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,
};

65 66 67
typedef struct _virNetworkDHCPRangeDef virNetworkDHCPRangeDef;
typedef virNetworkDHCPRangeDef *virNetworkDHCPRangeDefPtr;
struct _virNetworkDHCPRangeDef {
68 69
    virSocketAddr start;
    virSocketAddr end;
70 71
};

72 73 74 75 76
typedef struct _virNetworkDHCPHostDef virNetworkDHCPHostDef;
typedef virNetworkDHCPHostDef *virNetworkDHCPHostDefPtr;
struct _virNetworkDHCPHostDef {
    char *mac;
    char *name;
77
    virSocketAddr ip;
78 79
};

80 81 82
typedef struct _virNetworkDNSTxtDef virNetworkDNSTxtDef;
typedef virNetworkDNSTxtDef *virNetworkDNSTxtDefPtr;
struct _virNetworkDNSTxtDef {
83 84 85 86
    char *name;
    char *value;
};

87 88 89
typedef struct _virNetworkDNSSrvDef virNetworkDNSSrvDef;
typedef virNetworkDNSSrvDef *virNetworkDNSSrvDefPtr;
struct _virNetworkDNSSrvDef {
90 91 92 93
    char *domain;
    char *service;
    char *protocol;
    char *target;
94 95 96
    unsigned int port;
    unsigned int priority;
    unsigned int weight;
97 98
};

99 100 101
typedef struct _virNetworkDNSHostDef virNetworkDNSHostDef;
typedef virNetworkDNSHostDef *virNetworkDNSHostDefPtr;
struct _virNetworkDNSHostDef {
102 103 104
    virSocketAddr ip;
    int nnames;
    char **names;
105
};
106

107 108
typedef struct _virNetworkDNSDef virNetworkDNSDef;
typedef virNetworkDNSDef *virNetworkDNSDefPtr;
109
struct _virNetworkDNSDef {
110 111 112 113 114 115
    size_t ntxts;
    virNetworkDNSTxtDefPtr txts;
    size_t nhosts;
    virNetworkDNSHostDefPtr hosts;
    size_t nsrvs;
    virNetworkDNSSrvDefPtr srvs;
116
};
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
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 */

133
    size_t nranges;             /* Zero or more dhcp ranges */
134 135
    virNetworkDHCPRangeDefPtr ranges;

136
    size_t nhosts;              /* Zero or more dhcp hosts */
137 138 139 140 141 142 143
    virNetworkDHCPHostDefPtr hosts;

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

144 145 146
typedef struct _virNetworkForwardIfDef virNetworkForwardIfDef;
typedef virNetworkForwardIfDef *virNetworkForwardIfDefPtr;
struct _virNetworkForwardIfDef {
147 148 149 150 151 152 153
    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? */
154 155
};

156 157 158 159
typedef struct _virNetworkForwardPfDef virNetworkForwardPfDef;
typedef virNetworkForwardPfDef *virNetworkForwardPfDefPtr;
struct _virNetworkForwardPfDef {
    char *dev;      /* name of device */
160
    int connections; /* how many guest interfaces are connected to this device? */
161 162
};

163 164 165 166 167 168 169 170 171 172 173 174 175 176
typedef struct _virNetworkForwardDef virNetworkForwardDef;
typedef virNetworkForwardDef *virNetworkForwardDefPtr;
struct _virNetworkForwardDef {
    int type;     /* One of virNetworkForwardType constants */
    bool managed;  /* managed attribute for hostdev mode */

    /* 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;
177 178 179

    /* adresses for SNAT */
    virSocketAddr addrStart, addrEnd;
180 181
};

182 183 184 185 186
typedef struct _virPortGroupDef virPortGroupDef;
typedef virPortGroupDef *virPortGroupDefPtr;
struct _virPortGroupDef {
    char *name;
    bool isDefault;
187
    virNetDevVPortProfilePtr virtPortProfile;
188
    virNetDevBandwidthPtr bandwidth;
189
    virNetDevVlan vlan;
190 191
};

192 193 194 195
typedef struct _virNetworkDef virNetworkDef;
typedef virNetworkDef *virNetworkDefPtr;
struct _virNetworkDef {
    unsigned char uuid[VIR_UUID_BUFLEN];
M
Matthias Bolte 已提交
196
    bool uuid_specified;
197
    char *name;
198
    int   connections; /* # of guest interfaces connected to this network */
199 200

    char *bridge;       /* Name of bridge device */
201
    char *domain;
202
    unsigned long delay;   /* Bridge forward delay (ms) */
203
    unsigned int stp :1; /* Spanning tree protocol */
204
    virMacAddr mac; /* mac address of bridge device */
205
    bool mac_specified;
206

207 208 209 210 211
    /* specified if ip6tables rules added
     * when no ipv6 gateway addresses specified.
     */
    bool ipv6nogw;

212
    virNetworkForwardDef forward;
213

214 215
    size_t nips;
    virNetworkIpDefPtr ips; /* ptr to array of IP addresses on this network */
216

217
    virNetworkDNSDef dns;   /* dns related configuration */
218
    virNetDevVPortProfilePtr virtPortProfile;
219 220 221

    size_t nPortGroups;
    virPortGroupDefPtr portGroups;
222
    virNetDevBandwidthPtr bandwidth;
223
    virNetDevVlan vlan;
224 225 226 227 228
};

typedef struct _virNetworkObj virNetworkObj;
typedef virNetworkObj *virNetworkObjPtr;
struct _virNetworkObj {
229
    virMutex lock;
230

231
    pid_t dnsmasqPid;
232
    pid_t radvdPid;
233 234 235 236 237 238
    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 */
239 240 241

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

244 245 246 247 248
typedef struct _virNetworkObjList virNetworkObjList;
typedef virNetworkObjList *virNetworkObjListPtr;
struct _virNetworkObjList {
    unsigned int count;
    virNetworkObjPtr *objs;
249 250 251
};

static inline int
D
Daniel P. Berrange 已提交
252
virNetworkObjIsActive(const virNetworkObjPtr net)
253 254 255 256
{
    return net->active;
}

257
virNetworkObjPtr virNetworkFindByUUID(const virNetworkObjListPtr nets,
258
                                      const unsigned char *uuid);
259
virNetworkObjPtr virNetworkFindByName(const virNetworkObjListPtr nets,
260 261 262 263 264
                                      const char *name);


void virNetworkDefFree(virNetworkDefPtr def);
void virNetworkObjFree(virNetworkObjPtr net);
265
void virNetworkObjListFree(virNetworkObjListPtr vms);
266

267
virNetworkObjPtr virNetworkAssignDef(virNetworkObjListPtr nets,
268 269 270 271 272 273
                                     const virNetworkDefPtr def,
                                     bool live);
int virNetworkObjAssignDef(virNetworkObjPtr network,
                           const virNetworkDefPtr def,
                           bool live);
int virNetworkObjSetDefTransient(virNetworkObjPtr network, bool live);
274
void virNetworkObjUnsetDefTransient(virNetworkObjPtr network);
275 276 277 278 279 280
virNetworkDefPtr virNetworkObjGetPersistentDef(virNetworkObjPtr network);
int virNetworkObjReplacePersistentDef(virNetworkObjPtr network,
                                      virNetworkDefPtr def);
virNetworkDefPtr virNetworkDefCopy(virNetworkDefPtr def, unsigned int flags);
int virNetworkConfigChangeSetup(virNetworkObjPtr dom, unsigned int flags);

281
void virNetworkRemoveInactive(virNetworkObjListPtr nets,
282 283
                              const virNetworkObjPtr net);

284 285 286
virNetworkDefPtr virNetworkDefParseString(const char *xmlStr);
virNetworkDefPtr virNetworkDefParseFile(const char *filename);
virNetworkDefPtr virNetworkDefParseNode(xmlDocPtr xml,
287
                                        xmlNodePtr root);
288 289
int virNetworkObjUpdateParseFile(const char *filename,
                                 virNetworkObjPtr net);
290

291
char *virNetworkDefFormat(const virNetworkDefPtr def, unsigned int flags);
292

293 294 295
static inline const char *
virNetworkDefForwardIf(const virNetworkDefPtr def, size_t n)
{
296 297 298
    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);
299 300 301 302 303
}

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

304 305 306 307 308 309
virNetworkIpDefPtr
virNetworkDefGetIpByIndex(const virNetworkDefPtr def,
                          int family, size_t n);
int virNetworkIpDefPrefix(const virNetworkIpDefPtr def);
int virNetworkIpDefNetmask(const virNetworkIpDefPtr def,
                           virSocketAddrPtr netmask);
310

311
int virNetworkSaveXML(const char *configDir,
312 313 314
                      virNetworkDefPtr def,
                      const char *xml);

315
int virNetworkSaveConfig(const char *configDir,
316
                         virNetworkDefPtr def);
317

318 319 320
int virNetworkSaveStatus(const char *statusDir,
                         virNetworkObjPtr net) ATTRIBUTE_RETURN_CHECK;

321
virNetworkObjPtr virNetworkLoadConfig(virNetworkObjListPtr nets,
322 323 324 325
                                      const char *configDir,
                                      const char *autostartDir,
                                      const char *file);

326
int virNetworkLoadAllConfigs(virNetworkObjListPtr nets,
327 328 329
                             const char *configDir,
                             const char *autostartDir);

330
int virNetworkDeleteConfig(const char *configDir,
331
                           const char *autostartDir,
332 333
                           virNetworkObjPtr net);

334
char *virNetworkConfigFile(const char *dir,
335 336
                           const char *name);

337 338 339 340
int virNetworkBridgeInUse(const virNetworkObjListPtr nets,
                          const char *bridge,
                          const char *skipname);

341
char *virNetworkAllocateBridge(const virNetworkObjListPtr nets,
342
                               const char *template);
343

344
int virNetworkSetBridgeName(const virNetworkObjListPtr nets,
345 346
                            virNetworkDefPtr def,
                            int check_collision);
347

348 349
void virNetworkSetBridgeMacAddr(virNetworkDefPtr def);

350 351 352 353 354 355 356 357
int
virNetworkObjUpdate(virNetworkObjPtr obj,
                    unsigned int command, /* virNetworkUpdateCommand */
                    unsigned int section, /* virNetworkUpdateSection */
                    int parentIndex,
                    const char *xml,
                    unsigned int flags);  /* virNetworkUpdateFlags */

358 359
int virNetworkObjIsDuplicate(virNetworkObjListPtr doms,
                             virNetworkDefPtr def,
360
                             bool check_active);
361

D
Daniel P. Berrange 已提交
362 363 364
void virNetworkObjLock(virNetworkObjPtr obj);
void virNetworkObjUnlock(virNetworkObjPtr obj);

M
Matthias Bolte 已提交
365 366
VIR_ENUM_DECL(virNetworkForward)

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
# 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)

int virNetworkList(virConnectPtr conn,
                   virNetworkObjList netobjs,
                   virNetworkPtr **nets,
                   unsigned int flags);

389
#endif /* __NETWORK_CONF_H__ */