Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit a554624

Browse files
committed
support multi ip addresses
Signed-off-by: Gao feng <[email protected]>
1 parent 2e9a759 commit a554624

File tree

13 files changed

+183
-145
lines changed

13 files changed

+183
-145
lines changed

cli/network.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/exec"
99
"strconv"
10+
"strings"
1011
"syscall"
1112

1213
"github.com/golang/glog"
@@ -383,22 +384,24 @@ func collectionInterfaceInfo() []InterfaceInfo {
383384
// lo is here too
384385
continue
385386
}
386-
387+
info := InterfaceInfo{
388+
Index: link.Attrs().Index,
389+
PeerIndex: link.Attrs().ParentIndex,
390+
}
391+
ipAddrs := []string{}
387392
addrs, err := netlink.AddrList(link, netlink.FAMILY_V4)
388393
if err != nil {
389394
glog.Error(err)
390395
return infos
391396
}
392397

393398
for _, addr := range addrs {
394-
info := InterfaceInfo{
395-
Ip: addr.IPNet.String(),
396-
Index: link.Attrs().Index,
397-
PeerIndex: link.Attrs().ParentIndex,
398-
}
399-
glog.Infof("get interface %v", info)
400-
infos = append(infos, info)
399+
ipAddrs = append(ipAddrs, addr.IPNet.String())
401400
}
401+
info.Ip = strings.Join(ipAddrs, ",")
402+
glog.Infof("get interface %v", info)
403+
infos = append(infos, info)
404+
402405
}
403406
return infos
404407
}

hyperstart/api/grpc/hyperstart.pb.go

Lines changed: 107 additions & 87 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hyperstart/api/grpc/hyperstart.proto

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,14 @@ message StartSandboxRequest {
9292
message DestroySandboxRequest {
9393
}
9494

95+
message IpAddress {
96+
string address = 1;
97+
string mask = 2;
98+
}
99+
95100
message UpdateInterfaceRequest {
96101
string device = 1;
97-
string address = 2;
98-
string mask = 3;
102+
repeated IpAddress ipAddresses = 2;
99103
}
100104

101105
message AddRouteRequest {

hyperstart/api/json/spec.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,18 @@ type Container struct {
7777
Ports []Port `json:"ports,omitempty"` //deprecated
7878
}
7979

80-
type NetworkInf struct {
81-
Device string `json:"device"`
80+
type IpAddress struct {
8281
IpAddress string `json:"ipAddress"`
8382
NetMask string `json:"netMask"`
8483
}
8584

85+
type NetworkInf struct {
86+
Device string `json:"device"`
87+
IpAddress string `json:"ipAddress,omitempty"`
88+
NetMask string `json:"netMask,omitempty"`
89+
IpAddresses []IpAddress `json:"ipAddresses"`
90+
}
91+
8692
type Route struct {
8793
Dest string `json:"dest"`
8894
Gateway string `json:"gateway,omitempty"`

hyperstart/libhyperstart/grpc.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,14 @@ func (h *grpcBasedHyperstart) AddRoute(routes []hyperstartjson.Route) error {
8686
return err
8787
}
8888

89-
func (h *grpcBasedHyperstart) UpdateInterface(dev, ip, mask string) error {
90-
_, err := h.grpc.UpdateInterface(h.ctx, &hyperstartgrpc.UpdateInterfaceRequest{
91-
Device: dev,
92-
Address: ip,
93-
Mask: mask,
94-
})
89+
func (h *grpcBasedHyperstart) UpdateInterface(dev string, ipAddresses []hyperstartjson.IpAddress) error {
90+
req := &hyperstartgrpc.UpdateInterfaceRequest{
91+
Device: dev,
92+
}
93+
for _, addr := range ipAddresses {
94+
req.IpAddresses = append(req.IpAddresses, &hyperstartgrpc.IpAddress{addr.IpAddress, addr.NetMask})
95+
}
96+
_, err := h.grpc.UpdateInterface(h.ctx, req)
9597
return err
9698
}
9799

hyperstart/libhyperstart/hyperstart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Hyperstart interface {
3232
WriteFile(container, path string, data []byte) error
3333
ReadFile(container, path string) ([]byte, error)
3434
AddRoute(r []hyperstartapi.Route) error
35-
UpdateInterface(dev, ip, mask string) error
35+
UpdateInterface(dev string, addresses []hyperstartapi.IpAddress) error
3636
OnlineCpuMem() error
3737
}
3838

hyperstart/libhyperstart/json.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,11 +646,10 @@ func (h *jsonBasedHyperstart) AddRoute(r []hyperstartapi.Route) error {
646646
return h.hyperstartCommand(hyperstartapi.INIT_SETUPROUTE, hyperstartapi.Routes{Routes: r})
647647
}
648648

649-
func (h *jsonBasedHyperstart) UpdateInterface(dev, ip, mask string) error {
649+
func (h *jsonBasedHyperstart) UpdateInterface(dev string, ipAddresses []hyperstartapi.IpAddress) error {
650650
return h.hyperstartCommand(hyperstartapi.INIT_SETUPINTERFACE, hyperstartapi.NetworkInf{
651-
Device: dev,
652-
IpAddress: ip,
653-
NetMask: mask,
651+
Device: dev,
652+
IpAddresses: ipAddresses,
654653
})
655654
}
656655

hyperstart/proxy/proxy.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ func (proxy *jsonProxy) DestroySandbox(ctx context.Context, req *hyperstartgrpc.
123123
return pbEmpty(err), err
124124
}
125125
func (proxy *jsonProxy) UpdateInterface(ctx context.Context, req *hyperstartgrpc.UpdateInterfaceRequest) (*google_protobuf.Empty, error) {
126-
err := proxy.json.UpdateInterface(req.Device, req.Address, req.Mask)
126+
addresses := []hyperstartjson.IpAddress{}
127+
for _, addr := range req.IpAddresses {
128+
addresses = append(addresses, hyperstartjson.IpAddress{addr.Address, addr.Mask})
129+
}
130+
131+
err := proxy.json.UpdateInterface(req.Device, addresses)
127132
return pbEmpty(err), err
128133
}
129134
func (proxy *jsonProxy) AddRoute(ctx context.Context, req *hyperstartgrpc.AddRouteRequest) (*google_protobuf.Empty, error) {

hypervisor/events.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ type InterfaceCreated struct {
5757
DeviceName string
5858
MacAddr string
5959
IpAddr string
60-
NetMask string
6160
RouteTable []*RouteRule
6261
}
6362

hypervisor/network.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package hypervisor
22

33
import (
44
"fmt"
5-
"net"
65
"sync"
76

87
"github.com/hyperhq/runv/api"
@@ -94,7 +93,6 @@ func (nc *NetworkContext) addInterface(inf *api.InterfaceDescription, result cha
9493
Id: inf.Id,
9594
DeviceName: DEFAULT_LO_DEVICE_NAME,
9695
IpAddr: inf.Ip,
97-
NetMask: "255.255.255.255",
9896
}
9997
nc.lo[inf.Ip] = i
10098
nc.idMap[inf.Id] = i
@@ -313,13 +311,6 @@ func (nc *NetworkContext) close() {
313311
}
314312

315313
func interfaceGot(id string, index int, pciAddr int, name string, inf *network.Settings) (*InterfaceCreated, error) {
316-
ip, nw, err := net.ParseCIDR(inf.IPAddress)
317-
if err != nil {
318-
return &InterfaceCreated{Index: index, PCIAddr: pciAddr, DeviceName: name}, err
319-
}
320-
var tmp []byte = nw.Mask
321-
var mask net.IP = tmp
322-
323314
rt := []*RouteRule{}
324315
/* Route rule is generated automaticly on first interface,
325316
* or generated on the gateway configured interface. */
@@ -338,8 +329,7 @@ func interfaceGot(id string, index int, pciAddr int, name string, inf *network.S
338329
HostDevice: inf.Device,
339330
DeviceName: name,
340331
MacAddr: inf.Mac,
341-
IpAddr: ip.String(),
342-
NetMask: mask.String(),
332+
IpAddr: inf.IPAddress,
343333
RouteTable: rt,
344334
}, nil
345335
}

0 commit comments

Comments
 (0)