This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
plugin.go
893 lines (736 loc) · 21 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
//
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"bufio"
"bytes"
"encoding/gob"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"sync"
"time"
"github.com/01org/ciao/uuid"
"github.com/boltdb/bolt"
"github.com/docker/libnetwork/drivers/remote/api"
ipamapi "github.com/docker/libnetwork/ipams/remote/api"
"github.com/golang/glog"
"github.com/gorilla/mux"
)
type epVal struct {
IP string
vhostuserPort string //The dpdk vhost user port
vppInterface string
}
type nwVal struct {
Bridge string //The bridge on which the ports will be created
Gateway net.IPNet
}
var intfCounter int
var epMap struct {
sync.Mutex
m map[string]*epVal
}
var nwMap struct {
sync.Mutex
m map[string]*nwVal
}
var brMap struct {
sync.Mutex
brCount int
m map[string]int
}
var dbFile string
var db *bolt.DB
func init() {
epMap.m = make(map[string]*epVal)
nwMap.m = make(map[string]*nwVal)
brMap.m = make(map[string]int)
brMap.brCount = 1
dbFile = "/tmp/dpdk_bolt.db"
}
//We should never see any errors in this function
func sendResponse(resp interface{}, w http.ResponseWriter) {
rb, err := json.Marshal(resp)
if err != nil {
glog.Errorf("unable to marshal response %v", err)
}
glog.Infof("Sending response := %v, %v", resp, err)
fmt.Fprintf(w, "%s", rb)
return
}
func getBody(r *http.Request) ([]byte, error) {
body, err := ioutil.ReadAll(r.Body)
glog.Infof("URL [%s] Body [%s] Error [%v]", r.URL.Path[1:], string(body), err)
return body, err
}
func handler(w http.ResponseWriter, r *http.Request) {
body, _ := getBody(r)
resp := api.Response{}
resp.Err = "Unhandled API request " + string(r.URL.Path[1:]) + " " + string(body)
sendResponse(resp, w)
}
func handlerPluginActivate(w http.ResponseWriter, r *http.Request) {
_, _ = getBody(r)
//TODO: Where is this encoding?
resp := `{
"Implements": ["NetworkDriver", "IpamDriver"]
}`
fmt.Fprintf(w, "%s", resp)
}
func handlerGetCapabilities(w http.ResponseWriter, r *http.Request) {
_, _ = getBody(r)
resp := api.GetCapabilityResponse{Scope: "local"}
sendResponse(resp, w)
}
func handlerCreateNetwork(w http.ResponseWriter, r *http.Request) {
resp := api.CreateNetworkResponse{}
bridge := "br"
body, err := getBody(r)
if err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := api.CreateNetworkRequest{}
if err := json.Unmarshal(body, &req); err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
nwMap.Lock()
defer nwMap.Unlock()
//Record the docker network UUID to SDN bridge mapping
//This has to survive a plugin crash/restart and needs to be persisted
nwMap.m[req.NetworkID] = &nwVal{
Bridge: bridge,
Gateway: *req.IPv4Data[0].Gateway,
}
if err := dbAdd("nwMap", req.NetworkID, nwMap.m[req.NetworkID]); err != nil {
glog.Errorf("Unable to update db %v", err)
}
// For VPP, we are connecting endpoints via a bridge which requires
// a unique integer ID.
brMap.Lock()
brMap.m[req.NetworkID] = brMap.brCount
brMap.brCount = brMap.brCount + 1
if err := dbAdd("brMap", req.NetworkID, brMap.m[req.NetworkID]); err != nil {
glog.Errorf("Unable to update db %v", err)
}
brMap.Unlock()
sendResponse(resp, w)
}
func handlerDeleteNetwork(w http.ResponseWriter, r *http.Request) {
resp := api.DeleteNetworkResponse{}
body, err := getBody(r)
if err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := api.DeleteNetworkRequest{}
if err = json.Unmarshal(body, &req); err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
glog.Infof("Delete Network := %v", req.NetworkID)
nwMap.Lock()
defer nwMap.Unlock()
bridge := nwMap.m[req.NetworkID].Bridge
delete(nwMap.m, req.NetworkID)
if err := dbDelete("nwMap", req.NetworkID); err != nil {
glog.Errorf("Unable to update db %v %v", err, bridge)
}
brMap.Lock()
delete(brMap.m, req.NetworkID)
if err := dbDelete("brMap", req.NetworkID); err != nil {
glog.Errorf("Unable to update db %v %v", err, bridge)
}
brMap.Unlock()
sendResponse(resp, w)
return
}
func handlerEndpointOperInfof(w http.ResponseWriter, r *http.Request) {
resp := api.EndpointInfoResponse{}
body, err := getBody(r)
if err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := api.EndpointInfoRequest{}
err = json.Unmarshal(body, &req)
if err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
sendResponse(resp, w)
}
func handlerCreateEndpoint(w http.ResponseWriter, r *http.Request) {
resp := api.CreateEndpointResponse{}
body, err := getBody(r)
if err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := api.CreateEndpointRequest{}
if err = json.Unmarshal(body, &req); err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
if req.Interface.Address == "" {
resp.Err = "Error: IP Address parameter not provided in docker run"
sendResponse(resp, w)
return
}
ip, _, err := net.ParseCIDR(req.Interface.Address)
if err != nil {
resp.Err = "Error: Invalid IP Address " + err.Error()
sendResponse(resp, w)
return
}
nwMap.Lock()
bridge := nwMap.m[req.NetworkID].Bridge
nwMap.Unlock()
if bridge == "" {
resp.Err = "Error: incompatible network"
sendResponse(resp, w)
return
}
nwMap.Lock()
defer nwMap.Unlock()
epMap.Lock()
defer epMap.Unlock()
brMap.Lock()
defer brMap.Unlock()
//Generate a vhost-user port name to use with dummy interface.
//We'll use the interfaces IP address
vhostPort := fmt.Sprintf("%s", ip)
//Create a unique path on the host to place the socket
socketpath := fmt.Sprintf("/tmp/vhostuser_%s", ip)
err = os.Mkdir(socketpath, 0500)
if err != nil {
resp.Err = fmt.Sprintf("Error making socket path %s: err: %v", socketpath, err)
sendResponse(resp, w)
return
}
//Generate VPP-dpdk vhost-user interface:
cmd := "vppctl"
args := []string{"create", "vhost", "socket", fmt.Sprintf("%s/vhu.sock", socketpath), "server"}
bifc, err := exec.Command(cmd, args...).Output()
if err != nil {
glog.Infof("ERROR: [%v] [%v] [%v] ", cmd, args, err)
resp.Err = fmt.Sprintf("Error EndPointCreate: [%v] [%v] [%v]",
cmd, args, err)
sendResponse(resp, w)
return
}
ifcb, _, err := bufio.NewReader(bytes.NewReader(bifc)).ReadLine()
ifc := string(ifcb)
glog.Infof("Created vhost-user interface %v %v", ifc, err)
//Set Interface state to up
cmd = "vppctl"
args = []string{"set", "interface", "state", ifc, "up"}
if err := exec.Command(cmd, args...).Run(); err != nil {
resp.Err = fmt.Sprintf("Error EndPointCreate: [%v] [%v] [%v]",
cmd, args, err)
sendResponse(resp, w)
return
}
//Attach VPP vhost-user interface to VPP L2 Bridge Domain
//TODO: Need to create a unique bridge domain ID per network
cmd = "vppctl"
args = []string{"set", "interface", "l2", "bridge", ifc, fmt.Sprint(brMap.m[req.NetworkID])}
if err := exec.Command(cmd, args...).Run(); err != nil {
resp.Err = fmt.Sprintf("Error EndPointCreate: [%v] [%v] [%v]",
cmd, args, err)
sendResponse(resp, w)
return
}
/* Setup the dummy interface corresponding to the dpdk port
* This is done so that docker CNM will program the IP Address
* and other properties on this Interface
* This dummy interface will be discovered by clear containers
* which then maps the actual vhost-user port to the VM
* This is needed today as docker does not pass any information
* from the network plugin to the runtime
*/
cmd = "ip"
args = []string{"link", "add", vhostPort, "type", "dummy"}
if err := exec.Command(cmd, args...).Run(); err != nil {
resp.Err = fmt.Sprintf("Error EndPointCreate: [%v] [%v] [%v]",
cmd, args, err)
sendResponse(resp, w)
return
}
glog.Infof("Setup dummy port %v %v ", cmd, args)
epMap.m[req.EndpointID] = &epVal{
IP: req.Interface.Address,
vhostuserPort: vhostPort,
vppInterface: ifc,
}
if err := dbAdd("epMap", req.EndpointID, epMap.m[req.EndpointID]); err != nil {
glog.Errorf("Unable to update db %v %v", err, ip)
}
sendResponse(resp, w)
}
func handlerDeleteEndpoint(w http.ResponseWriter, r *http.Request) {
resp := api.DeleteEndpointResponse{}
body, err := getBody(r)
if err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := api.DeleteEndpointRequest{}
if err = json.Unmarshal(body, &req); err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
epMap.Lock()
nwMap.Lock()
m := epMap.m[req.EndpointID]
vhostPort := m.vhostuserPort
vppInterface := m.vppInterface
delete(epMap.m, req.EndpointID)
if err := dbDelete("epMap", req.EndpointID); err != nil {
glog.Errorf("Unable to update db %v %v", err, m)
}
nwMap.Unlock()
epMap.Unlock()
//put interface into down state
cmd := "vppctl"
args := []string{"set", "interface", "state", vppInterface, "down"}
if err := exec.Command(cmd, args...).Run(); err != nil {
resp.Err = fmt.Sprintf("Error DeleteEndpoint: [%v] [%v] [%v]",
cmd, args, err)
sendResponse(resp, w)
return
}
//delete vhost interface
cmd = "vppctl"
args = []string{"delete", "vhost-user", vppInterface}
if err := exec.Command(cmd, args...).Run(); err != nil {
resp.Err = fmt.Sprintf("Error DeleteEndpoint: [%v] [%v] [%v]",
cmd, args, err)
sendResponse(resp, w)
return
}
//delete dummy port
cmd = "ip"
args = []string{"link", "del", vhostPort}
if err := exec.Command(cmd, args...).Run(); err != nil {
resp.Err = fmt.Sprintf("Error EndPointCreate: [%v] [%v] [%v]",
cmd, args, err)
sendResponse(resp, w)
return
}
glog.Infof("Deleted dummy port %v %v ", cmd, args)
// vhostPort contains the IP address
os.RemoveAll(fmt.Sprintf("/tmp/vhostuser_%s/", vhostPort))
if err != nil {
glog.Infof("Couldn't remove /tmp/vhostuser_%s", vhostPort)
resp.Err = fmt.Sprintf("Couldn't delete /tmp/vhostuser_%s: %v", vhostPort, err)
sendResponse(resp, w)
return
}
sendResponse(resp, w)
}
func handlerJoin(w http.ResponseWriter, r *http.Request) {
resp := api.JoinResponse{}
body, err := getBody(r)
if err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := api.JoinRequest{}
if err := json.Unmarshal(body, &req); err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
nwMap.Lock()
epMap.Lock()
nm := nwMap.m[req.NetworkID]
em := epMap.m[req.EndpointID]
nwMap.Unlock()
epMap.Unlock()
resp.Gateway = nm.Gateway.IP.String()
resp.InterfaceName = &api.InterfaceName{
SrcName: em.vhostuserPort,
DstPrefix: "eth",
}
glog.Infof("Join Response %v %v", resp, em.vhostuserPort)
sendResponse(resp, w)
}
func handlerLeave(w http.ResponseWriter, r *http.Request) {
resp := api.LeaveResponse{}
body, err := getBody(r)
if err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := api.LeaveRequest{}
if err := json.Unmarshal(body, &req); err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
sendResponse(resp, w)
}
func handlerDiscoverNew(w http.ResponseWriter, r *http.Request) {
resp := api.DiscoveryResponse{}
body, err := getBody(r)
if err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := api.DiscoveryNotification{}
if err := json.Unmarshal(body, &req); err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
sendResponse(resp, w)
}
func handlerDiscoverDelete(w http.ResponseWriter, r *http.Request) {
resp := api.DiscoveryResponse{}
body, err := getBody(r)
if err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := api.DiscoveryNotification{}
if err := json.Unmarshal(body, &req); err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
sendResponse(resp, w)
}
func handlerExternalConnectivity(w http.ResponseWriter, r *http.Request) {
resp := api.ProgramExternalConnectivityResponse{}
body, err := getBody(r)
if err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := api.ProgramExternalConnectivityRequest{}
if err := json.Unmarshal(body, &req); err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
sendResponse(resp, w)
}
func handlerRevokeExternalConnectivity(w http.ResponseWriter, r *http.Request) {
resp := api.RevokeExternalConnectivityResponse{}
body, err := getBody(r)
if err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := api.RevokeExternalConnectivityResponse{}
if err := json.Unmarshal(body, &req); err != nil {
resp.Err = "Error: " + err.Error()
sendResponse(resp, w)
return
}
sendResponse(resp, w)
}
func ipamGetCapabilities(w http.ResponseWriter, r *http.Request) {
if _, err := getBody(r); err != nil {
glog.Infof("ipamGetCapabilities: unable to get request body [%v]", err)
}
resp := ipamapi.GetCapabilityResponse{RequiresMACAddress: true}
sendResponse(resp, w)
}
func ipamGetDefaultAddressSpaces(w http.ResponseWriter, r *http.Request) {
resp := ipamapi.GetAddressSpacesResponse{}
if _, err := getBody(r); err != nil {
glog.Infof("ipamGetDefaultAddressSpaces: unable to get request body [%v]", err)
}
resp.GlobalDefaultAddressSpace = ""
resp.LocalDefaultAddressSpace = ""
sendResponse(resp, w)
}
func ipamRequestPool(w http.ResponseWriter, r *http.Request) {
resp := ipamapi.RequestPoolResponse{}
body, err := getBody(r)
if err != nil {
resp.Error = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := ipamapi.RequestPoolRequest{}
if err := json.Unmarshal(body, &req); err != nil {
resp.Error = "Error: " + err.Error()
sendResponse(resp, w)
return
}
resp.PoolID = uuid.Generate().String()
resp.Pool = req.Pool
sendResponse(resp, w)
}
func ipamReleasePool(w http.ResponseWriter, r *http.Request) {
resp := ipamapi.ReleasePoolResponse{}
body, err := getBody(r)
if err != nil {
resp.Error = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := ipamapi.ReleasePoolRequest{}
if err := json.Unmarshal(body, &req); err != nil {
resp.Error = "Error: " + err.Error()
sendResponse(resp, w)
return
}
sendResponse(resp, w)
}
func ipamRequestAddress(w http.ResponseWriter, r *http.Request) {
resp := ipamapi.RequestAddressResponse{}
body, err := getBody(r)
if err != nil {
resp.Error = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := ipamapi.RequestAddressRequest{}
if err := json.Unmarshal(body, &req); err != nil {
resp.Error = "Error: " + err.Error()
sendResponse(resp, w)
return
}
//TODO: Should come from the subnet mask for the subnet
if req.Address != "" {
resp.Address = req.Address + "/24"
} else {
resp.Error = "Error: Request does not have IP address. Specify using --ip"
}
sendResponse(resp, w)
}
func ipamReleaseAddress(w http.ResponseWriter, r *http.Request) {
resp := ipamapi.ReleaseAddressResponse{}
body, err := getBody(r)
if err != nil {
resp.Error = "Error: " + err.Error()
sendResponse(resp, w)
return
}
req := ipamapi.ReleaseAddressRequest{}
if err := json.Unmarshal(body, &req); err != nil {
resp.Error = "Error: " + err.Error()
sendResponse(resp, w)
return
}
sendResponse(resp, w)
}
func dbTableInit(tables []string) (err error) {
glog.Infof("dbInit Tables := %v", tables)
for i, v := range tables {
glog.Infof("table[%v] := %v, %v", i, v, []byte(v))
}
err = db.Update(func(tx *bolt.Tx) error {
for _, table := range tables {
_, err := tx.CreateBucketIfNotExists([]byte(table))
if err != nil {
return fmt.Errorf("Bucket creation error: %v %v", table, err)
}
}
return nil
})
if err != nil {
glog.Errorf("Table creation error %v", err)
}
return err
}
func dbAdd(table string, key string, value interface{}) (err error) {
err = db.Update(func(tx *bolt.Tx) error {
var v bytes.Buffer
if err := gob.NewEncoder(&v).Encode(value); err != nil {
glog.Errorf("Encode Error: %v %v", err, value)
return err
}
bucket := tx.Bucket([]byte(table))
if bucket == nil {
return fmt.Errorf("Bucket %v not found", table)
}
err = bucket.Put([]byte(key), v.Bytes())
if err != nil {
return fmt.Errorf("Key Store error: %v %v %v %v", table, key, value, err)
}
return nil
})
return err
}
func dbDelete(table string, key string) (err error) {
err = db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(table))
if bucket == nil {
return fmt.Errorf("Bucket %v not found", table)
}
err = bucket.Delete([]byte(key))
if err != nil {
return fmt.Errorf("Key Delete error: %v %v ", key, err)
}
return nil
})
return err
}
func dbGet(table string, key string) (value interface{}, err error) {
err = db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(table))
if bucket == nil {
return fmt.Errorf("Bucket %v not found", table)
}
val := bucket.Get([]byte(key))
if val == nil {
return nil
}
v := bytes.NewReader(val)
if err := gob.NewDecoder(v).Decode(value); err != nil {
glog.Errorf("Decode Error: %v %v %v", table, key, err)
return err
}
return nil
})
return value, err
}
func initDb() error {
options := bolt.Options{
Timeout: 3 * time.Second,
}
var err error
db, err = bolt.Open(dbFile, 0644, &options)
if err != nil {
return fmt.Errorf("dbInit failed %v", err)
}
tables := []string{"global", "nwMap", "epMap", "brMap"}
if err := dbTableInit(tables); err != nil {
return fmt.Errorf("dbInit failed %v", err)
}
c, err := dbGet("global", "counter")
if err != nil {
glog.Errorf("dbGet failed %v", err)
intfCounter = 100
} else {
var ok bool
intfCounter, ok = c.(int)
if !ok {
intfCounter = 100
}
}
err = db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("nwMap"))
err := b.ForEach(func(k, v []byte) error {
vr := bytes.NewReader(v)
nVal := &nwVal{}
if err := gob.NewDecoder(vr).Decode(nVal); err != nil {
return fmt.Errorf("Decode Error: %v %v %v", string(k), string(v), err)
}
nwMap.m[string(k)] = nVal
glog.Infof("nwMap key=%v, value=%v\n", string(k), nVal)
return nil
})
return err
})
if err != nil {
return err
}
err = db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("epMap"))
err := b.ForEach(func(k, v []byte) error {
vr := bytes.NewReader(v)
eVal := &epVal{}
if err := gob.NewDecoder(vr).Decode(eVal); err != nil {
return fmt.Errorf("Decode Error: %v %v %v", string(k), string(v), err)
}
epMap.m[string(k)] = eVal
glog.Infof("epMap key=%v, value=%v\n", string(k), eVal)
return nil
})
return err
})
if err != nil {
return err
}
err = db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("brMap"))
err := b.ForEach(func(k, v []byte) error {
vr := bytes.NewReader(v)
brVal := 0
if err := gob.NewDecoder(vr).Decode(&brVal); err != nil {
return fmt.Errorf("Decode Error: %v %v %v", string(k), string(v), err)
}
brMap.m[string(k)] = brVal
glog.Infof("brMap key=%v, value=%v\n", string(k), brVal)
return nil
})
return err
})
return err
}
func main() {
flag.Parse()
if err := initDb(); err != nil {
glog.Fatalf("db init failed, quitting [%v]", err)
}
defer func() {
err := db.Close()
glog.Errorf("unable to close database [%v]", err)
}()
r := mux.NewRouter()
r.HandleFunc("/Plugin.Activate", handlerPluginActivate)
r.HandleFunc("/NetworkDriver.GetCapabilities", handlerGetCapabilities)
r.HandleFunc("/NetworkDriver.CreateNetwork", handlerCreateNetwork)
r.HandleFunc("/NetworkDriver.DeleteNetwork", handlerDeleteNetwork)
r.HandleFunc("/NetworkDriver.CreateEndpoint", handlerCreateEndpoint)
r.HandleFunc("/NetworkDriver.DeleteEndpoint", handlerDeleteEndpoint)
r.HandleFunc("/NetworkDriver.EndpointOperInfo", handlerEndpointOperInfof)
r.HandleFunc("/NetworkDriver.Join", handlerJoin)
r.HandleFunc("/NetworkDriver.Leave", handlerLeave)
r.HandleFunc("/NetworkDriver.DiscoverNew", handlerDiscoverNew)
r.HandleFunc("/NetworkDriver.DiscoverDelete", handlerDiscoverDelete)
r.HandleFunc("/NetworkDriver.ProgramExternalConnectivity", handlerExternalConnectivity)
r.HandleFunc("/NetworkDriver.RevokeExternalConnectivity", handlerRevokeExternalConnectivity)
r.HandleFunc("/IpamDriver.GetCapabilities", ipamGetCapabilities)
r.HandleFunc("/IpamDriver.GetDefaultAddressSpaces", ipamGetDefaultAddressSpaces)
r.HandleFunc("/IpamDriver.RequestPool", ipamRequestPool)
r.HandleFunc("/IpamDriver.ReleasePool", ipamReleasePool)
r.HandleFunc("/IpamDriver.RequestAddress", ipamRequestAddress)
r.HandleFunc("/", handler)
err := http.ListenAndServe("127.0.0.1:9075", r)
if err != nil {
glog.Errorf("docker plugin http server failed, [%v]", err)
}
}