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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| func cmdAdd(args *skel.CmdArgs) error { var success bool = false
n, cniVersion, err := loadNetConf(args.StdinData) if err != nil { return err }
isLayer3 := n.IPAM.Type != ""
if n.IsDefaultGW { n.IsGW = true }
if n.HairpinMode && n.PromiscMode { return fmt.Errorf("cannot set hairpin mode and promiscous mode at the same time.") }
br, brInterface, err := setupBridge(n) if err != nil { return err }
netns, err := ns.GetNS(args.Netns) if err != nil { return fmt.Errorf("failed to open netns %q: %v", args.Netns, err) } defer netns.Close()
hostInterface, containerInterface, err := setupVeth(netns, br, args.IfName, n.MTU, n.HairpinMode) if err != nil { return err }
result := ¤t.Result{CNIVersion: cniVersion, Interfaces: []*current.Interface{brInterface, hostInterface, containerInterface}}
if isLayer3 { r, err := ipam.ExecAdd(n.IPAM.Type, args.StdinData) if err != nil { return err }
defer func() { if !success { os.Setenv("CNI_COMMAND", "DEL") ipam.ExecDel(n.IPAM.Type, args.StdinData) os.Setenv("CNI_COMMAND", "ADD") } }()
ipamResult, err := current.NewResultFromResult(r) if err != nil { return err }
result.IPs = ipamResult.IPs result.Routes = ipamResult.Routes
if len(result.IPs) == 0 { return errors.New("IPAM plugin returned missing IP config") }
gwsV4, gwsV6, err := calcGateways(result, n) if err != nil { return err }
if err := netns.Do(func(_ ns.NetNS) error { contVeth, err := net.InterfaceByName(args.IfName) if err != nil { return err }
for _, ipc := range result.IPs { if ipc.Version == "6" && (n.HairpinMode || n.PromiscMode) { if err := disableIPV6DAD(args.IfName); err != nil { return err } break } }
if err := ipam.ConfigureIface(args.IfName, result); err != nil { return err }
for _, ipc := range result.IPs { if ipc.Version == "4" { _ = arping.GratuitousArpOverIface(ipc.Address.IP, *contVeth) } } return nil }); err != nil { return err }
if n.IsGW { var firstV4Addr net.IP for _, gws := range []*gwInfo{gwsV4, gwsV6} { for _, gw := range gws.gws { if gw.IP.To4() != nil && firstV4Addr == nil { firstV4Addr = gw.IP }
err = ensureBridgeAddr(br, gws.family, &gw, n.ForceAddress) if err != nil { return fmt.Errorf("failed to set bridge addr: %v", err) } }
if gws.gws != nil { if err = enableIPForward(gws.family); err != nil { return fmt.Errorf("failed to enable forwarding: %v", err) } } } }
if n.IPMasq { chain := utils.FormatChainName(n.Name, args.ContainerID) comment := utils.FormatComment(n.Name, args.ContainerID) for _, ipc := range result.IPs { if err = ip.SetupIPMasq(ip.Network(&ipc.Address), chain, comment); err != nil { return err } } } }
br, err = bridgeByName(n.BrName) if err != nil { return err } brInterface.Mac = br.Attrs().HardwareAddr.String()
result.DNS = n.DNS
if debugPostIPAMError != nil { return debugPostIPAMError }
success = true
return types.PrintResult(result, cniVersion) }
|