network_conf.h 9.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * network_conf.h: network XML handling
 *
 * Copyright (C) 2006-2008 Red Hat, Inc.
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * 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 "util.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 158
    unsigned char mac[VIR_MAC_BUFLEN]; /* mac address of bridge device */
    bool mac_specified;
159 160

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

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

168 169
    size_t nips;
    virNetworkIpDefPtr ips; /* ptr to array of IP addresses on this network */
170 171

    virNetworkDNSDefPtr dns; /* ptr to dns related configuration */
172
    virNetDevVPortProfilePtr virtPortProfile;
173 174 175

    size_t nPortGroups;
    virPortGroupDefPtr portGroups;
176
    virNetDevBandwidthPtr bandwidth;
177 178 179 180 181
};

typedef struct _virNetworkObj virNetworkObj;
typedef virNetworkObj *virNetworkObjPtr;
struct _virNetworkObj {
182
    virMutex lock;
183

184
    pid_t dnsmasqPid;
185
    pid_t radvdPid;
186 187 188 189 190 191
    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 */
192
};
193

194 195 196 197 198
typedef struct _virNetworkObjList virNetworkObjList;
typedef virNetworkObjList *virNetworkObjListPtr;
struct _virNetworkObjList {
    unsigned int count;
    virNetworkObjPtr *objs;
199 200 201
};

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

207
virNetworkObjPtr virNetworkFindByUUID(const virNetworkObjListPtr nets,
208
                                      const unsigned char *uuid);
209
virNetworkObjPtr virNetworkFindByName(const virNetworkObjListPtr nets,
210 211 212 213 214
                                      const char *name);


void virNetworkDefFree(virNetworkDefPtr def);
void virNetworkObjFree(virNetworkObjPtr net);
215
void virNetworkObjListFree(virNetworkObjListPtr vms);
216

217
virNetworkObjPtr virNetworkAssignDef(virNetworkObjListPtr nets,
218
                                     const virNetworkDefPtr def);
219
void virNetworkRemoveInactive(virNetworkObjListPtr nets,
220 221
                              const virNetworkObjPtr net);

222 223 224
virNetworkDefPtr virNetworkDefParseString(const char *xmlStr);
virNetworkDefPtr virNetworkDefParseFile(const char *filename);
virNetworkDefPtr virNetworkDefParseNode(xmlDocPtr xml,
225 226
                                        xmlNodePtr root);

227
char *virNetworkDefFormat(const virNetworkDefPtr def);
228

229 230 231 232 233 234 235 236 237 238
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);

239 240 241 242 243 244
virNetworkIpDefPtr
virNetworkDefGetIpByIndex(const virNetworkDefPtr def,
                          int family, size_t n);
int virNetworkIpDefPrefix(const virNetworkIpDefPtr def);
int virNetworkIpDefNetmask(const virNetworkIpDefPtr def,
                           virSocketAddrPtr netmask);
245

246
int virNetworkSaveXML(const char *configDir,
247 248 249
                      virNetworkDefPtr def,
                      const char *xml);

250
int virNetworkSaveConfig(const char *configDir,
251
                         virNetworkDefPtr def);
252

253
virNetworkObjPtr virNetworkLoadConfig(virNetworkObjListPtr nets,
254 255 256 257
                                      const char *configDir,
                                      const char *autostartDir,
                                      const char *file);

258
int virNetworkLoadAllConfigs(virNetworkObjListPtr nets,
259 260 261
                             const char *configDir,
                             const char *autostartDir);

262
int virNetworkDeleteConfig(const char *configDir,
263
                           const char *autostartDir,
264 265
                           virNetworkObjPtr net);

266
char *virNetworkConfigFile(const char *dir,
267 268
                           const char *name);

269 270 271 272
int virNetworkBridgeInUse(const virNetworkObjListPtr nets,
                          const char *bridge,
                          const char *skipname);

273
char *virNetworkAllocateBridge(const virNetworkObjListPtr nets,
274
                               const char *template);
275

276
int virNetworkSetBridgeName(const virNetworkObjListPtr nets,
277 278
                            virNetworkDefPtr def,
                            int check_collision);
279

280 281
void virNetworkSetBridgeMacAddr(virNetworkDefPtr def);

282 283 284 285
int virNetworkObjIsDuplicate(virNetworkObjListPtr doms,
                             virNetworkDefPtr def,
                             unsigned int check_active);

D
Daniel P. Berrange 已提交
286 287 288
void virNetworkObjLock(virNetworkObjPtr obj);
void virNetworkObjUnlock(virNetworkObjPtr obj);

289
#endif /* __NETWORK_CONF_H__ */