network_conf.h 12.5 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 34
# include "internal.h"
# include "threads.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 42 43 44 45

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

    VIR_NETWORK_FORWARD_LAST,
};

55 56 57 58 59 60 61 62 63
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,
};

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

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

79 80 81 82 83 84 85
typedef struct _virNetworkDNSTxtRecordsDef virNetworkDNSTxtRecordsDef;
typedef virNetworkDNSTxtRecordsDef *virNetworkDNSTxtRecordsDefPtr;
struct _virNetworkDNSTxtRecordsDef {
    char *name;
    char *value;
};

86 87 88 89 90 91 92 93 94 95 96 97
typedef struct _virNetworkDNSSrvRecordsDef virNetworkDNSSrvRecordsDef;
typedef virNetworkDNSSrvRecordsDef *virNetworkDNSSrvRecordsDefPtr;
struct _virNetworkDNSSrvRecordsDef {
    char *domain;
    char *service;
    char *protocol;
    char *target;
    int port;
    int priority;
    int weight;
};

98
struct _virNetworkDNSHostsDef {
99 100 101
    virSocketAddr ip;
    int nnames;
    char **names;
102
};
103

104
typedef struct _virNetworkDNSHostsDef *virNetworkDNSHostsDefPtr;
105

106
struct _virNetworkDNSDef {
107 108
    unsigned int ntxtrecords;
    virNetworkDNSTxtRecordsDefPtr txtrecords;
109 110
    unsigned int nhosts;
    virNetworkDNSHostsDefPtr hosts;
111 112
    unsigned int nsrvrecords;
    virNetworkDNSSrvRecordsDefPtr srvrecords;
113
};
114

115
typedef struct _virNetworkDNSDef *virNetworkDNSDefPtr;
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
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 */

    unsigned int nranges;        /* Zero or more dhcp ranges */
    virNetworkDHCPRangeDefPtr ranges;

    unsigned int nhosts;         /* Zero or more dhcp hosts */
    virNetworkDHCPHostDefPtr hosts;

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

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

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

162 163 164 165 166
typedef struct _virPortGroupDef virPortGroupDef;
typedef virPortGroupDef *virPortGroupDefPtr;
struct _virPortGroupDef {
    char *name;
    bool isDefault;
167
    virNetDevVPortProfilePtr virtPortProfile;
168
    virNetDevBandwidthPtr bandwidth;
169
    virNetDevVlan vlan;
170 171
};

172 173 174 175
typedef struct _virNetworkDef virNetworkDef;
typedef virNetworkDef *virNetworkDefPtr;
struct _virNetworkDef {
    unsigned char uuid[VIR_UUID_BUFLEN];
M
Matthias Bolte 已提交
176
    bool uuid_specified;
177
    char *name;
178
    int   connections; /* # of guest interfaces connected to this network */
179 180

    char *bridge;       /* Name of bridge device */
181
    char *domain;
182
    unsigned long delay;   /* Bridge forward delay (ms) */
183
    unsigned int stp :1; /* Spanning tree protocol */
184
    virMacAddr mac; /* mac address of bridge device */
185
    bool mac_specified;
186

187 188 189 190 191
    /* specified if ip6tables rules added
     * when no ipv6 gateway addresses specified.
     */
    bool ipv6nogw;

192
    int forwardType;    /* One of virNetworkForwardType constants */
193
    int managed;        /* managed attribute for hostdev mode */
194 195 196 197

    /* If there are multiple forward devices (i.e. a pool of
     * interfaces), they will be listed here.
     */
198
    size_t nForwardPfs;
199
    virNetworkForwardPfDefPtr forwardPfs;
200

201 202
    size_t nForwardIfs;
    virNetworkForwardIfDefPtr forwardIfs;
203

204 205
    size_t nips;
    virNetworkIpDefPtr ips; /* ptr to array of IP addresses on this network */
206 207

    virNetworkDNSDefPtr dns; /* ptr to dns related configuration */
208
    virNetDevVPortProfilePtr virtPortProfile;
209 210 211

    size_t nPortGroups;
    virPortGroupDefPtr portGroups;
212
    virNetDevBandwidthPtr bandwidth;
213
    virNetDevVlan vlan;
214 215 216 217 218
};

typedef struct _virNetworkObj virNetworkObj;
typedef virNetworkObj *virNetworkObjPtr;
struct _virNetworkObj {
219
    virMutex lock;
220

221
    pid_t dnsmasqPid;
222
    pid_t radvdPid;
223 224 225 226 227 228
    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 */
229
};
230

231 232 233 234 235
typedef struct _virNetworkObjList virNetworkObjList;
typedef virNetworkObjList *virNetworkObjListPtr;
struct _virNetworkObjList {
    unsigned int count;
    virNetworkObjPtr *objs;
236 237 238
};

static inline int
D
Daniel P. Berrange 已提交
239
virNetworkObjIsActive(const virNetworkObjPtr net)
240 241 242 243
{
    return net->active;
}

244
virNetworkObjPtr virNetworkFindByUUID(const virNetworkObjListPtr nets,
245
                                      const unsigned char *uuid);
246
virNetworkObjPtr virNetworkFindByName(const virNetworkObjListPtr nets,
247 248 249 250 251
                                      const char *name);


void virNetworkDefFree(virNetworkDefPtr def);
void virNetworkObjFree(virNetworkObjPtr net);
252
void virNetworkObjListFree(virNetworkObjListPtr vms);
253

254
virNetworkObjPtr virNetworkAssignDef(virNetworkObjListPtr nets,
255 256 257 258 259 260
                                     const virNetworkDefPtr def,
                                     bool live);
int virNetworkObjAssignDef(virNetworkObjPtr network,
                           const virNetworkDefPtr def,
                           bool live);
int virNetworkObjSetDefTransient(virNetworkObjPtr network, bool live);
261
void virNetworkObjUnsetDefTransient(virNetworkObjPtr network);
262 263 264 265 266 267
virNetworkDefPtr virNetworkObjGetPersistentDef(virNetworkObjPtr network);
int virNetworkObjReplacePersistentDef(virNetworkObjPtr network,
                                      virNetworkDefPtr def);
virNetworkDefPtr virNetworkDefCopy(virNetworkDefPtr def, unsigned int flags);
int virNetworkConfigChangeSetup(virNetworkObjPtr dom, unsigned int flags);

268
void virNetworkRemoveInactive(virNetworkObjListPtr nets,
269 270
                              const virNetworkObjPtr net);

271 272 273
virNetworkDefPtr virNetworkDefParseString(const char *xmlStr);
virNetworkDefPtr virNetworkDefParseFile(const char *filename);
virNetworkDefPtr virNetworkDefParseNode(xmlDocPtr xml,
274 275
                                        xmlNodePtr root);

276
char *virNetworkDefFormat(const virNetworkDefPtr def, unsigned int flags);
277

278 279 280
static inline const char *
virNetworkDefForwardIf(const virNetworkDefPtr def, size_t n)
{
281 282 283
    return ((def->forwardIfs && (def->nForwardIfs > n) &&
             def->forwardIfs[n].type == VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV)
            ? def->forwardIfs[n].device.dev : NULL);
284 285 286 287 288
}

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

289 290 291 292 293 294
virNetworkIpDefPtr
virNetworkDefGetIpByIndex(const virNetworkDefPtr def,
                          int family, size_t n);
int virNetworkIpDefPrefix(const virNetworkIpDefPtr def);
int virNetworkIpDefNetmask(const virNetworkIpDefPtr def,
                           virSocketAddrPtr netmask);
295

296
int virNetworkSaveXML(const char *configDir,
297 298 299
                      virNetworkDefPtr def,
                      const char *xml);

300
int virNetworkSaveConfig(const char *configDir,
301
                         virNetworkDefPtr def);
302

303 304 305
int virNetworkSaveStatus(const char *statusDir,
                         virNetworkObjPtr net) ATTRIBUTE_RETURN_CHECK;

306
virNetworkObjPtr virNetworkLoadConfig(virNetworkObjListPtr nets,
307 308 309 310
                                      const char *configDir,
                                      const char *autostartDir,
                                      const char *file);

311
int virNetworkLoadAllConfigs(virNetworkObjListPtr nets,
312 313 314
                             const char *configDir,
                             const char *autostartDir);

315
int virNetworkDeleteConfig(const char *configDir,
316
                           const char *autostartDir,
317 318
                           virNetworkObjPtr net);

319
char *virNetworkConfigFile(const char *dir,
320 321
                           const char *name);

322 323 324 325
int virNetworkBridgeInUse(const virNetworkObjListPtr nets,
                          const char *bridge,
                          const char *skipname);

326
char *virNetworkAllocateBridge(const virNetworkObjListPtr nets,
327
                               const char *template);
328

329
int virNetworkSetBridgeName(const virNetworkObjListPtr nets,
330 331
                            virNetworkDefPtr def,
                            int check_collision);
332

333 334
void virNetworkSetBridgeMacAddr(virNetworkDefPtr def);

335 336 337 338 339 340 341 342
int
virNetworkObjUpdate(virNetworkObjPtr obj,
                    unsigned int command, /* virNetworkUpdateCommand */
                    unsigned int section, /* virNetworkUpdateSection */
                    int parentIndex,
                    const char *xml,
                    unsigned int flags);  /* virNetworkUpdateFlags */

343 344
int virNetworkObjIsDuplicate(virNetworkObjListPtr doms,
                             virNetworkDefPtr def,
345
                             bool check_active);
346

D
Daniel P. Berrange 已提交
347 348 349
void virNetworkObjLock(virNetworkObjPtr obj);
void virNetworkObjUnlock(virNetworkObjPtr obj);

M
Matthias Bolte 已提交
350 351
VIR_ENUM_DECL(virNetworkForward)

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
# 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);

374
#endif /* __NETWORK_CONF_H__ */