diff --git a/plugins/idalloc/idalloc.go b/plugins/idalloc/idalloc.go index 2954dc7bb3..5f245060d9 100644 --- a/plugins/idalloc/idalloc.go +++ b/plugins/idalloc/idalloc.go @@ -51,8 +51,9 @@ type IDAllocator struct { type Deps struct { infra.PluginDeps - ContivConf contivconf.API - RemoteDB nodesync.KVDBWithAtomic + ServiceLabel servicelabel.ReaderAPI + ContivConf contivconf.API + RemoteDB nodesync.KVDBWithAtomic } // poolMetadata contains metadata of a pool used for faster ID allocation. @@ -153,7 +154,7 @@ func (a *IDAllocator) InitPool(name string, poolRange *idallocation.AllocationPo pool := &idallocation.AllocationPool{ Name: name, Range: poolRange, - IdAllocations: map[string]uint32{}, + IdAllocations: map[string]*idallocation.AllocationPool_Allocation{}, } // save the pool in db @@ -252,7 +253,7 @@ func (a *IDAllocator) ReleaseID(poolName string, idLabel string) (err error) { if poolMeta == nil { return } - id := pool.IdAllocations[idLabel] + alloc := pool.IdAllocations[idLabel] succeeded := false for i := 0; i < maxIDAllocationAttempts; i++ { @@ -262,7 +263,7 @@ func (a *IDAllocator) ReleaseID(poolName string, idLabel string) (err error) { } if succeeded { // successfully released an ID - delete(poolMeta.allocatedIDs, id) + delete(poolMeta.allocatedIDs, alloc.Id) break } else { // pool changed in db, re-read from db and retry @@ -281,7 +282,7 @@ func (a *IDAllocator) ReleaseID(poolName string, idLabel string) (err error) { a.Log.Errorf("Error by releasing ID: %v", err) } - a.Log.Debugf("Released ID for label '%s' in pool %s: %d", idLabel, poolName, id) + a.Log.Debugf("Released ID for label '%s' in pool %s: %d", idLabel, poolName, alloc.Id) return nil } @@ -291,8 +292,8 @@ func (a *IDAllocator) tryToAllocateID(pool *idallocation.AllocationPool, poolMet id uint32, succeeded bool, err error) { // step 0, try to get already allocated ID number - if id, exists := pool.IdAllocations[idLabel]; exists { - return id, true, nil + if alloc, exists := pool.IdAllocations[idLabel]; exists { + return alloc.Id, true, nil } // step 1, find a free ID number @@ -316,7 +317,10 @@ func (a *IDAllocator) tryToAllocateID(pool *idallocation.AllocationPool, poolMet if err != nil { return 0, false, err } - pool.IdAllocations[idLabel] = id + pool.IdAllocations[idLabel] = &idallocation.AllocationPool_Allocation{ + Id: id, + Owner: a.ServiceLabel.GetAgentLabel(), + } newData, err := a.serializer.Marshal(pool) if err != nil { return 0, false, err @@ -335,21 +339,33 @@ func (a *IDAllocator) tryToAllocateID(pool *idallocation.AllocationPool, poolMet func (a *IDAllocator) tryToReleaseID(pool *idallocation.AllocationPool, poolMeta *poolMetadata, idLabel string) ( succeeded bool, err error) { - // check if it is not already released - if _, exists := pool.IdAllocations[idLabel]; !exists { + alloc, exists := pool.IdAllocations[idLabel] + if !exists { + // already released + return true, nil + } + if alloc.Owner != a.ServiceLabel.GetAgentLabel() { + // we do not own this allocation, do not write into db + delete(pool.IdAllocations, idLabel) return true, nil } - // try to write into db + // marshal the old state prevData, err := a.serializer.Marshal(pool) if err != nil { return false, err } + + // delete the allocation from cache delete(pool.IdAllocations, idLabel) + + // marshal the new state newData, err := a.serializer.Marshal(pool) if err != nil { return false, err } + + // delete from db db, err := a.getDBBroker() if err != nil { a.Log.Error(err) @@ -392,8 +408,8 @@ func (a *IDAllocator) buildPoolMetadata(pool *idallocation.AllocationPool) *pool for _, id := range pool.Range.Reserved { meta.reservedIDs[id] = true } - for label, id := range pool.IdAllocations { - meta.allocatedIDs[id] = label + for label, alloc := range pool.IdAllocations { + meta.allocatedIDs[alloc.Id] = label } return meta } diff --git a/plugins/idalloc/idallocation/idallocation.pb.go b/plugins/idalloc/idallocation/idallocation.pb.go index 6f818a8a84..b6b908cd1f 100644 --- a/plugins/idalloc/idallocation/idallocation.pb.go +++ b/plugins/idalloc/idallocation/idallocation.pb.go @@ -25,11 +25,11 @@ type AllocationPool struct { Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` Range *AllocationPool_Range `protobuf:"bytes,2,opt,name=range,proto3" json:"range,omitempty"` // map of all allocations, key is the allocation "label" - // describing its purpose (e.g. nework name for vrf pool, etc.) - IdAllocations map[string]uint32 `protobuf:"bytes,3,rep,name=id_allocations,json=idAllocations,proto3" json:"id_allocations,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + // describing its purpose (e.g. network name for vrf pool, etc.) + IdAllocations map[string]*AllocationPool_Allocation `protobuf:"bytes,3,rep,name=id_allocations,json=idAllocations,proto3" json:"id_allocations,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *AllocationPool) Reset() { *m = AllocationPool{} } @@ -70,7 +70,7 @@ func (m *AllocationPool) GetRange() *AllocationPool_Range { return nil } -func (m *AllocationPool) GetIdAllocations() map[string]uint32 { +func (m *AllocationPool) GetIdAllocations() map[string]*AllocationPool_Allocation { if m != nil { return m.IdAllocations } @@ -131,29 +131,78 @@ func (m *AllocationPool_Range) GetReserved() []uint32 { return nil } +type AllocationPool_Allocation struct { + Id uint32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + Owner string `protobuf:"bytes,2,opt,name=owner,proto3" json:"owner,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *AllocationPool_Allocation) Reset() { *m = AllocationPool_Allocation{} } +func (m *AllocationPool_Allocation) String() string { return proto.CompactTextString(m) } +func (*AllocationPool_Allocation) ProtoMessage() {} +func (*AllocationPool_Allocation) Descriptor() ([]byte, []int) { + return fileDescriptor_4826bf6deca43300, []int{0, 1} +} +func (m *AllocationPool_Allocation) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_AllocationPool_Allocation.Unmarshal(m, b) +} +func (m *AllocationPool_Allocation) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_AllocationPool_Allocation.Marshal(b, m, deterministic) +} +func (m *AllocationPool_Allocation) XXX_Merge(src proto.Message) { + xxx_messageInfo_AllocationPool_Allocation.Merge(m, src) +} +func (m *AllocationPool_Allocation) XXX_Size() int { + return xxx_messageInfo_AllocationPool_Allocation.Size(m) +} +func (m *AllocationPool_Allocation) XXX_DiscardUnknown() { + xxx_messageInfo_AllocationPool_Allocation.DiscardUnknown(m) +} + +var xxx_messageInfo_AllocationPool_Allocation proto.InternalMessageInfo + +func (m *AllocationPool_Allocation) GetId() uint32 { + if m != nil { + return m.Id + } + return 0 +} + +func (m *AllocationPool_Allocation) GetOwner() string { + if m != nil { + return m.Owner + } + return "" +} + func init() { proto.RegisterType((*AllocationPool)(nil), "idallocation.AllocationPool") - proto.RegisterMapType((map[string]uint32)(nil), "idallocation.AllocationPool.IdAllocationsEntry") + proto.RegisterMapType((map[string]*AllocationPool_Allocation)(nil), "idallocation.AllocationPool.IdAllocationsEntry") proto.RegisterType((*AllocationPool_Range)(nil), "idallocation.AllocationPool.Range") + proto.RegisterType((*AllocationPool_Allocation)(nil), "idallocation.AllocationPool.Allocation") } func init() { proto.RegisterFile("idallocation.proto", fileDescriptor_4826bf6deca43300) } var fileDescriptor_4826bf6deca43300 = []byte{ - // 238 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x90, 0xcf, 0x4a, 0xc4, 0x30, - 0x10, 0x87, 0x49, 0x6a, 0x16, 0x9d, 0xb5, 0x8b, 0x0c, 0x0a, 0xa5, 0xa7, 0xb2, 0xa7, 0x9e, 0x2a, - 0xac, 0x97, 0xc5, 0x93, 0x1e, 0x3c, 0xf4, 0xa6, 0x39, 0x78, 0x5d, 0xa2, 0x09, 0x12, 0x4c, 0x13, - 0x49, 0xeb, 0xd2, 0x3e, 0xa3, 0x2f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, 0x00, 0x00, + // 269 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x91, 0x41, 0x4b, 0xfb, 0x40, + 0x10, 0xc5, 0xc9, 0xe6, 0xbf, 0xe5, 0xdf, 0xa9, 0x09, 0x32, 0x28, 0x84, 0x9c, 0x42, 0x2f, 0xe6, + 0x14, 0x21, 0x5e, 0x8a, 0xe0, 0xc1, 0x83, 0x87, 0xdc, 0x74, 0x0f, 0x5e, 0xcb, 0xea, 0x2e, 0x32, + 0x98, 0xec, 0xca, 0x26, 0xd6, 0xf6, 0x1b, 0xf9, 0x31, 0x25, 0x9b, 0x9a, 0xb4, 0x08, 0xbd, 0xcd, + 0xfb, 0x31, 0xf3, 0xe6, 0xed, 0x2c, 0x20, 0x29, 0x59, 0xd7, 0xf6, 0x55, 0x76, 0x64, 0x4d, 0xf1, + 0xe1, 0x6c, 0x67, 0xf1, 0xec, 0x90, 0x2d, 0xbf, 0x43, 0x88, 0xef, 0x47, 0xf9, 0x68, 0x6d, 0x8d, + 0x08, 0xff, 0x8c, 0x6c, 0x74, 0x12, 0x64, 0x41, 0x3e, 0x17, 0xbe, 0xc6, 0x15, 0x70, 0x27, 0xcd, + 0x9b, 0x4e, 0x58, 0x16, 0xe4, 0x8b, 0x72, 0x59, 0x1c, 0x19, 0x1f, 0x1b, 0x14, 0xa2, 0xef, 0x14, + 0xc3, 0x00, 0x3e, 0x43, 0x4c, 0x6a, 0x3d, 0x35, 0xb7, 0x49, 0x98, 0x85, 0xf9, 0xa2, 0xbc, 0x3e, + 0x69, 0x51, 0xa9, 0x09, 0xb4, 0x0f, 0xa6, 0x73, 0x3b, 0x11, 0xd1, 0x21, 0x4b, 0x9f, 0x80, 0xfb, + 0x3d, 0x78, 0x09, 0xb3, 0x86, 0xcc, 0x9a, 0x94, 0x0f, 0x1c, 0x09, 0xde, 0x90, 0xa9, 0x94, 0xc7, + 0x72, 0xdb, 0x63, 0xb6, 0xc7, 0x72, 0x5b, 0x29, 0x4c, 0xe1, 0xbf, 0xd3, 0xad, 0x76, 0x1b, 0xad, + 0x7c, 0x90, 0x48, 0x8c, 0x3a, 0x2d, 0x01, 0xa6, 0x0d, 0x18, 0x03, 0x1b, 0x3d, 0x19, 0x29, 0xbc, + 0x00, 0x6e, 0xbf, 0x8c, 0x76, 0xde, 0x6f, 0x2e, 0x06, 0x91, 0x12, 0xe0, 0xdf, 0xac, 0x78, 0x0e, + 0xe1, 0xbb, 0xde, 0xed, 0x2f, 0xd8, 0x97, 0x78, 0x07, 0x7c, 0x23, 0xeb, 0xcf, 0xdf, 0x03, 0x5e, + 0x9d, 0x7c, 0xfd, 0x24, 0xc5, 0x30, 0x75, 0xcb, 0x56, 0xc1, 0xcb, 0xcc, 0xff, 0xdf, 0xcd, 0x4f, + 0x00, 0x00, 0x00, 0xff, 0xff, 0xd0, 0x36, 0xc7, 0xa9, 0xd5, 0x01, 0x00, 0x00, } diff --git a/plugins/idalloc/idallocation/idallocation.proto b/plugins/idalloc/idallocation/idallocation.proto index 72f0b4de80..bf6dff9880 100644 --- a/plugins/idalloc/idallocation/idallocation.proto +++ b/plugins/idalloc/idallocation/idallocation.proto @@ -27,7 +27,12 @@ message AllocationPool { } Range range = 2; // specification of the pool range + message Allocation { + uint32 id = 1; // allocated ID + string owner = 2; // owner of the allocation (only owner can release the allocation) + } + // map of all allocations, key is the allocation "label" - // describing its purpose (e.g. nework name for vrf pool, etc.) - map id_allocations = 3; + // describing its purpose (e.g. network name for vrf pool, etc.) + map id_allocations = 3; } diff --git a/plugins/idalloc/options.go b/plugins/idalloc/options.go index 85e4b418b4..73b3ecfa0c 100644 --- a/plugins/idalloc/options.go +++ b/plugins/idalloc/options.go @@ -17,6 +17,7 @@ package idalloc import ( "github.com/contiv/vpp/plugins/contivconf" "github.com/ligato/cn-infra/logging" + "github.com/ligato/cn-infra/servicelabel" ) // DefaultPlugin is a default instance of IDAlloc plugin. @@ -28,6 +29,7 @@ func NewPlugin(opts ...Option) *IDAllocator { p.PluginName = "idalloc" p.ContivConf = &contivconf.DefaultPlugin + p.ServiceLabel = &servicelabel.DefaultPlugin for _, o := range opts { o(p) diff --git a/plugins/ipnet/pod.go b/plugins/ipnet/pod.go index c9ecce1895..34c57281a8 100755 --- a/plugins/ipnet/pod.go +++ b/plugins/ipnet/pod.go @@ -260,6 +260,7 @@ func (n *IPNet) podCustomIfsConfig(pod *podmanager.LocalPod, eventType configEve switch customIf.ifType { case memifIfType: // handle custom memif interface + memifID++ if memifInfo == nil { memifInfo, err = n.DeviceManager.GetPodMemifInfo(pod.ID) if err != nil || memifInfo == nil { @@ -270,7 +271,6 @@ func (n *IPNet) podCustomIfsConfig(pod *podmanager.LocalPod, eventType configEve // VPP side of the memif k, v := n.podVPPMemif(pod, podIP, customIf.ifName, customIf.ifNet, memifInfo, memifID) config[k] = v - memifID++ case tapIfType: // handle custom tap interface