network_conf.h 12.8 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 68
typedef struct _virNetworkDHCPHostDef virNetworkDHCPHostDef;
typedef virNetworkDHCPHostDef *virNetworkDHCPHostDefPtr;
struct _virNetworkDHCPHostDef {
    char *mac;
69
    char *id;
70
    char *name;
71
    virSocketAddr ip;
72 73
};

74 75 76
typedef struct _virNetworkDNSTxtDef virNetworkDNSTxtDef;
typedef virNetworkDNSTxtDef *virNetworkDNSTxtDefPtr;
struct _virNetworkDNSTxtDef {
77 78 79 80
    char *name;
    char *value;
};

81 82 83
typedef struct _virNetworkDNSSrvDef virNetworkDNSSrvDef;
typedef virNetworkDNSSrvDef *virNetworkDNSSrvDefPtr;
struct _virNetworkDNSSrvDef {
84 85 86 87
    char *domain;
    char *service;
    char *protocol;
    char *target;
88 89 90
    unsigned int port;
    unsigned int priority;
    unsigned int weight;
91 92
};

93 94 95
typedef struct _virNetworkDNSHostDef virNetworkDNSHostDef;
typedef virNetworkDNSHostDef *virNetworkDNSHostDefPtr;
struct _virNetworkDNSHostDef {
96 97 98
    virSocketAddr ip;
    int nnames;
    char **names;
99
};
100

101 102
typedef struct _virNetworkDNSDef virNetworkDNSDef;
typedef virNetworkDNSDef *virNetworkDNSDefPtr;
103
struct _virNetworkDNSDef {
104 105 106 107 108 109
    size_t ntxts;
    virNetworkDNSTxtDefPtr txts;
    size_t nhosts;
    virNetworkDNSHostDefPtr hosts;
    size_t nsrvs;
    virNetworkDNSSrvDefPtr srvs;
110
};
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
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 */

127
    size_t nranges;             /* Zero or more dhcp ranges */
128
    virSocketAddrRangePtr ranges;
129

130
    size_t nhosts;              /* Zero or more dhcp hosts */
131 132 133 134 135 136 137
    virNetworkDHCPHostDefPtr hosts;

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

138 139 140
typedef struct _virNetworkForwardIfDef virNetworkForwardIfDef;
typedef virNetworkForwardIfDef *virNetworkForwardIfDefPtr;
struct _virNetworkForwardIfDef {
141 142 143 144 145 146 147
    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? */
148 149
};

150 151 152 153
typedef struct _virNetworkForwardPfDef virNetworkForwardPfDef;
typedef virNetworkForwardPfDef *virNetworkForwardPfDefPtr;
struct _virNetworkForwardPfDef {
    char *dev;      /* name of device */
154
    int connections; /* how many guest interfaces are connected to this device? */
155 156
};

157 158 159 160 161 162 163 164 165 166 167 168 169 170
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;
171

172
    /* ranges for NAT */
173 174
    virSocketAddrRange addr;
    virPortRange port;
175 176
};

177 178 179 180 181
typedef struct _virPortGroupDef virPortGroupDef;
typedef virPortGroupDef *virPortGroupDefPtr;
struct _virPortGroupDef {
    char *name;
    bool isDefault;
182
    virNetDevVPortProfilePtr virtPortProfile;
183
    virNetDevBandwidthPtr bandwidth;
184
    virNetDevVlan vlan;
185 186
};

187 188 189 190
typedef struct _virNetworkDef virNetworkDef;
typedef virNetworkDef *virNetworkDefPtr;
struct _virNetworkDef {
    unsigned char uuid[VIR_UUID_BUFLEN];
M
Matthias Bolte 已提交
191
    bool uuid_specified;
192
    char *name;
193
    int   connections; /* # of guest interfaces connected to this network */
194 195

    char *bridge;       /* Name of bridge device */
196
    char *domain;
197
    unsigned long delay;   /* Bridge forward delay (ms) */
198
    bool stp; /* Spanning tree protocol */
199
    virMacAddr mac; /* mac address of bridge device */
200
    bool mac_specified;
201

202 203 204 205 206
    /* specified if ip6tables rules added
     * when no ipv6 gateway addresses specified.
     */
    bool ipv6nogw;

207
    virNetworkForwardDef forward;
208

209 210
    size_t nips;
    virNetworkIpDefPtr ips; /* ptr to array of IP addresses on this network */
211

212
    virNetworkDNSDef dns;   /* dns related configuration */
213
    virNetDevVPortProfilePtr virtPortProfile;
214 215 216

    size_t nPortGroups;
    virPortGroupDefPtr portGroups;
217
    virNetDevBandwidthPtr bandwidth;
218
    virNetDevVlan vlan;
219 220 221 222 223
};

typedef struct _virNetworkObj virNetworkObj;
typedef virNetworkObj *virNetworkObjPtr;
struct _virNetworkObj {
224
    virMutex lock;
225

226
    pid_t dnsmasqPid;
227
    pid_t radvdPid;
228 229 230 231 232 233
    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 */
234 235 236

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

239 240 241 242 243
typedef struct _virNetworkObjList virNetworkObjList;
typedef virNetworkObjList *virNetworkObjListPtr;
struct _virNetworkObjList {
    unsigned int count;
    virNetworkObjPtr *objs;
244 245 246
};

static inline int
D
Daniel P. Berrange 已提交
247
virNetworkObjIsActive(const virNetworkObjPtr net)
248 249 250 251
{
    return net->active;
}

252
virNetworkObjPtr virNetworkFindByUUID(const virNetworkObjListPtr nets,
253
                                      const unsigned char *uuid);
254
virNetworkObjPtr virNetworkFindByName(const virNetworkObjListPtr nets,
255 256 257 258 259
                                      const char *name);


void virNetworkDefFree(virNetworkDefPtr def);
void virNetworkObjFree(virNetworkObjPtr net);
260
void virNetworkObjListFree(virNetworkObjListPtr vms);
261

262
virNetworkObjPtr virNetworkAssignDef(virNetworkObjListPtr nets,
263 264 265 266 267 268
                                     const virNetworkDefPtr def,
                                     bool live);
int virNetworkObjAssignDef(virNetworkObjPtr network,
                           const virNetworkDefPtr def,
                           bool live);
int virNetworkObjSetDefTransient(virNetworkObjPtr network, bool live);
269
void virNetworkObjUnsetDefTransient(virNetworkObjPtr network);
270 271 272 273 274 275
virNetworkDefPtr virNetworkObjGetPersistentDef(virNetworkObjPtr network);
int virNetworkObjReplacePersistentDef(virNetworkObjPtr network,
                                      virNetworkDefPtr def);
virNetworkDefPtr virNetworkDefCopy(virNetworkDefPtr def, unsigned int flags);
int virNetworkConfigChangeSetup(virNetworkObjPtr dom, unsigned int flags);

276
void virNetworkRemoveInactive(virNetworkObjListPtr nets,
277 278
                              const virNetworkObjPtr net);

279 280 281
virNetworkDefPtr virNetworkDefParseString(const char *xmlStr);
virNetworkDefPtr virNetworkDefParseFile(const char *filename);
virNetworkDefPtr virNetworkDefParseNode(xmlDocPtr xml,
282
                                        xmlNodePtr root);
283 284
int virNetworkObjUpdateParseFile(const char *filename,
                                 virNetworkObjPtr net);
285

286
char *virNetworkDefFormat(const virNetworkDefPtr def, unsigned int flags);
287

288 289 290
static inline const char *
virNetworkDefForwardIf(const virNetworkDefPtr def, size_t n)
{
291 292 293
    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);
294 295 296 297 298
}

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

299 300 301 302 303 304
virNetworkIpDefPtr
virNetworkDefGetIpByIndex(const virNetworkDefPtr def,
                          int family, size_t n);
int virNetworkIpDefPrefix(const virNetworkIpDefPtr def);
int virNetworkIpDefNetmask(const virNetworkIpDefPtr def,
                           virSocketAddrPtr netmask);
305

306
int virNetworkSaveXML(const char *configDir,
307 308 309
                      virNetworkDefPtr def,
                      const char *xml);

310
int virNetworkSaveConfig(const char *configDir,
311
                         virNetworkDefPtr def);
312

313 314 315
int virNetworkSaveStatus(const char *statusDir,
                         virNetworkObjPtr net) ATTRIBUTE_RETURN_CHECK;

316
virNetworkObjPtr virNetworkLoadConfig(virNetworkObjListPtr nets,
317 318 319 320
                                      const char *configDir,
                                      const char *autostartDir,
                                      const char *file);

321
int virNetworkLoadAllConfigs(virNetworkObjListPtr nets,
322 323 324
                             const char *configDir,
                             const char *autostartDir);

325
int virNetworkDeleteConfig(const char *configDir,
326
                           const char *autostartDir,
327 328
                           virNetworkObjPtr net);

329
char *virNetworkConfigFile(const char *dir,
330 331
                           const char *name);

332 333 334 335
int virNetworkBridgeInUse(const virNetworkObjListPtr nets,
                          const char *bridge,
                          const char *skipname);

336
char *virNetworkAllocateBridge(const virNetworkObjListPtr nets,
337
                               const char *template);
338

339
int virNetworkSetBridgeName(const virNetworkObjListPtr nets,
340 341
                            virNetworkDefPtr def,
                            int check_collision);
342

343 344
void virNetworkSetBridgeMacAddr(virNetworkDefPtr def);

345 346 347 348 349 350 351 352
int
virNetworkObjUpdate(virNetworkObjPtr obj,
                    unsigned int command, /* virNetworkUpdateCommand */
                    unsigned int section, /* virNetworkUpdateSection */
                    int parentIndex,
                    const char *xml,
                    unsigned int flags);  /* virNetworkUpdateFlags */

353 354
int virNetworkObjIsDuplicate(virNetworkObjListPtr doms,
                             virNetworkDefPtr def,
355
                             bool check_active);
356

D
Daniel P. Berrange 已提交
357 358 359
void virNetworkObjLock(virNetworkObjPtr obj);
void virNetworkObjUnlock(virNetworkObjPtr obj);

M
Matthias Bolte 已提交
360 361
VIR_ENUM_DECL(virNetworkForward)

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
# 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);

384
#endif /* __NETWORK_CONF_H__ */