Skip to content

Commit

Permalink
Set MAC addr for host side of veth pair
Browse files Browse the repository at this point in the history
- Use hardcoded MAC of EE:EE:EE:EE:EE:EE
  (matchs libnetwork)
- If there is an error log message and fallback
  to kernel provided MAC
  • Loading branch information
tmjd committed Nov 28, 2017
1 parent 9eabe9b commit 0038728
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions calico_cni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ var _ = Describe("CalicoCni", func() {
log.Fatalf("Error getting result from the session: %v\n", err)
}

mac := contVeth.Attrs().HardwareAddr

Expect(len(result.IPs)).Should(Equal(1))
ip := result.IPs[0].Address.IP.String()
result.IPs[0].Address.IP = result.IPs[0].Address.IP.To4() // Make sure the IP is respresented as 4 bytes
Expand Down Expand Up @@ -96,6 +94,8 @@ var _ = Describe("CalicoCni", func() {
Expect(endpoints.Items[0].Name).Should(Equal(wrkload))
Expect(endpoints.Items[0].Namespace).Should(Equal(testutils.TEST_DEFAULT_NS))

mac := contVeth.Attrs().HardwareAddr

Expect(endpoints.Items[0].Spec).Should(Equal(api.WorkloadEndpointSpec{
InterfaceName: fmt.Sprintf("cali%s", containerID),
IPNetworks: []string{result.IPs[0].Address.String()},
Expand All @@ -116,6 +116,7 @@ var _ = Describe("CalicoCni", func() {
Expect(err).ToNot(HaveOccurred())
Expect(hostVeth.Attrs().Flags.String()).Should(ContainSubstring("up"))
Expect(hostVeth.Attrs().MTU).Should(Equal(1500))
Expect(hostVeth.Attrs().HardwareAddr.String()).Should(Equal("ee:ee:ee:ee:ee:ee"))

// Assert hostVeth sysctl values are set to what we expect for IPv4.
err = testutils.CheckSysctlValue(fmt.Sprintf("/proc/sys/net/ipv4/conf/%s/proxy_arp", hostVethName), "1")
Expand Down
10 changes: 10 additions & 0 deletions utils/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ func DoNetworking(args *skel.CmdArgs, conf types.NetConf, result *current.Result
return err
}

if mac, err := net.ParseMAC("EE:EE:EE:EE:EE:EE"); err != nil {
logger.Infof("failed to parse MAC Address: %v. Using kernel generated MAC.", err)
} else {
// Set the MAC address on the host side interface so the kernel does not
// have to generate a persistent address which fails some times.
if err = netlink.LinkSetHardwareAddr(hostVeth, mac); err != nil {
logger.Warnf("failed to Set MAC of %q: %v. Using kernel generated MAC.", hostVethName, err)
}
}

// Explicitly set the veth to UP state, because netlink doesn't always do that on all the platforms with net.FlagUp.
// veth won't get a link local address unless it's set to UP state.
if err = netlink.LinkSetUp(hostVeth); err != nil {
Expand Down

0 comments on commit 0038728

Please sign in to comment.