| 1 | // Copyright 2015 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "flag" |
| 20 | "fmt" |
| 21 | "log" |
| 22 | "net/rpc" |
| 23 | "os" |
| 24 | "path/filepath" |
| 25 | "time" |
| 26 | |
| 27 | "github.com/containernetworking/cni/pkg/skel" |
| 28 | "github.com/containernetworking/cni/pkg/types" |
| 29 | current "github.com/containernetworking/cni/pkg/types/100" |
| 30 | "github.com/containernetworking/cni/pkg/version" |
| 31 | bv "github.com/containernetworking/plugins/pkg/utils/buildversion" |
| 32 | ) |
| 33 | |
| 34 | const defaultSocketPath = "/run/cni/dhcp.sock" |
| 35 | |
| 36 | // The top-level network config - IPAM plugins are passed the full configuration |
| 37 | // of the calling plugin, not just the IPAM section. |
| 38 | type NetConf struct { |
| 39 | types.NetConf |
| 40 | IPAM *IPAMConfig `json:"ipam"` |
| 41 | } |
| 42 | |
| 43 | type IPAMConfig struct { |
| 44 | types.IPAM |
| 45 | DaemonSocketPath string `json:"daemonSocketPath"` |
| 46 | // When requesting IP from DHCP server, carry these options for management purpose. |
| 47 | // Some fields have default values, and can be override by setting a new option with the same name at here. |
| 48 | ProvideOptions []ProvideOption `json:"provide"` |
| 49 | // When requesting IP from DHCP server, claiming these options are necessary. Options are necessary unless `optional` |
| 50 | // is set to `false`. |
| 51 | // To override default requesting fields, set `skipDefault` to `false`. |
| 52 | // If an field is not optional, but the server failed to provide it, error will be raised. |
| 53 | RequestOptions []RequestOption `json:"request"` |
| 54 | // The metric of routes |
| 55 | Priority int `json:"priority,omitempty"` |
| 56 | } |
| 57 | |
| 58 | // DHCPOption represents a DHCP option. It can be a number, or a string defined in manual dhcp-options(5). |
| 59 | // Note that not all DHCP options are supported at all time. Error will be raised if unsupported options are used. |
| 60 | type DHCPOption string |
| 61 | |
| 62 | type ProvideOption struct { |
| 63 | Option DHCPOption `json:"option"` |
| 64 | |
| 65 | Value string `json:"value"` |
| 66 | ValueFromCNIArg string `json:"fromArg"` |
| 67 | } |
| 68 | |
| 69 | type RequestOption struct { |
| 70 | SkipDefault bool `json:"skipDefault"` |
| 71 | |
| 72 | Option DHCPOption `json:"option"` |
| 73 | } |
| 74 | |
| 75 | func main() { |
| 76 | if len(os.Args) > 1 && os.Args[1] == "daemon" { |
| 77 | var pidfilePath string |
| 78 | var hostPrefix string |
| 79 | var socketPath string |
| 80 | var broadcast bool |
| 81 | var timeout time.Duration |
| 82 | var resendMax time.Duration |
| 83 | var resendTimeout time.Duration |
| 84 | daemonFlags := flag.NewFlagSet("daemon", flag.ExitOnError) |
| 85 | daemonFlags.StringVar(&pidfilePath, "pidfile", "", "optional path to write daemon PID to") |
| 86 | daemonFlags.StringVar(&hostPrefix, "hostprefix", "", "optional prefix to host root") |
| 87 | daemonFlags.StringVar(&socketPath, "socketpath", "", "optional dhcp server socketpath") |
| 88 | daemonFlags.BoolVar(&broadcast, "broadcast", false, "broadcast DHCP leases") |
| 89 | daemonFlags.DurationVar(&timeout, "timeout", 10*time.Second, "optional dhcp client timeout duration for each request") |
| 90 | daemonFlags.DurationVar(&resendMax, "resendmax", resendDelayMax, "optional dhcp client max resend delay between requests") |
| 91 | daemonFlags.DurationVar(&resendTimeout, "resendtimeout", defaultResendTimeout, "optional dhcp client resend timeout, no more retries after this timeout") |
| 92 | daemonFlags.Parse(os.Args[2:]) |
| 93 | |
| 94 | if socketPath == "" { |
| 95 | socketPath = defaultSocketPath |
| 96 | } |
| 97 | |
| 98 | if err := runDaemon(pidfilePath, hostPrefix, socketPath, timeout, resendMax, resendTimeout, broadcast); err != nil { |
| 99 | log.Print(err.Error()) |
| 100 | os.Exit(1) |
| 101 | } |
| 102 | } else { |
| 103 | skel.PluginMainFuncs(skel.CNIFuncs{ |
| 104 | Add: cmdAdd, |
| 105 | Check: cmdCheck, |
| 106 | Del: cmdDel, |
| 107 | /* FIXME GC */ |
| 108 | /* FIXME Status */ |
| 109 | }, version.All, bv.BuildString("dhcp")) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func cmdAdd(args *skel.CmdArgs) error { |
| 114 | // Plugin must return result in same version as specified in netconf |
| 115 | versionDecoder := &version.ConfigDecoder{} |
| 116 | confVersion, err := versionDecoder.Decode(args.StdinData) |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | |
| 121 | result := ¤t.Result{CNIVersion: current.ImplementedSpecVersion} |
| 122 | if err := rpcCall("DHCP.Allocate", args, result); err != nil { |
| 123 | return err |
| 124 | } |
| 125 | |
| 126 | return types.PrintResult(result, confVersion) |
| 127 | } |
| 128 | |
| 129 | func cmdDel(args *skel.CmdArgs) error { |
| 130 | result := struct{}{} |
| 131 | return rpcCall("DHCP.Release", args, &result) |
| 132 | } |
| 133 | |
| 134 | func cmdCheck(args *skel.CmdArgs) error { |
| 135 | // Plugin must return result in same version as specified in netconf |
| 136 | versionDecoder := &version.ConfigDecoder{} |
| 137 | // confVersion, err := versionDecoder.Decode(args.StdinData) |
| 138 | _, err := versionDecoder.Decode(args.StdinData) |
| 139 | if err != nil { |
| 140 | return err |
| 141 | } |
| 142 | |
| 143 | result := ¤t.Result{CNIVersion: current.ImplementedSpecVersion} |
| 144 | return rpcCall("DHCP.Allocate", args, result) |
| 145 | } |
| 146 | |
| 147 | func getSocketPath(stdinData []byte) (string, error) { |
| 148 | conf := NetConf{} |
| 149 | if err := json.Unmarshal(stdinData, &conf); err != nil { |
| 150 | return "", fmt.Errorf("error parsing socket path conf: %v", err) |
| 151 | } |
| 152 | if conf.IPAM.DaemonSocketPath == "" { |
| 153 | return defaultSocketPath, nil |
| 154 | } |
| 155 | return conf.IPAM.DaemonSocketPath, nil |
| 156 | } |
| 157 | |
| 158 | func rpcCall(method string, args *skel.CmdArgs, result interface{}) error { |
| 159 | socketPath, err := getSocketPath(args.StdinData) |
| 160 | if err != nil { |
| 161 | return fmt.Errorf("error obtaining socketPath: %v", err) |
| 162 | } |
| 163 | |
| 164 | client, err := rpc.DialHTTP("unix", socketPath) |
| 165 | if err != nil { |
| 166 | return fmt.Errorf("error dialing DHCP daemon: %v", err) |
| 167 | } |
| 168 | |
| 169 | // The daemon may be running under a different working dir |
| 170 | // so make sure the netns path is absolute. |
| 171 | netns, err := filepath.Abs(args.Netns) |
| 172 | if err != nil { |
| 173 | return fmt.Errorf("failed to make %q an absolute path: %v", args.Netns, err) |
| 174 | } |
| 175 | args.Netns = netns |
| 176 | |
| 177 | err = client.Call(method, args, result) |
| 178 | if err != nil { |
| 179 | return fmt.Errorf("error calling %v: %v", method, err) |
| 180 | } |
| 181 | |
| 182 | return nil |
| 183 | } |