Statistics
| Branch: | Tag: | Revision:

one / include / AddressRange.h @ 1772a358

History | View | Annotate | Download (23.4 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems                */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* a copy of the License at                                                   */
7
/*                                                                            */
8
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
9
/*                                                                            */
10
/* Unless required by applicable law or agreed to in writing, software        */
11
/* distributed under the License is distributed on an "AS IS" BASIS,          */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
13
/* See the License for the specific language governing permissions and        */
14
/* limitations under the License.                                             */
15
/* -------------------------------------------------------------------------- */
16

    
17
#ifndef ADDRESS_RANGE_H_
18
#define ADDRESS_RANGE_H_
19

    
20
#include <string>
21
#include <set>
22
#include <vector>
23

    
24
#include "PoolObjectSQL.h"
25
#include "AddressRangePool.h"
26

    
27
using namespace std;
28

    
29
class VectorAttribute;
30

    
31
/**
32
 *  The Lease class represents an address lease from a Virtual Network.
33
 */
34
class AddressRange
35
{
36
public:
37

    
38
    virtual ~AddressRange(){};
39

    
40
    // *************************************************************************
41
    // Address Range types
42
    // *************************************************************************
43

    
44
    /**
45
     *  Type of Addresses defined by this address range
46
     */
47
    enum AddressType
48
    {
49
        NONE  = 0x00000000, /** Undefined Address Type */
50
        ETHER = 0x00000001, /** MAC address type */
51
        IP4   = 0x00000003, /** IP version 4 address */
52
        IP6   = 0x00000005, /** IP version 6 address */
53
        IP4_6 = 0x00000007  /** IP dual stack version 4 & 6 addresses */
54
    };
55

    
56
    /**
57
     *  Return the string representation of an AddressType
58
     *    @param ob the type
59
     *    @return the string
60
     */
61
    static string type_to_str(AddressType ob);
62

    
63
    /**
64
     *  Return the string representation of an AddressType
65
     *    @param ob the type
66
     *    @return the string
67
     */
68
    static AddressType str_to_type(string& str_type);
69

    
70
    // *************************************************************************
71
    // Address Range initialization functions
72
    // *************************************************************************
73

    
74
    /**
75
     *  Init an Address Range based on a vector attribute the following
76
     *  attributes will be parsed (* are optional):
77
     *    - TYPE = ETHER | IP4 | IP6 | IP4_6
78
     *    - SIZE
79
     *    - MAC
80
     *    - IP
81
     *    - ULA_PREFIX
82
     *    - GLOBAL_PREFIX
83
     *
84
     *  The following can be defined to override VNET values:
85
     *    - BRIDGE
86
     *    - VLAN_ID
87
     *    - PHYDEV
88
     *
89
     *  Any value defined in the INHERIT_VNET_ATTR can be defined as well.
90
     *
91
     *  Any value for context can be included in the AR.
92
     *
93
     * Example:
94
     *   - AR = [ TYPE = "ETHER", SIZE = 128, MAC = "00:02:01:02:03:04"]
95
     *   - AR = [ TYPE = "ETHER", SIZE = 128]
96
     *   - AR = [ TYPE = IP4,
97
     *            SIZE = 256,
98
     *            IP   = 10.0.0.0,
99
     *            DNS  = 10.0.0.5]
100
     *   - AR = [ TYPE = "IP6",
101
     *            SIZE = 1024,
102
     *            ULA_PREFIX    = "fd00:0:0:1::",
103
     *            GLOBAL_PREFIX = "2001::"]
104
     *
105
     * NOTE: This function is part of the AddressRange interface. The AR
106
     * implementation may contact an external IPAM to complete or validate
107
     * the AR allocation request.
108
     */
109
    virtual int from_vattr(VectorAttribute * attr, string& error_msg) = 0;
110

    
111
    /**
112
     *  Builds an Address Range from a vector attribute stored in the DB
113
     *    @param vattr the VectorAttribute stored in a ADDRESS_RANGE template
114
     */
115
    int from_vattr_db(VectorAttribute *vattr);
116

    
117
    /**
118
     *  Builds an extended XML representation of the AR to send it back to
119
     *  clients
120
     *    @param oss stream to write the XML
121
     *    @param vm_ids list of VM the user can access VNET usage info from.
122
     *      A vector containing just -1 means all VMs.
123
     *    @param vnet_ids list of VNET the user can access reservation info from.
124
     *      A vector containing just -1 means all VNETs.
125
     *    @param vrs list of VRouter the user can access VNET usage info from.
126
     *      A vector containing just -1 means all VRouters.
127
     */
128
    void to_xml(ostringstream &oss, const vector<int>& vms,
129
        const vector<int>& vnets, const vector<int>& vrs) const;
130

    
131
    /**
132
     *  Same as above but without the LEASES section
133
     */
134
    void to_xml(ostringstream &oss) const;
135

    
136

    
137
    // *************************************************************************
138
    // Address allocation functions
139
    // *************************************************************************
140

    
141
    /**
142
     *  Returns an unused address, which becomes used and fills a NIC attribute
143
     *  with the configuration parameters from the address range.
144
     *    @param ot the type of the object allocating the address
145
     *    @param obid the id of the object
146
     *    @param nic the VM NIC attribute
147
     *    @param inherit attributes to be added to the NIC attribute
148
     *    @return 0 if success
149
     */
150
    int allocate_addr(PoolObjectSQL::ObjectType ot, int obid,
151
        VectorAttribute * nic, const vector<string> &inherit);
152

    
153
    /**
154
     *  Returns the specific address by mac if is not allocated. The NIC attr
155
     *  is filled with the configuration parameters from the address range.
156
     *    @param mac the mac address
157
     *    @param ot the type of the object allocating the address
158
     *    @param obid the id of the object
159
     *    @param nic the VM NIC attribute
160
     *    @param inherit attributes to be added to the NIC attribute
161
     *    @return 0 if success
162
     */
163
    int allocate_by_mac(const string& mac, PoolObjectSQL::ObjectType ot,
164
        int obid, VectorAttribute * nic, const vector<string> &inherit);
165

    
166
    /**
167
     *  Returns the specific address by ip if is not allocated. The NIC attr
168
     *  is filled with the configuration parameters from the address range.
169
     *    @param ot the type of the object allocating the address
170
     *    @param obid the id of the object
171
     *    @param nic the VM NIC attribute
172
     *    @param inherit attributes to be added to the NIC attribute
173
     *    @return 0 if success
174
     */
175
    int allocate_by_ip(const string& ip, PoolObjectSQL::ObjectType ot,
176
        int obid, VectorAttribute * nic, const vector<string> &inherit);
177

    
178
    /**
179
     *  Sets the given ip on hold, the address is associated to a VM of id -1.
180
     *    @param ip the ip to hold
181
     */
182
    int hold_by_ip(const string& ip);
183

    
184
    /**
185
     *  Sets the given mac on hold, the address is associated to a VM of id -1.
186
     *    @param mac the mac to hold
187
     */
188
    int hold_by_mac(const string& mac);
189

    
190
    /**
191
     *  Frees a previous allocated address, referenced by its MAC address
192
     *  @param ot the object type of the owner of the address
193
     *  @param obid the id of the owner of the address
194
     *  @param mac the MAC address in string form
195
     *  @return 0 if the address was freed
196
     */
197
    int free_addr(PoolObjectSQL::ObjectType ot, int obid, const string& mac);
198

    
199
    /**
200
     *  Frees a previous allocated address, referenced by its IP address
201
     *  @param ot the object type of the owner of the address
202
     *  @param obid the id of the owner of the address
203
     *  @param ip the IP address in string form
204
     *  @return 0 if the address was freed
205
     */
206
    int free_addr_by_ip(PoolObjectSQL::ObjectType ot, int id, const string& ip);
207

    
208
    /**
209
     *  Frees all previous allocated address to the given object
210
     *  @param ot the object type of the owner of the address
211
     *  @param obid the id of the owner of the address
212
     *  @return the number of addresses freed
213
     */
214
    int free_addr_by_owner(PoolObjectSQL::ObjectType ot, int obid);
215

    
216
    /**
217
     *  Frees a previous allocated address range, referenced by its MAC address
218
     *  and size
219
     *  @param ot the object type of the owner of the address
220
     *  @param obid the id of the owner of the address
221
     *  @param mac the first MAC address in string form
222
     *  @param rsize the size of the range
223
     *  @return the number of addresses freed
224
     */
225
    int free_addr_by_range(PoolObjectSQL::ObjectType ot, int obid,
226
        const string& mac, unsigned int rsize);
227

    
228
    /**
229
     * Adds the relevant AR definition attributes to the Security Group rule
230
     *
231
     * @param rule rule to modify
232
     */
233
    void process_security_rule(VectorAttribute * rule);
234

    
235
    // *************************************************************************
236
    // Address Reservation
237
    // *************************************************************************
238

    
239
    /**
240
     *  Reserve a given number of addresses from this address range
241
     *    @param vid the id of the VNET making the reservation
242
     *    @param size number of addresses to reserve
243
     *    @param rar a new address range to place the reservation
244
     *    @return 0 on success
245
     */
246
    int reserve_addr(int vid, unsigned int rsize, AddressRange *rar);
247

    
248
    /**
249
     *  Reserve a given number of addresses from this address range
250
     *    @param vid the id of the VNET making the reservation
251
     *    @param size number of addresses to reserve
252
     *    @param rar a new address range to place the reservation
253
     *    @param ip the firs ip in the Reservation
254
     *    @return 0 on success
255
     */
256
    int reserve_addr_by_ip(int vid, unsigned int rsize, const string& ip,
257
        AddressRange *rar);
258

    
259
    /**
260
     *  Reserve a given number of addresses from this address range
261
     *    @param vid the id of the VNET making the reservation
262
     *    @param size number of addresses to reserve
263
     *    @param rar a new address range to place the reservation
264
     *    @param mac the firs mac in the Reservation
265
     *    @return 0 on success
266
     */
267
    int reserve_addr_by_mac(int vid, unsigned int rsize, const string& mac,
268
        AddressRange *rar);
269

    
270
    // *************************************************************************
271
    // Helpers
272
    // *************************************************************************
273

    
274
    /**
275
     *  Return the id for this address range
276
     */
277
    unsigned int ar_id() const
278
    {
279
        return id;
280
    }
281

    
282
    /**
283
     *  Return the number of used addresses
284
     */
285
    unsigned int get_used_addr() const
286
    {
287
        return allocated.size();
288
    }
289

    
290
    /**
291
     *  Return the number of free addresses
292
     */
293
    unsigned int get_free_addr() const
294
    {
295
        return size - allocated.size();
296
    }
297

    
298
    /**
299
     *  Return the total number of addresses
300
     */
301
    unsigned int get_size() const
302
    {
303
        return size;
304
    }
305

    
306
    /**
307
     *  Returns the string value of an Address Range Attribute
308
     *    @param name of the attribute
309
     *    @return the value of the attribute if found, empty otherwise
310
     */
311
    string get_attribute(const char *name) const
312
    {
313
        return attr->vector_value(name);
314
    }
315

    
316
    /**
317
     *  Returns the int value of an Address Range Attribute
318
     *    @param name of the attribute
319
     *    @param value of the attribute
320
     *    @return 0 on success
321
     */
322
    int get_attribute(const char *name, int& value) const
323
    {
324
        return attr->vector_value(name, value);
325
    }
326

    
327
    /**
328
     *  Updates the Address Range with the attributes provided. The following
329
     *  CANNOT be updated: TYPE, SIZE, IP, MAC (plus the internal AR_ID and
330
     *  ALLOCATED)
331
     *    @param vup the new vector attributes for the address range
332
     *    @param error_msg If the action fails, this message contains
333
     *    the reason.
334
     *    @return 0 on success
335
     */
336
    int update_attributes(
337
            VectorAttribute *   vup,
338
            bool                keep_restricted,
339
            string&             error_msg);
340

    
341
    /**
342
     *  Helper function to initialize restricte attributes of an AddressRange
343
     */
344
    static void set_restricted_attributes(vector<const SingleAttribute *>& ras);
345

    
346
    /**
347
     *  Get the security groups for this AR.
348
     *    @return a reference to the security group set
349
     */
350
    const set<int>& get_security_groups() const
351
    {
352
        return security_groups;
353
    }
354

    
355
    /*
356
     *  add_ar from AddressRangePool needs to access the internal representation
357
     *  of the AR to include it in the ARPool template.
358
     */
359
    friend int AddressRangePool::add_ar(AddressRange * ar);
360

    
361
protected:
362
    /**
363
     *  Base constructor it cannot be called directly but from the
364
     *  AddressRange factory constructor.
365
     */
366
    AddressRange(unsigned int _id):id(_id){};
367

    
368
    /**
369
     * Builds the AddressRange from its vector attribute representation
370
     */
371
    int from_attr(VectorAttribute * attr, string& error_msg);
372

    
373
    /**
374
     *  Builds an address request representation in XML form:
375
     *  <ADDRESS>
376
     *    <IP>
377
     *    <MAC>
378
     *    <IP6_ULA>
379
     *    <IP6_GLOBAL>
380
     *    <SIZE>
381
     */
382
    void addr_to_xml(unsigned int index, unsigned int size,
383
            ostringstream& oss) const;
384

    
385
    /* ---------------------------------------------------------------------- */
386
    /* Implementation specific address management interface                   */
387
    /* ---------------------------------------------------------------------- */
388
    /**
389
     *  Sets the given range of addresses (by index) as used
390
     *    @param index the first address to set as used
391
     *    @param sz number of addresses to set
392
     *    @param msg describing the error if any
393
     *
394
     *    @return 0 if success
395
     */
396
    virtual int allocate_addr(unsigned int index, unsigned int sz, string& msg) = 0;
397
    /**
398
     *  Gets a range of free addresses
399
     *    @param index the first address in the range
400
     *    @param size number of addresses requested in the range
401
     *    @param msg describing the error if any
402
     *
403
     *    @return 0 if success
404
     */
405
    virtual int get_addr(unsigned int& index, unsigned int sz, string& msg) = 0;
406

    
407
    /**
408
     *  Sets the given address (by index) as free
409
     *    @param index of the address
410
     *    @param msg describing the error if any
411
     *
412
     *    @return 0 if success
413
     */
414
    virtual int free_addr(unsigned int index, string& msg) = 0;
415

    
416
    /* ---------------------------------------------------------------------- */
417
    /* Allocated addresses                                                    */
418
    /* ---------------------------------------------------------------------- */
419
    /**
420
     *  Map to store the allocated address indexed by the address index relative
421
     *  to the mac/ip values. It contains also the type and id of the object
422
     *  owning the address.
423
     *
424
     *              +--------------------+--------------------+
425
     *  index ----> | ObjectType(32bits) | Object ID (32bits) |
426
     *              +--------------------+--------------------+
427
     *
428
     *  Address = First Address + index
429
     */
430
    map<unsigned int, long long> allocated;
431

    
432
private:
433
    /* ---------------------------------------------------------------------- */
434
    /* String to binary conversion functions for different address types      */
435
    /* ---------------------------------------------------------------------- */
436
    /**
437
     *  MAC to binary (48 bits)
438
     *    @param mac in string form 00:02:01:02:03:04
439
     *    @return 0 on success
440
     */
441
    int mac_to_i(string mac, unsigned int i_mac[]) const;
442

    
443
    /**
444
     *  MAC to string
445
     *    @param mac in array form
446
     */
447
    string mac_to_s(const unsigned int mac[]) const;
448

    
449
    /**
450
     *  IP version 4 to binary (32 bits)
451
     *    @param ip in string form 192.168.0.2
452
     *    @return 0 on success
453
     */
454
    int ip_to_i(const string& _ip, unsigned int& i_ip) const;
455

    
456
    /**
457
     * IP version 4 to dot notation
458
     *
459
     * @param i_ip Numeric (32 bits) IP
460
     * @return dot notation
461
     */
462
    string ip_to_s(unsigned int i_ip) const;
463

    
464
    /**
465
     *  IPv6 64bits prefix conversion
466
     *    @param prefix in string form 2a00:1bc0:b001:A::
467
     *    @return 0 on success
468
     */
469
    int prefix6_to_i(const string& prefix, unsigned int ip[]) const;
470

    
471
    /**
472
     * IPv6 to string
473
     * @param prefix Numeric IPv6 prefix
474
     * @param mac Numeric (48 bits) mac address
475
     * @param ip6_s Will contain the resulting IPv6 string
476
     * @return 0 on success
477
     */
478
    int ip6_to_s(const unsigned int prefix[], const unsigned int mac[],
479
        string& ip6_s) const;
480

    
481
    /* ---------------------------------------------------------------------- */
482
    /* NIC setup functions                                                    */
483
    /* ---------------------------------------------------------------------- */
484
    /**
485
     *  Check if the given MAC is valid for this address range by verifying:
486
     *    - Correct : notation
487
     *    - Part of the AR
488
     *
489
     *    @param index of the MAC in the AR
490
     *    @param mac_s string representation of the MAC in : notation
491
     *    @param check_free apart from previous checks
492
     *
493
     *    @return true if the MAC is valid
494
     */
495
    bool is_valid_mac(unsigned int& index, const string& mac_s, bool check_free);
496

    
497
    /**
498
     *  Check if the given IP is valid for this address range by verifying:
499
     *    - AR is of type IP4 or IP4_6
500
     *    - Correct . notation
501
     *    - Part of the AR
502
     *
503
     *    @param index of the IP in the AR
504
     *    @param ip_s string representation of the IP in . notation
505
     *    @param check_free apart from previous checks
506
     *
507
     *    @return true if the IP is valid
508
     */
509
    bool is_valid_ip(unsigned int& index, const string& ip_s, bool check_free);
510

    
511
    /**
512
     *  Writes MAC address to the given NIC attribute
513
     *    @param addr_index internal index for the lease
514
     *    @param nic attribute of a VMTemplate
515
     */
516
    void set_mac(unsigned int addr_index, VectorAttribute * nic) const;
517

    
518
    /**
519
     *  Writes IP address to the given NIC attribute
520
     *    @param addr_index internal index for the lease
521
     *    @param nic attribute of a VMTemplate
522
     */
523
    void set_ip(unsigned int addr_index, VectorAttribute * nic) const;
524

    
525
    /**
526
     *  Writes IPv6 address to the given NIC attribute
527
     *    @param addr_index internal index for the lease
528
     *    @param nic attribute of a VMTemplate
529
     */
530
    void set_ip6(unsigned int addr_index, VectorAttribute * nic) const;
531

    
532
    /**
533
     *  Writes VNET configuration attributes to the given NIC attribute. It
534
     *  includes: BRIDGE, VLAN_ID, PHYDEV and INHERIT_VNET_ATTR in oned.conf
535
     *    @param addr_index internal index for the lease
536
     *    @param nic attribute of a VMTemplate
537
     */
538
    void set_vnet(VectorAttribute *nic, const vector<string> &inherit) const;
539

    
540
    /* ---------------------------------------------------------------------- */
541
    /* Address index map helper functions                                     */
542
    /* ---------------------------------------------------------------------- */
543
    /**
544
     *  This function generates a string representation of the in-memory allocated
545
     *  addresses. It'll be stored along side the AR vector attribute in the
546
     *  ADDRESS_RANGE template.
547
     */
548
    void allocated_to_attr();
549

    
550
    /**
551
     *  Generates a memory map for the addresses.
552
     *    @param allocated_s the string representation of the allocated addresses
553
     *    generated by allocated_to_attr()
554
     *    @return 0 on success
555
     */
556
    int  attr_to_allocated(const string& allocated_s);
557

    
558
    /**
559
     *  Adds a new allocated address to the map. Updates the ALLOCATED attribute
560
     */
561
    void set_allocated_addr(PoolObjectSQL::ObjectType ot, int obid,
562
        unsigned int addr_index);
563

    
564
    /**
565
     *  Sets the address lease as used and fills a NIC attribute with the
566
     *  configuration parameters from the address range.
567
     *    @param index of the lease in the address range
568
     *    @param ot the type of the object allocating the address
569
     *    @param obid the id of the object
570
     *    @param nic the VM NIC attribute
571
     *    @param inherit attributes to be added to the NIC attribute
572
     *    @return 0 if success
573
     */
574
    void allocate_by_index(unsigned int index,
575
        PoolObjectSQL::ObjectType ot,
576
        int                       obid,
577
        VectorAttribute*          nic,
578
        const vector<string>&     inherit);
579

    
580
    /**
581
     *  Frees an address from the map. Updates the ALLOCATED attribute
582
     */
583
    int free_allocated_addr(PoolObjectSQL::ObjectType ot, int obid,
584
        unsigned int addr_index);
585

    
586
    /**
587
     *  Reserve a set of addresses from an starting one
588
     *    @param vid the id of the VNET making the reservation
589
     *    @param rsize number of addresses to reserve
590
     *    @param sindex the first index to start the reservation
591
     *    @param rar a new address range to place the reservation
592
     *    @return 0 on success
593
     */
594
    int reserve_addr_by_index(int vid, unsigned int rsize, unsigned int sindex,
595
        AddressRange *rar);
596

    
597
    /* ---------------------------------------------------------------------- */
598
    /* Restricted Attributes functions                                        */
599
    /* ---------------------------------------------------------------------- */
600
    bool check(string& rs_attr) const;
601

    
602
    /**
603
     * Deletes all restricted attributes
604
     */
605
    void remove_restricted(VectorAttribute* va);
606

    
607
    /**
608
     * Deletes all the attributes, except the restricted ones
609
     */
610
    void remove_all_except_restricted(VectorAttribute* va);
611

    
612
    /* ---------------------------------------------------------------------- */
613
    /* Address Range data                                                     */
614
    /* ---------------------------------------------------------------------- */
615
    /**
616
     *  The type of addresses defined in the range
617
     */
618
    AddressType type;
619

    
620
    /**
621
     *  ID for this range, unique within the Virtual Network
622
     */
623
    unsigned int id;
624

    
625
    /**
626
     *  Number of addresses in the range
627
     */
628
    unsigned int size;
629

    
630
    /**
631
     *  First IP4 in the range
632
     */
633
    unsigned int ip;
634

    
635
    /**
636
     *  First MAC in the range
637
     */
638
    unsigned int mac[2];
639

    
640
    /**
641
     *  Binary representation of the IPv6 address global unicast prefix
642
     */
643
    unsigned int global6[2];
644

    
645
    /**
646
     *  Binary representation of the IPv6 address site unicast prefix
647
     */
648
    unsigned int ula6[2];
649

    
650
    /**
651
     *  Security Group IDs for this Address Range
652
     */
653
    set<int> security_groups;
654

    
655
    /**
656
     *  The name of the IPAM driver (internal for OpenNebula built-in)
657
     */
658
    string ipam_mad;
659

    
660
    /**
661
     *  The Address Range attributes as a Template VectorAttribute. This is
662
     *  used to generate XML or a template representation of the AR.
663
     */
664
    VectorAttribute * attr;
665

    
666
    /* ---------------------------------------------------------------------- */
667
    /* Restricted Attributes                                                  */
668
    /* ---------------------------------------------------------------------- */
669
    /**
670
     *  TRUE if restricted attributes have been defined for Address Ranges
671
     */
672
    static bool restricted_set;
673

    
674
    /**
675
     *  The restricted attributes from oned.conf
676
     */
677
    static set<string> restricted_attributes;
678

    
679
    /**
680
     *  Attributes to be process for Security Group rules
681
     */
682
    const static char * SG_RULE_ATTRIBUTES[];
683

    
684
    const static int  NUM_SG_RULE_ATTRIBUTES;
685
};
686

    
687
#endif