network_conf.h 12.6 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
typedef struct _virNetworkDNSTxtDef virNetworkDNSTxtDef;
typedef virNetworkDNSTxtDef *virNetworkDNSTxtDefPtr;
struct _virNetworkDNSTxtDef {
82 83 84 85
    char *name;
    char *value;
};

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

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

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

117
typedef struct _virNetworkDNSDef *virNetworkDNSDefPtr;
118

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

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

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

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

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

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

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

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

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

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

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

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

203 204
    size_t nForwardIfs;
    virNetworkForwardIfDefPtr forwardIfs;
205

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

    virNetworkDNSDefPtr dns; /* ptr to dns related configuration */
210
    virNetDevVPortProfilePtr virtPortProfile;
211 212 213

    size_t nPortGroups;
    virPortGroupDefPtr portGroups;
214
    virNetDevBandwidthPtr bandwidth;
215
    virNetDevVlan vlan;
216 217 218 219 220
};

typedef struct _virNetworkObj virNetworkObj;
typedef virNetworkObj *virNetworkObjPtr;
struct _virNetworkObj {
221
    virMutex lock;
222

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

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

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

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


void virNetworkDefFree(virNetworkDefPtr def);
void virNetworkObjFree(virNetworkObjPtr net);
254
void virNetworkObjListFree(virNetworkObjListPtr vms);
255

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

270
void virNetworkRemoveInactive(virNetworkObjListPtr nets,
271 272
                              const virNetworkObjPtr net);

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

278
char *virNetworkDefFormat(const virNetworkDefPtr def, unsigned int flags);
279

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

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

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

298
int virNetworkSaveXML(const char *configDir,
299 300 301
                      virNetworkDefPtr def,
                      const char *xml);

302
int virNetworkSaveConfig(const char *configDir,
303
                         virNetworkDefPtr def);
304

305 306 307
int virNetworkSaveStatus(const char *statusDir,
                         virNetworkObjPtr net) ATTRIBUTE_RETURN_CHECK;

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

313
int virNetworkLoadAllConfigs(virNetworkObjListPtr nets,
314 315 316
                             const char *configDir,
                             const char *autostartDir);

317
int virNetworkDeleteConfig(const char *configDir,
318
                           const char *autostartDir,
319 320
                           virNetworkObjPtr net);

321
char *virNetworkConfigFile(const char *dir,
322 323
                           const char *name);

324 325 326 327
int virNetworkBridgeInUse(const virNetworkObjListPtr nets,
                          const char *bridge,
                          const char *skipname);

328
char *virNetworkAllocateBridge(const virNetworkObjListPtr nets,
329
                               const char *template);
330

331
int virNetworkSetBridgeName(const virNetworkObjListPtr nets,
332 333
                            virNetworkDefPtr def,
                            int check_collision);
334

335 336
void virNetworkSetBridgeMacAddr(virNetworkDefPtr def);

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

345 346
int virNetworkObjIsDuplicate(virNetworkObjListPtr doms,
                             virNetworkDefPtr def,
347
                             bool check_active);
348

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

M
Matthias Bolte 已提交
352 353
VIR_ENUM_DECL(virNetworkForward)

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

376
#endif /* __NETWORK_CONF_H__ */