network_conf.h 9.3 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
O
Osier Yang 已提交
18 19
 * License along with this library;  If not, see
 * <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 "virmacaddr.h"
39 40 41 42 43

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

    VIR_NETWORK_FORWARD_LAST,
};

typedef struct _virNetworkDHCPRangeDef virNetworkDHCPRangeDef;
typedef virNetworkDHCPRangeDef *virNetworkDHCPRangeDefPtr;
struct _virNetworkDHCPRangeDef {
55 56
    virSocketAddr start;
    virSocketAddr end;
57 58
};

59 60 61 62 63
typedef struct _virNetworkDHCPHostDef virNetworkDHCPHostDef;
typedef virNetworkDHCPHostDef *virNetworkDHCPHostDefPtr;
struct _virNetworkDHCPHostDef {
    char *mac;
    char *name;
64
    virSocketAddr ip;
65 66
};

67 68 69 70 71 72 73
typedef struct _virNetworkDNSTxtRecordsDef virNetworkDNSTxtRecordsDef;
typedef virNetworkDNSTxtRecordsDef *virNetworkDNSTxtRecordsDefPtr;
struct _virNetworkDNSTxtRecordsDef {
    char *name;
    char *value;
};

74 75 76 77 78 79 80 81 82 83 84 85
typedef struct _virNetworkDNSSrvRecordsDef virNetworkDNSSrvRecordsDef;
typedef virNetworkDNSSrvRecordsDef *virNetworkDNSSrvRecordsDefPtr;
struct _virNetworkDNSSrvRecordsDef {
    char *domain;
    char *service;
    char *protocol;
    char *target;
    int port;
    int priority;
    int weight;
};

86
struct _virNetworkDNSHostsDef {
87 88 89
    virSocketAddr ip;
    int nnames;
    char **names;
90
};
91

92
typedef struct _virNetworkDNSHostsDef *virNetworkDNSHostsDefPtr;
93

94
struct _virNetworkDNSDef {
95 96
    unsigned int ntxtrecords;
    virNetworkDNSTxtRecordsDefPtr txtrecords;
97 98
    unsigned int nhosts;
    virNetworkDNSHostsDefPtr hosts;
99 100
    unsigned int nsrvrecords;
    virNetworkDNSSrvRecordsDefPtr srvrecords;
101
};
102

103
typedef struct _virNetworkDNSDef *virNetworkDNSDefPtr;
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
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;
   };

131 132 133 134 135 136 137 138 139 140 141 142
typedef struct _virNetworkForwardIfDef virNetworkForwardIfDef;
typedef virNetworkForwardIfDef *virNetworkForwardIfDefPtr;
struct _virNetworkForwardIfDef {
    char *dev;      /* name of device */
    int usageCount; /* how many guest interfaces are bound to this device? */
};

typedef struct _virPortGroupDef virPortGroupDef;
typedef virPortGroupDef *virPortGroupDefPtr;
struct _virPortGroupDef {
    char *name;
    bool isDefault;
143
    virNetDevVPortProfilePtr virtPortProfile;
144
    virNetDevBandwidthPtr bandwidth;
145 146
};

147 148 149 150 151 152 153
typedef struct _virNetworkDef virNetworkDef;
typedef virNetworkDef *virNetworkDefPtr;
struct _virNetworkDef {
    unsigned char uuid[VIR_UUID_BUFLEN];
    char *name;

    char *bridge;       /* Name of bridge device */
154
    char *domain;
155
    unsigned long delay;   /* Bridge forward delay (ms) */
156
    unsigned int stp :1; /* Spanning tree protocol */
157
    virMacAddr mac; /* mac address of bridge device */
158
    bool mac_specified;
159 160

    int forwardType;    /* One of virNetworkForwardType constants */
161 162 163 164

    /* If there are multiple forward devices (i.e. a pool of
     * interfaces), they will be listed here.
     */
165 166 167
    size_t nForwardPfs;
    virNetworkForwardIfDefPtr forwardPfs;

168 169
    size_t nForwardIfs;
    virNetworkForwardIfDefPtr forwardIfs;
170

171 172
    size_t nips;
    virNetworkIpDefPtr ips; /* ptr to array of IP addresses on this network */
173 174

    virNetworkDNSDefPtr dns; /* ptr to dns related configuration */
175
    virNetDevVPortProfilePtr virtPortProfile;
176 177 178

    size_t nPortGroups;
    virPortGroupDefPtr portGroups;
179
    virNetDevBandwidthPtr bandwidth;
180 181 182 183 184
};

typedef struct _virNetworkObj virNetworkObj;
typedef virNetworkObj *virNetworkObjPtr;
struct _virNetworkObj {
185
    virMutex lock;
186

187
    pid_t dnsmasqPid;
188
    pid_t radvdPid;
189 190 191 192 193 194
    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 */
195
};
196

197 198 199 200 201
typedef struct _virNetworkObjList virNetworkObjList;
typedef virNetworkObjList *virNetworkObjListPtr;
struct _virNetworkObjList {
    unsigned int count;
    virNetworkObjPtr *objs;
202 203 204
};

static inline int
D
Daniel P. Berrange 已提交
205
virNetworkObjIsActive(const virNetworkObjPtr net)
206 207 208 209
{
    return net->active;
}

210
virNetworkObjPtr virNetworkFindByUUID(const virNetworkObjListPtr nets,
211
                                      const unsigned char *uuid);
212
virNetworkObjPtr virNetworkFindByName(const virNetworkObjListPtr nets,
213 214 215 216 217
                                      const char *name);


void virNetworkDefFree(virNetworkDefPtr def);
void virNetworkObjFree(virNetworkObjPtr net);
218
void virNetworkObjListFree(virNetworkObjListPtr vms);
219

220
virNetworkObjPtr virNetworkAssignDef(virNetworkObjListPtr nets,
221
                                     const virNetworkDefPtr def);
222
void virNetworkRemoveInactive(virNetworkObjListPtr nets,
223 224
                              const virNetworkObjPtr net);

225 226 227
virNetworkDefPtr virNetworkDefParseString(const char *xmlStr);
virNetworkDefPtr virNetworkDefParseFile(const char *filename);
virNetworkDefPtr virNetworkDefParseNode(xmlDocPtr xml,
228 229
                                        xmlNodePtr root);

230
char *virNetworkDefFormat(const virNetworkDefPtr def, unsigned int flags);
231

232 233 234 235 236 237 238 239 240 241
static inline const char *
virNetworkDefForwardIf(const virNetworkDefPtr def, size_t n)
{
    return ((def->forwardIfs && (def->nForwardIfs > n))
            ? def->forwardIfs[n].dev : NULL);
}

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

242 243 244 245 246 247
virNetworkIpDefPtr
virNetworkDefGetIpByIndex(const virNetworkDefPtr def,
                          int family, size_t n);
int virNetworkIpDefPrefix(const virNetworkIpDefPtr def);
int virNetworkIpDefNetmask(const virNetworkIpDefPtr def,
                           virSocketAddrPtr netmask);
248

249
int virNetworkSaveXML(const char *configDir,
250 251 252
                      virNetworkDefPtr def,
                      const char *xml);

253
int virNetworkSaveConfig(const char *configDir,
254
                         virNetworkDefPtr def);
255

256
virNetworkObjPtr virNetworkLoadConfig(virNetworkObjListPtr nets,
257 258 259 260
                                      const char *configDir,
                                      const char *autostartDir,
                                      const char *file);

261
int virNetworkLoadAllConfigs(virNetworkObjListPtr nets,
262 263 264
                             const char *configDir,
                             const char *autostartDir);

265
int virNetworkDeleteConfig(const char *configDir,
266
                           const char *autostartDir,
267 268
                           virNetworkObjPtr net);

269
char *virNetworkConfigFile(const char *dir,
270 271
                           const char *name);

272 273 274 275
int virNetworkBridgeInUse(const virNetworkObjListPtr nets,
                          const char *bridge,
                          const char *skipname);

276
char *virNetworkAllocateBridge(const virNetworkObjListPtr nets,
277
                               const char *template);
278

279
int virNetworkSetBridgeName(const virNetworkObjListPtr nets,
280 281
                            virNetworkDefPtr def,
                            int check_collision);
282

283 284
void virNetworkSetBridgeMacAddr(virNetworkDefPtr def);

285 286 287 288
int virNetworkObjIsDuplicate(virNetworkObjListPtr doms,
                             virNetworkDefPtr def,
                             unsigned int check_active);

D
Daniel P. Berrange 已提交
289 290 291
void virNetworkObjLock(virNetworkObjPtr obj);
void virNetworkObjUnlock(virNetworkObjPtr obj);

292
#endif /* __NETWORK_CONF_H__ */