func (p *OVSPlugin) SetupOVSInterface(podName, podInfraContainerID string, port *ports.Port, ipcidr, gateway string, containerRuntime string) error { qvb, qvo := p.buildVethName(port.ID) ret, err := exec.RunCommand("ip", "link", "add", qvb, "type", "veth", "peer", "name", qvo) if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, containerRuntime) return err } bridge := p.buildBridgeName(port.ID) ret, err = exec.RunCommand("brctl", "addbr", bridge) if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, containerRuntime) return err } ret, err = exec.RunCommand("ip", "link", "set", qvb, "up") if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, containerRuntime) return err } ret, err = exec.RunCommand("ip", "link", "set", qvo, "up") if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, containerRuntime) return err } ret, err = exec.RunCommand("ip", "link", "set", bridge, "up") if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, containerRuntime) return err } ret, err = exec.RunCommand("brctl", "addif", bridge, qvb) if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, containerRuntime) return err } ret, err = exec.RunCommand("ovs-vsctl", "-vconsole:off", "--", "--if-exists", "del-port", qvo, "--", "add-port", p.IntegrationBridage, qvo, "--", "set", "Interface", qvo, fmt.Sprintf("external_ids:attached-mac=%s", port.MACAddress), fmt.Sprintf("external_ids:iface-id=%s", port.ID), fmt.Sprintf("external_ids:vm-id=%s", podName), "external_ids:iface-status=active") if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, containerRuntime) return err } return nil }
func (p *OVSPlugin) destroyDockerInterface(podName, podInfraContainerID, portID string) error { tapName, _ := p.buildTapName(portID) _, err := exec.RunCommand("ip", "link", "delete", tapName) if err != nil { glog.V(5).Infof("Warning: DestroyInterface failed: %v", err) } pid, err := exec.RunCommand("docker", "inspect", "-f", "'{{.State.Pid}}'", podInfraContainerID) if err != nil { glog.V(5).Infof("Warning: DestroyInterface failed: %v", err) } if pid != nil && len(pid) > 0 { netns := strings.Trim(pid[0], "'") _, err = exec.RunCommand("rm", "-f", fmt.Sprintf("/var/run/netns/%s", netns)) if err != nil { glog.V(5).Infof("Warning: DestroyInterface failed: %v", err) } } return nil }
func (p *OVSPlugin) destroyOVSInterface(podName, podInfraContainerID, portID string) error { qvb, qvo := p.buildVethName(portID) bridge := p.buildBridgeName(portID) output, err := exec.RunCommand("brctl", "delif", bridge, qvb) if err != nil { glog.Warningf("Warning: brctl delif %s failed: %v, %v", qvb, output, err) } output, err = exec.RunCommand("ip", "link", "set", "dev", bridge, "down") if err != nil { glog.Warningf("Warning: set bridge %s down failed: %v, %v", bridge, output, err) } output, err = exec.RunCommand("brctl", "delbr", bridge) if err != nil { glog.Warningf("Warning: delete bridge %s failed: %v, %v", bridge, output, err) } output, err = exec.RunCommand("ovs-vsctl", "-vconsole:off", "--if-exists", "del-port", qvo) if err != nil { glog.Warningf("Warning: ovs del-port %s failed: %v, %v", qvo, output, err) } output, err = exec.RunCommand("ip", "link", "set", "dev", qvo, "down") if err != nil { glog.Warningf("Warning: set dev %s down failed: %v, %v", qvo, output, err) } output, err = exec.RunCommand("ip", "link", "delete", "dev", qvo) if err != nil { glog.Warningf("Warning: delete dev %s failed: %v, %v", qvo, output, err) } return nil }
func (p *OVSPlugin) SetupDockerInterface(podName, podInfraContainerID string, port *ports.Port, ipcidr, gateway string) error { tapName, vifName := p.buildTapName(port.ID) ret, err := exec.RunCommand("ip", "link", "add", tapName, "type", "veth", "peer", "name", vifName) if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } bridge := p.buildBridgeName(port.ID) ret, err = exec.RunCommand("brctl", "addif", bridge, tapName) if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } ret, err = exec.RunCommand("ip", "link", "set", "dev", vifName, "address", port.MACAddress) if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } pid, err := exec.RunCommand("docker", "inspect", "-f", "'{{.State.Pid}}'", podInfraContainerID) if err != nil { p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } netns := strings.Trim(pid[0], "'") ret, err = exec.RunCommand("ln", "-s", fmt.Sprintf("/proc/%s/ns/net", netns), fmt.Sprintf("/var/run/netns/%s", netns)) if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } ret, err = exec.RunCommand("ip", "link", "set", vifName, "netns", netns) if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } ret, err = exec.RunCommand("ip", "netns", "exec", netns, "ip", "link", "delete", "eth0") if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } ret, err = exec.RunCommand("ip", "netns", "exec", netns, "ip", "link", "set", vifName, "down") if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } ret, err = exec.RunCommand("ip", "netns", "exec", netns, "ip", "link", "set", vifName, "name", "eth0") if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } ret, err = exec.RunCommand("ip", "netns", "exec", netns, "ip", "link", "set", "eth0", "up") if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } ret, err = exec.RunCommand("ip", "netns", "exec", netns, "ip", "addr", "add", "dev", "eth0", ipcidr) if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } ret, err = exec.RunCommand("ip", "netns", "exec", netns, "ip", "route", "add", "default", "via", gateway) if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } ret, err = exec.RunCommand("ip", "link", "set", "dev", tapName, "up") if err != nil { glog.Warningf("SetupInterface failed, ret:%s, error:%v", strings.Join(ret, "\n"), err) p.DestroyInterface(podName, podInfraContainerID, port, runtimeTypeDocker) return err } _, err = exec.RunCommand("rm", "-f", fmt.Sprintf("/var/run/netns/%s", netns)) if err != nil { glog.V(5).Infof("Warning: remove netns symlink failed: %v", err) } return nil }