opennebula-lease-reservation-v1.patch

Simon Boulet, 05/10/2013 02:34 AM

Download (23.1 KB)

View differences:

include/FixedLeases.h
67 67
     *  @param vid identifier of the VM getting this lease
68 68
     *  @param ip ip of lease requested
69 69
     *  @param mac mac of the lease
70
     *  @param uid of the owner of the VM getting this lease
70 71
     *  @return 0 if success
71 72
     */
72
    int set(int vid, const string&  ip, string&  mac, unsigned int eui64[]);
73
    int set(int vid, const string&  ip, string&  mac, unsigned int eui64[], int uid);
73 74

  
74 75
    /**
75 76
     * Release an used lease, which becomes unused
76
     *   @param ip of the lease in use
77
     *  @param ip of the lease in use
78
     *  @return 0 if success
77 79
     */
78
    void release(const string& ip)
80
    int release(const string& ip)
79 81
    {
80
        unset(ip);
82
        return unset(ip);
81 83
    }
82 84

  
83 85
    /**
......
101 103
    int remove_leases(vector<const Attribute*>& leases, string& error_msg);
102 104

  
103 105
    /**
106
     *  Reserves leases for user or group
107
     *   @param vector_leases vector of VectorAttribute objects. For the
108
     *          moment, the vector can only contain one LEASE.
109
     *   @param error_msg If the action fails, this message contains
110
     *          the reason.
111
     *   @param uid of the user to reserve lease
112
     *   @param gid of the group to reserve lease
113
     *   @return 0 on success
114
     */
115
    int reserve_leases(vector<const Attribute*>& vector_leases, string& error_msg, int uid, int gid);
116

  
117
    /**
104 118
     *  Loads the leases from the DB.
105 119
     */
106 120
    int select(SqlDB * db)
include/Leases.h
72 72
     *  @param eui64 extended unique identifier
73 73
     *  @return 0 if success
74 74
     */
75
     virtual int get(int vid, string& ip, string& mac, unsigned int *eui64) = 0;
76

  
77
     /**
78
      * Ask for a specific lease in the network
79
      *  @param vid identifier of the VM getting this lease
80
      *  @param ip ip of lease requested
81
      *  @param mac mac of the lease
82
      *  @param eui64 extended unique identifier
83
      *  @return 0 if success
84
      */
85
     virtual int set(int vid, const string&  ip, string&  mac, unsigned int *eui64) = 0;
86

  
87
     /**
88
      * Release an used lease, which becomes unused
89
      *   @param ip of the lease in use
90
      */
91
     virtual void release(const string& ip) = 0;
75
    virtual int get(int vid, string& ip, string& mac, unsigned int *eui64) = 0;
76

  
77
    /**
78
     * Ask for a specific lease in the network
79
     *  @param vid identifier of the VM getting this lease
80
     *  @param ip ip of lease requested
81
     *  @param mac mac of the lease
82
     *  @param eui64 extended unique identifier
83
     *  @param uid of the owner of the VM getting this lease
84
     *  @return 0 if success
85
     */
86
    virtual int set(int vid, const string&  ip, string&  mac, unsigned int *eui64, int uid) = 0;
87

  
88
    /**
89
     * Releases a used lease, which becomes unused
90
     *  @param ip of the lease in use
91
     *  @return 0 if success
92
     */
93
    virtual int release(const string& ip) = 0;
92 94

  
93 95
    /**
94 96
     * Adds New leases. (Only implemented for FIXED networks)
......
132 134
     */
133 135
    int free_leases(vector<const Attribute*>& vector_leases, string& error_msg);
134 136

  
137
    /**
138
     * Reserves a Lease for user or group. (Only available for FIXED networks)
139
     *  @param vector_leases vector of VectorAttribute objects. For the
140
     *         moment, the vector can only contain one LEASE.
141
     *  @param error_msg If the action fails, this message contains
142
     *         the reason.
143
     *  @param uid of the user to reserve lease
144
     *  @param gid of the group to reserve lease
145
     *  @return 0 on success
146
     */
147
    virtual int reserve_leases(vector<const Attribute*>& vector_leases, string& error_msg, int uid, int gid) = 0;
148

  
135 149
protected:
136 150
    /**
137 151
     *  The Lease class, it represents a pair of IP and MAC assigned to
......
209 223
        static int prefix6_to_number(const string& prefix, unsigned int ip[]);
210 224

  
211 225
        /**
226
         * Function to check if the Lease is used
227
         * @return true if the lease is used
228
         */
229
        bool is_used() { return used && vid != -1; }
230

  
231
        /**
232
         * Function to check if the Lease is reserved
233
         * @return true if the lease is reserved
234
         */
235
        bool is_reserved() { return used && (uid != -1 || gid != -1); }
236

  
237
        /**
238
         * Function to check if the Lease is held
239
         * @return true if the lease is held
240
         */
241
        bool is_held() { return used && vid == -1; }
242

  
243
        /**
212 244
         * Function to print the Lease object into a string in
213 245
         * XML format
214 246
         *  @param xml the resulting XML string
......
243 275
        int  vid;
244 276

  
245 277
        bool used;
278

  
279
        int uid;
280

  
281
        int gid;
246 282
    };
247 283

  
248 284
    friend class VirtualNetwork;
include/RangedLeases.h
64 64
     *  @param vid identifier of the VM getting this lease
65 65
     *  @param ip ip of lease requested
66 66
     *  @param mac mac of the lease
67
     *  @param uid of the owner of the VM getting this lease
67 68
     *  @return 0 if success
68 69
     */
69
    int set(int vid, const string&  ip, string&  mac, unsigned int eui64[]);
70
    int set(int vid, const string&  ip, string&  mac, unsigned int eui64[], int uid);
70 71

  
71 72
    /**
72 73
     * Release an used lease, which becomes unused
73
     *   @param ip of the lease in use
74
     *  @param ip of the lease in use
75
     *  @return 0 if success
74 76
     */
75
    void release(const string& ip)
77
    int release(const string& ip)
76 78
    {
77
        del(ip);
79
        return del(ip);
78 80
    }
79 81

  
80 82
    /**
......
108 110
    }
109 111

  
110 112
    /**
113
     *  Reserves leases for user or group
114
     *  Only available for FIXED networks.
115
     *    @param vector_leases vector of VectorAttribute objects. For the
116
     *      moment, the vector can only contain one LEASE.
117
     *    @param error_msg If the action fails, this message contains
118
     *      the reason.
119
     *    @param uid of the user to reserve lease
120
     *    @param gid of the group to reserve lease
121
     *    @return 0 on success
122
     */
123
    int reserve_leases(vector<const Attribute*>& vector_leases, string& error_msg, int uid, int gid)
124
    {
125
        error_msg = "Reserving leases is only supported for FIXED networks.";
126
        return -1;
127
    }
128

  
129
    /**
111 130
     *  Loads the leases from the DB.
112 131
     */
113 132
    int select(SqlDB * db)
include/RequestManagerVirtualNetwork.h
30 30
{
31 31
protected:
32 32
    RequestManagerVirtualNetwork(const string& method_name,
33
                                 const string& help)
34
        :Request(method_name,"A:sis",help)
33
                                 const string& help,
34
                                 const string& params = "A:sis")
35
        :Request(method_name,params,help)
35 36
    {
36 37
        Nebula& nd  = Nebula::instance();
37 38
        pool        = nd.get_vnpool();
......
131 132
    }
132 133
};
133 134

  
135
/* ------------------------------------------------------------------------- */
136
/* ------------------------------------------------------------------------- */
137

  
138
class VirtualNetworkReserve : public RequestManagerVirtualNetwork
139
{
140
public:
141
    VirtualNetworkReserve():
142
        RequestManagerVirtualNetwork("VirtualNetworkReserve",
143
                                     "Reserves a virtual network Lease for user or group",
144
                                     "A:sisii") {};
145
    ~VirtualNetworkReserve(){};
146

  
147
    void request_execute(xmlrpc_c::paramList const& _paramList,
148
            RequestAttributes& att) {
149
        uid = xmlrpc_c::value_int(_paramList.getInt(3));
150
        gid = xmlrpc_c::value_int(_paramList.getInt(4));
151
        RequestManagerVirtualNetwork::request_execute(_paramList, att);
152
    }
153

  
154
    int leases_action(VirtualNetwork * vn,
155
                      VirtualNetworkTemplate * tmpl,
156
                      string& error_str)
157
    {
158
        return vn->reserve_leases(tmpl, error_str, uid, gid);
159
    }
160

  
161
private:
162
    int uid;
163
    int gid;
164
};
165

  
134 166

  
135 167
/* -------------------------------------------------------------------------- */
136 168
/* -------------------------------------------------------------------------- */
include/VirtualNetwork.h
107 107
     */
108 108
    int free_leases(VirtualNetworkTemplate* leases, string& error_msg);
109 109

  
110
     /**
111
     * Reserves a Lease for user or group
112
     *  @param leases template in the form LEASES = [IP=XX].
113
     *          The template can only contain one LEASE definition.
114
     *  @param error_msg If the action fails, this message contains
115
     *         the reason.
116
     *  @param uid of the user to reserve lease
117
     *  @param gid of the group to reserve lease
118
     *  @return 0 on success
119
     */
120
    int reserve_leases(VirtualNetworkTemplate* leases, string& error_msg, int uid, int gid);
121

  
110 122
    /**
111 123
     *    Gets a new lease for a specific VM
112 124
     *    @param vid VM identifier
......
129 141
     *    @param _ip the ip of the requested lease
130 142
     *    @param _mac pointer to string for MAC to be stored into
131 143
     *    @param _bridge name of the physical bridge this VN binds to
144
     *    @param _uid of the owner of the VM getting the lease
132 145
     *    @return 0 if success
133 146
     */
134
    int set_lease(int vid, const string& _ip, string& _mac, string& _bridge)
147
    int set_lease(int vid, const string& _ip, string& _mac, string& _bridge, int _uid)
135 148
    {
136 149
        unsigned int eui64[2];
137 150

  
138 151
        _bridge = bridge;
139
        return leases->set(vid, _ip, _mac, eui64);
152
        return leases->set(vid, _ip, _mac, eui64, _uid);
140 153
    };
141 154

  
142 155
    /**
......
144 157
     *    @param _ip IP identifying the lease
145 158
     *    @return 0 if success
146 159
     */
147
    void release_lease(const string& ip)
160
    int release_lease(const string& ip)
148 161
    {
149 162
        return leases->release(ip);
150 163
    };
......
192 205
     *  * BRIDGE: for this virtual network
193 206
     *  @param nic attribute for the VM template
194 207
     *  @param vid of the VM getting the lease
208
     *  @param uid of the owner of the VM getting the lease
195 209
     *  @return 0 on success
196 210
     */
197
    int nic_attribute(VectorAttribute * nic, int vid);
211
    int nic_attribute(VectorAttribute * nic, int vid, int uid);
198 212

  
199 213
private:
200 214

  
src/rm/RequestManager.cc
269 269
    xmlrpc_c::methodPtr vn_rmleases(new VirtualNetworkRemoveLeases());
270 270
    xmlrpc_c::methodPtr vn_hold(new VirtualNetworkHold());
271 271
    xmlrpc_c::methodPtr vn_release(new VirtualNetworkRelease());
272
    xmlrpc_c::methodPtr vn_reserve(new VirtualNetworkReserve());
272 273

  
273 274
    // Update Template Methods
274 275
    xmlrpc_c::methodPtr image_update(new ImageUpdateTemplate());
......
455 456
    RequestManagerRegistry.addMethod("one.vn.rmleases", vn_rmleases);
456 457
    RequestManagerRegistry.addMethod("one.vn.hold", vn_hold);
457 458
    RequestManagerRegistry.addMethod("one.vn.release", vn_release);
459
    RequestManagerRegistry.addMethod("one.vn.reserve", vn_reserve);
458 460
    RequestManagerRegistry.addMethod("one.vn.allocate", vn_allocate);
459 461
    RequestManagerRegistry.addMethod("one.vn.update", vn_update);
460 462
    RequestManagerRegistry.addMethod("one.vn.delete", vn_delete);
src/vnm/FixedLeases.cc
17 17

  
18 18
#include "FixedLeases.h"
19 19
#include "NebulaLog.h"
20
#include "Nebula.h"
20 21

  
21 22
#include <string.h>
22 23

  
......
266 267
        return 0; //Not in the map, not leased
267 268
    }
268 269

  
269
    // Flip used flag to false
270
    it_ip->second->used = false;
270
    if ( !it_ip->second->is_reserved() )
271
    {
272
        // Flip used flag to false
273
        it_ip->second->used = false;
274

  
275
        n_used--;
276
    }
277

  
271 278
    it_ip->second->vid  = -1;
272 279

  
273 280
    // Update the lease
......
295 302

  
296 303
        if (current->second->used == false)
297 304
        {
298
            ostringstream oss;
299

  
300 305
            current->second->used = true;
301 306
            current->second->vid  = vid;
302 307

  
308
            n_used++;
309

  
303 310
            rc = update_lease(current->second);
304 311

  
305 312
            Leases::Lease::mac_to_string(current->second->mac, mac);
......
319 326
/* -------------------------------------------------------------------------- */
320 327
/* -------------------------------------------------------------------------- */
321 328

  
322
int FixedLeases::set(int vid, const string&  ip, string&  mac, unsigned int eui64[])
329
int FixedLeases::set(int vid, const string&  ip, string&  mac, unsigned int eui64[], int uid)
323 330
{
324 331
    map<unsigned int,Lease *>::iterator it;
325 332

  
326 333
    unsigned int    num_ip;
327 334
    int             rc;
328 335

  
336
    User *          user;
337
    UserPool *      upool = Nebula::instance().get_upool();
338

  
329 339
    rc = Leases::Lease::ip_to_number(ip,num_ip);
330 340

  
331 341
    if (rc != 0)
......
339 349
    {
340 350
        return -1;
341 351
    }
342
    else if (it->second->used) //it is in use
352
    else if (it->second->is_used()) //it is in use
343 353
    {
344 354
        return -1;
345 355
    }
346 356

  
347
    it->second->used = true;
357
    if (it->second->is_reserved()) // check reservation
358
    {
359
        user = upool->get(uid, false);
360

  
361
        if ( user == 0 )
362
        {
363
            return -1;
364
        }
365

  
366
        int gid = user->get_gid();
367

  
368
        if (gid != GroupPool::ONEADMIN_ID &&
369
            it->second->uid != uid && it->second->gid != gid)
370
        {
371
            return -1; //it is reserved for another user/group
372
        }
373
    }
374
    else
375
    {
376
        it->second->used = true;
377
        n_used++;
378
    }
379

  
348 380
    it->second->vid  = vid;
349 381

  
350 382
    Leases::Lease::mac_to_string(it->second->mac,mac);
......
371 403
        return -1;
372 404
    }
373 405

  
374
    if( lease->used )
375
    {
376
        n_used++;
377
    }
378
    else
379
    {
380
        n_used--;
381
    }
382

  
383 406
    oss << "UPDATE " << table << " SET body='" << sql_xml << "'"
384 407
        << " WHERE oid=" << oid << " AND ip='" << lease->ip <<"'";
385 408

  
......
463 486

  
464 487
/* -------------------------------------------------------------------------- */
465 488
/* -------------------------------------------------------------------------- */
489

  
490
int FixedLeases::reserve_leases(vector<const Attribute*>&   vector_leases,
491
                                string&                     error_msg,
492
                                int uid, int gid)
493
{
494
    const VectorAttribute * single_attr_lease = 0;
495
    map<unsigned int, Lease *>::iterator  it;
496

  
497
    unsigned int     i_ip;
498
    string           st_ip;
499

  
500
    if ( vector_leases.size() > 0 )
501
    {
502
        single_attr_lease =
503
                dynamic_cast<const VectorAttribute *>(vector_leases[0]);
504
    }
505

  
506
    if( single_attr_lease == 0 )
507
    {
508
        error_msg = "Empty lease description.";
509
        return -1;
510
    }
511

  
512
    st_ip  = single_attr_lease->vector_value("IP");
513

  
514
    if ( Leases::Lease::ip_to_number(st_ip,i_ip) != 0 )
515
    {
516
        error_msg = "Wrong Lease format.";
517
        return -1;
518
    }
519

  
520
    it = leases.find(i_ip);
521

  
522
    if ( it == leases.end() )
523
    {
524
        error_msg = "Lease is not part of the NET.";
525
        return -1;
526
    }
527

  
528
    if ( !it->second->is_used() )
529
    {
530
        it->second->used = true;
531
        it->second->vid = -1;
532

  
533
        n_used++;
534
    }
535

  
536
    it->second->uid = uid;
537
    it->second->gid = gid;
538

  
539
    return update_lease(it->second);
540
}
541

  
542
/* -------------------------------------------------------------------------- */
543
/* -------------------------------------------------------------------------- */
src/vnm/Leases.cc
288 288

  
289 289
    os << "<USED>"<< used  << "</USED>"<<
290 290
          "<VID>" << vid   << "</VID>" <<
291
          "<UID>" << uid   << "</UID>" <<
292
          "<GID>" << gid   << "</GID>" <<
291 293
        "</LEASE>";
292 294

  
293 295
    str = os.str();
......
309 311
            "<MAC_SUFFIX>" << mac[0] << "</MAC_SUFFIX>" <<
310 312
            "<USED>"       << used   << "</USED>"<<
311 313
            "<VID>"        << vid    << "</VID>" <<
314
            "<UID>"        << uid    << "</UID>" <<
315
            "<GID>"        << gid    << "</GID>" <<
312 316
        "</LEASE>";
313 317

  
314 318
    str = os.str();
......
335 339
    rc += xpath(int_used, "/LEASE/USED", 0);
336 340
    rc += xpath(vid, "/LEASE/VID", 0);
337 341

  
342
    xpath(uid, "/LEASE/UID", -1);
343
    xpath(gid, "/LEASE/GID", -1);
344

  
338 345
    used = static_cast<bool>(int_used);
339 346

  
340 347
    if (rc != 0)
......
544 551
        return -1;
545 552
    }
546 553

  
547
    rc = set(-1, ip, mac, tmp);
554
    rc = set(-1, ip, mac, tmp, -1);
548 555

  
549 556
    if ( rc != 0 )
550 557
    {
......
564 571
    const VectorAttribute * single_attr_lease = 0;
565 572
    map<unsigned int,Lease *>::iterator it;
566 573

  
574
    int             rc = 0;
567 575
    unsigned int    i_ip;
568 576
    string          st_ip;
569
    string          mac;
570 577

  
571 578
    if ( vector_leases.size() > 0 )
572 579
    {
......
590 597

  
591 598
    it = leases.find(i_ip);
592 599

  
593
    if ( it == leases.end() || !it->second->used || it->second->vid != -1 )
600
    if ( it == leases.end() )
601
    {
602
        error_msg = "Lease is not part of the NET.";
603
        return -1;
604
    }
605
    else if ( !it->second->is_held() && !it->second->is_reserved() )
594 606
    {
595
        error_msg = "Lease is not on hold.";
607
        error_msg = "Lease is not on hold or reserved.";
596 608
        return -1;
597 609
    }
598 610

  
599
    release(st_ip);
611
    if ( it->second->is_reserved() )
612
    {
613
        rc += reserve_leases(vector_leases, error_msg, -1, -1); //unreserve
614
    }
600 615

  
601
    return 0;
616
    if ( it->second->is_held() )
617
    {
618
        rc += release(st_ip);
619
    }
620

  
621
    return rc;
602 622
}
603 623

  
604 624
/* ************************************************************************** */
src/vnm/RangedLeases.cc
408 408
/* -------------------------------------------------------------------------- */
409 409
/* -------------------------------------------------------------------------- */
410 410

  
411
int RangedLeases::set(int vid, const string&  ip, string&  mac, unsigned int eui64[])
411
int RangedLeases::set(int vid, const string&  ip, string&  mac, unsigned int eui64[], int uid)
412 412
{
413 413
    unsigned int num_ip;
414 414
    unsigned int num_mac[2];
src/vnm/VirtualNetwork.cc
686 686
/* -------------------------------------------------------------------------- */
687 687
/* -------------------------------------------------------------------------- */
688 688

  
689
int VirtualNetwork::nic_attribute(VectorAttribute *nic, int vid)
689
int VirtualNetwork::nic_attribute(VectorAttribute *nic, int vid, int uid)
690 690
{
691 691
    int rc;
692 692

  
......
711 711
    }
712 712
    else
713 713
    {
714
        rc = leases->set(vid,ip,mac, eui64);
714
        rc = leases->set(vid,ip,mac, eui64, uid);
715 715
    }
716 716

  
717 717
    if ( rc != 0 )
......
829 829

  
830 830
/* -------------------------------------------------------------------------- */
831 831
/* -------------------------------------------------------------------------- */
832

  
833
int VirtualNetwork::reserve_leases(VirtualNetworkTemplate * leases_template,
834
                                  string&                   error_msg,
835
                                  int uid, int gid)
836
{
837
    vector<const Attribute *> vector_leases;
838

  
839
    leases_template->get("LEASES", vector_leases);
840

  
841
    return leases->reserve_leases(vector_leases, error_msg, uid, gid);
842
}
843

  
844
/* -------------------------------------------------------------------------- */
845
/* -------------------------------------------------------------------------- */
src/vnm/VirtualNetworkPool.cc
250 250
        return -1;
251 251
    }
252 252

  
253
    int rc = vnet->nic_attribute(nic,vid);
253
    int rc = vnet->nic_attribute(nic,vid,uid);
254 254

  
255 255
    if ( rc == 0 )
256 256
    {
257
-