-
Notifications
You must be signed in to change notification settings - Fork 0
/
vlan.go
61 lines (48 loc) · 1.88 KB
/
vlan.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
package main
import (
"context"
"fmt"
"github.com/vishvananda/netlink"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/lovi-cloud/teleskop/protoc/agent"
)
func (a *agent) AddVLANInterface(ctx context.Context, req *pb.AddVLANInterfaceRequest) (*pb.AddVLANInterfaceResponse, error) {
_, err := netlink.LinkByName(fmt.Sprintf("%s.%d", req.ParentInterface, req.VlanId))
if err == nil {
// TODO: already exist
return &pb.AddVLANInterfaceResponse{}, nil
}
parentLink, err := netlink.LinkByName(req.ParentInterface)
if err != nil {
return nil, status.Errorf(codes.NotFound, "failed to find parent interface: %+v\n", err)
}
vlan := &netlink.Vlan{
LinkAttrs: netlink.LinkAttrs{
Name: fmt.Sprintf("%s.%d", parentLink.Attrs().Name, req.VlanId),
ParentIndex: parentLink.Attrs().Index,
},
VlanId: int(req.VlanId),
VlanProtocol: netlink.VLAN_PROTOCOL_8021Q,
}
if err := netlink.LinkAdd(vlan); err != nil {
return nil, status.Errorf(codes.Internal, "failed to create vlan interface: %+v", err)
}
if err := netlink.LinkSetUp(vlan); err != nil {
return nil, status.Errorf(codes.Internal, "failed to set up vlan interface: %+v", err)
}
return &pb.AddVLANInterfaceResponse{}, nil
}
func (a *agent) DeleteVLANInterface(ctx context.Context, req *pb.DeleteVLANInterfaceRequest) (*pb.DeleteVLANInterfaceResponse, error) {
link, err := netlink.LinkByName(fmt.Sprintf("%s.%d", req.ParentInterface, req.VlanId))
if err != nil {
return nil, status.Errorf(codes.NotFound, "failed to find vlan interface: %+v\n", err)
}
if err := netlink.LinkSetDown(link); err != nil {
return nil, status.Errorf(codes.Internal, "failed to set down vlan interface: %+v", err)
}
if err := netlink.LinkDel(link); err != nil {
return nil, status.Errorf(codes.Internal, "failed to delete vlan interface: %+v", err)
}
return &pb.DeleteVLANInterfaceResponse{}, nil
}