Skip to content

Commit

Permalink
Implemented writing to carrier file to enable it when bringing up an …
Browse files Browse the repository at this point in the history
…interface

Fix for #4
  • Loading branch information
gshimansky committed Feb 1, 2019
1 parent a1502aa commit 27a04c6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ FROM ${USER_NAME}/nff-go-base

LABEL RUN docker run -it --privileged -v /sys/bus/pci/drivers:/sys/bus/pci/drivers -v /sys/kernel/mm/hugepages:/sys/kernel/mm/hugepages -v /sys/devices/system/node:/sys/devices/system/node -v /dev:/dev --name NAME -e NAME=NAME -e IMAGE=IMAGE IMAGE

RUN apt-get install -y procps iproute2 iputils-ping apache2 wget; apt-get clean
RUN apt-get install -y procps iproute2 iputils-ping net-tools apache2 wget; apt-get clean
RUN dd if=/dev/zero of=/var/www/html/10k.bin bs=1 count=10240
RUN dd if=/dev/zero of=/var/www/html/100k.bin bs=1 count=102400
RUN dd if=/dev/zero of=/var/www/html/1m.bin bs=1 count=1048576
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.7.0 // indirect
github.com/intel-go/nff-go v0.7.3
github.com/microcosm-cc/bluemonday v1.0.2 // indirect
github.com/nsf/gocode v0.0.0-20181120081338-6cac7c69a41e // indirect
github.com/openzipkin/zipkin-go v0.1.5 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/le
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo=
github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM=
github.com/nsf/gocode v0.0.0-20181120081338-6cac7c69a41e h1:ihaECqr05sBOt6hJAEIHzbE2RF47BK34Z0LbQnxDiYw=
github.com/nsf/gocode v0.0.0-20181120081338-6cac7c69a41e/go.mod h1:6Q8/OMaaKAgTX7/jt2bOXVDrm1eJhoNd+iwzghR7jvs=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
Expand Down
62 changes: 45 additions & 17 deletions nat/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,34 @@ func ParseAllKnownL4(pkt *packet.Packet, pktIPv4 *packet.IPv4Hdr, pktIPv6 *packe
}
}

const sysFsIfFormat = "/sys/devices/virtual/net/%s/carrier"

func bringInterfaceUp(dev netlink.Link, name string) error {
err := netlink.LinkSetUp(dev)
var err error
err = netlink.LinkSetUp(dev)
if err != nil {
return fmt.Errorf("Failed to bring interface up", name, ":", err)
} else {
fmt.Println("Interface", name, "brought up successfully")
return nil
return fmt.Errorf("Failed to bring interface up \"%s\": %+v", name, err)
}

fname := fmt.Sprintf(sysFsIfFormat, name)
fmt.Println("NAME=", name)
fmt.Println("FILE NAME=", fname)
var file *os.File
file, err = os.OpenFile(fname, os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("Failed to enable carrier for interface \"%s\" because file \"%s\" could not be open: %+v", name, fname, err)
}
_, err = file.Write([]byte("1"))
if err != nil {
return fmt.Errorf("Failed to write to carrier file \"%s\": %+v", fname, err)
}
err = file.Close()
if err != nil {
return fmt.Errorf("Failed to close carrier file \"%s\": %+v", fname, err)
}

fmt.Println("Interface", name, "brought up successfully")
return nil
}

func (port *ipPort) setLinkIPv4KNIAddress(ipv4addr, mask, oldaddr, oldmask uint32, bringup bool) error {
Expand All @@ -255,7 +275,7 @@ func (port *ipPort) setLinkIPv4KNIAddress(ipv4addr, mask, oldaddr, oldmask uint3

myKNI, err := netlink.LinkByName(port.KNIName)
if err != nil {
return fmt.Errorf("Failed to get KNI interface", port.KNIName, ":", err)
return fmt.Errorf("Failed to get KNI interface %s: %+v", port.KNIName, err)
}

if oldaddr != 0 {
Expand All @@ -270,7 +290,14 @@ func (port *ipPort) setLinkIPv4KNIAddress(ipv4addr, mask, oldaddr, oldmask uint3
fmt.Println("Removing address", addr, "on interface", port.KNIName)
err = netlink.AddrDel(myKNI, addr)
if err != nil {
return fmt.Errorf("Failed to remove address", addr, "from interface", port.KNIName, ":", err)
return fmt.Errorf("Failed to remove address %+v from interface \"%s\": %+v", addr, port.KNIName, err)
}
}

if bringup {
err = bringInterfaceUp(myKNI, port.KNIName)
if err != nil {
return err
}
}

Expand All @@ -285,13 +312,10 @@ func (port *ipPort) setLinkIPv4KNIAddress(ipv4addr, mask, oldaddr, oldmask uint3
fmt.Println("Setting address", addr, "on interface", port.KNIName)
err = netlink.AddrAdd(myKNI, addr)
if err != nil {
return fmt.Errorf("Failed to set interface", port.KNIName, "address", addr, ":", err)
return fmt.Errorf("Failed to set interface \"%s\" address %+v: %+v", port.KNIName, addr, err)
}

fmt.Println("Set address", addr, "on KNI interface", port.KNIName)
if bringup {
return bringInterfaceUp(myKNI, port.KNIName)
}
fmt.Println("Successfully set address", addr, "on KNI interface", port.KNIName)
return nil
}

Expand All @@ -305,6 +329,13 @@ func (port *ipPort) setLinkIPv6KNIAddress(ipv6addr, mask, oldaddr, oldmask [comm
return fmt.Errorf("Failed to get KNI interface", port.KNIName, ":", err)
}

if bringup {
err = bringInterfaceUp(myKNI, port.KNIName)
if err != nil {
return err
}
}

if oldaddr != zeroIPv6Addr {
addr := &netlink.Addr{
IPNet: &net.IPNet{
Expand All @@ -328,12 +359,9 @@ func (port *ipPort) setLinkIPv6KNIAddress(ipv6addr, mask, oldaddr, oldmask [comm
fmt.Println("Setting address", addr)
err = netlink.AddrAdd(myKNI, addr)
if err != nil {
return fmt.Errorf("Failed to set interface", port.KNIName, "address", addr, ":", err)
return fmt.Errorf("Failed to set interface \"%s\" address %+v: %+v", port.KNIName, addr, err)
}

fmt.Println("Set address", addr, "on KNI interface", port.KNIName)
if bringup {
return bringInterfaceUp(myKNI, port.KNIName)
}
fmt.Println("Successfully set address", addr, "on KNI interface", port.KNIName)
return nil
}

0 comments on commit 27a04c6

Please sign in to comment.