Skip to content

Commit

Permalink
add vlan tag for vlan supported kernel mechanism
Browse files Browse the repository at this point in the history
Signed-off-by: Periyasamy Palanisamy <periyasamy.palanisamy@est.tech>
  • Loading branch information
pperiyasamy committed Sep 28, 2021
1 parent dd436eb commit 1d6415f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/HdrHistogram/hdrhistogram-go v1.0.1 // indirect
github.com/RoaringBitmap/roaring v0.9.4
github.com/benbjohnson/clock v1.1.0
github.com/edwarnicke/exechelper v1.0.2
github.com/edwarnicke/grpcfd v0.1.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ github.com/HdrHistogram/hdrhistogram-go v1.0.1/go.mod h1:BWJ+nMSHY3L41Zj7CA3uXnl
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/OneOfOne/xxhash v1.2.3 h1:wS8NNaIgtzapuArKIAjsyXtEN/IUjQkbw90xszUdS40=
github.com/OneOfOne/xxhash v1.2.3/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/RoaringBitmap/roaring v0.9.4 h1:ckvZSX5gwCRaJYBNe7syNawCU5oruY9gQmjXlp4riwo=
github.com/RoaringBitmap/roaring v0.9.4/go.mod h1:icnadbWcNyfEHlYdr+tDlOTih1Bf/h+rzPpv4sbomAA=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/armon/go-metrics v0.0.0-20190430140413-ec5e00d3c878 h1:EFSB7Zo9Eg91v7MJPVsifUysc/wPdN+NOnVe6bWbdBM=
github.com/armon/go-metrics v0.0.0-20190430140413-ec5e00d3c878/go.mod h1:3AMJUQhVx52RsWOnlkpikZr01T/yAVN2gn0861vByNg=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/bits-and-blooms/bitset v1.2.0 h1:Kn4yilvwNtMACtf1eYDlG8H77R07mZSPbMjLyS07ChA=
github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
Expand Down Expand Up @@ -116,6 +120,8 @@ github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-runewidth v0.0.0-20181025052659-b20a3daf6a39/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mna/pigeon v0.0.0-20180808201053-bb0192cfc2ae/go.mod h1:Iym28+kJVnC1hfQvv5MUtI6AiFFzvQjHcvI4RFTG/04=
github.com/mschoch/smat v0.2.0 h1:8imxQsjDm8yFEAVBe7azKmKSgzSkZXDuKkSq9374khM=
github.com/mschoch/smat v0.2.0/go.mod h1:kc9mz7DoBKqDyiRL7VZN8KvXQMWeTaVnttLRXOlotKw=
github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg=
github.com/nats-io/jwt v0.3.2 h1:+RB5hMpXUUA2dfxuhBTEkMOrYmM+gKIZYS1KjSostMI=
github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU=
Expand Down
41 changes: 39 additions & 2 deletions pkg/networkservice/common/mechanisms/kernel/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package kernel

import (
"context"
"errors"
"sync"

"github.com/RoaringBitmap/roaring"
"github.com/golang/protobuf/ptypes/empty"

"github.com/networkservicemesh/api/pkg/api/networkservice"
Expand All @@ -30,6 +33,8 @@ import (

type kernelMechanismServer struct {
interfaceName string
freeVLANs *roaring.Bitmap
lock sync.Mutex
}

// NewServer - creates a NetworkServiceServer that requests a kernel interface and populates the netns inode
Expand All @@ -38,23 +43,55 @@ func NewServer(opts ...Option) networkservice.NetworkServiceServer {
for _, opt := range opts {
opt(o)
}
vlans := roaring.New()
vlans.AddRange(1, 4095)
return &kernelMechanismServer{
interfaceName: o.interfaceName,
freeVLANs: vlans,
}
}

func (m *kernelMechanismServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
if mechanism := kernelmech.ToMechanism(request.GetConnection().GetMechanism()); mechanism != nil {
mechanism := kernelmech.ToMechanism(request.GetConnection().GetMechanism())
if mechanism != nil {
mechanism.SetNetNSURL(netNSURL)
if m.interfaceName != "" {
mechanism.SetInterfaceName(m.interfaceName)
} else {
mechanism.SetInterfaceName(getNameFromConnection(request.GetConnection()))
}
if mechanism.SupportsVLAN() {
m.lock.Lock()
if m.freeVLANs.IsEmpty() {
m.lock.Unlock()
return nil, errors.New("vlan id pool is empty")
}
vlanID := m.freeVLANs.Minimum()
m.freeVLANs.Remove(vlanID)
mechanism.SetVLAN(vlanID)
m.lock.Unlock()
}
}
conn, err := next.Server(ctx).Request(ctx, request)
if err != nil && mechanism != nil && mechanism.SupportsVLAN() {
m.releaseVLANID(mechanism)
}
return next.Server(ctx).Request(ctx, request)
return conn, err
}

func (m *kernelMechanismServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
mechanism := kernelmech.ToMechanism(conn.GetMechanism())
if mechanism != nil && mechanism.SupportsVLAN() {
m.releaseVLANID(mechanism)
}
return next.Server(ctx).Close(ctx, conn)
}

func (m *kernelMechanismServer) releaseVLANID(mechanism *kernelmech.Mechanism) {
m.lock.Lock()
defer m.lock.Unlock()
vlanID := mechanism.GetVLAN()
if vlanID > 0 {
m.freeVLANs.Add(vlanID)
}
}

0 comments on commit 1d6415f

Please sign in to comment.