add ovsdpdk cni
[openretriever.git] / src / cni / ovsdpdk / ovsdpdk.patch
1 diff --git a/build.sh b/build.sh
2 index cd21ba8..bc60d91 100755
3 --- a/build.sh
4 +++ b/build.sh
5 @@ -19,7 +19,7 @@ export GOPATH=${PWD}/gopath
6  mkdir -p "${PWD}/bin"
7  
8  echo "Building plugins"
9 -PLUGINS="plugins/meta/* plugins/main/* plugins/ipam/* plugins/sample"
10 +PLUGINS="plugins/main/ovsdpdk plugins/main/bridge plugins/ipam/host-local"
11  for d in $PLUGINS; do
12         if [ -d "$d" ]; then
13                 plugin="$(basename "$d")"
14 diff --git a/plugins/main/ovsdpdk/ovsdpdk.go b/plugins/main/ovsdpdk/ovsdpdk.go
15 new file mode 100644
16 index 0000000..1b931d4
17 --- /dev/null
18 +++ b/plugins/main/ovsdpdk/ovsdpdk.go
19 @@ -0,0 +1,117 @@
20 +// Copyright 2014 CNI authors
21 +//
22 +// Licensed under the Apache License, Version 2.0 (the "License");
23 +// you may not use this file except in compliance with the License.
24 +// You may obtain a copy of the License at
25 +//
26 +//     http://www.apache.org/licenses/LICENSE-2.0
27 +//
28 +// Unless required by applicable law or agreed to in writing, software
29 +// distributed under the License is distributed on an "AS IS" BASIS,
30 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 +// See the License for the specific language governing permissions and
32 +// limitations under the License.
33 +
34 +package main
35 +
36 +import (
37 +       "encoding/json"
38 +       "errors"
39 +       "fmt"
40 +       //"net"
41 +       "runtime"
42 +       //"syscall"
43 +        "os/exec"
44 +       //"io/ioutil"
45 +
46 +       "github.com/containernetworking/cni/pkg/skel"
47 +       "github.com/containernetworking/cni/pkg/types"
48 +       "github.com/containernetworking/cni/pkg/types/current"
49 +       "github.com/containernetworking/cni/pkg/version"
50 +       //"github.com/containernetworking/plugins/pkg/ip"
51 +       "github.com/containernetworking/plugins/pkg/ipam"
52 +       //"github.com/containernetworking/plugins/pkg/ns"
53 +       //"github.com/containernetworking/plugins/pkg/utils"
54 +       //"github.com/vishvananda/netlink"
55 +)
56 +
57 +const defaultBrName = "cni0"
58 +
59 +type NetConf struct {
60 +       types.NetConf
61 +       BrName       string `json:"bridge"`
62 +}
63 +
64 +func init() {
65 +       // this ensures that main runs only on main thread (thread group leader).
66 +       // since namespace ops (unshare, setns) are done for a single thread, we
67 +       // must ensure that the goroutine does not jump from OS thread to thread
68 +       runtime.LockOSThread()
69 +}
70 +
71 +func loadNetConf(bytes []byte) (*NetConf, string, error) {
72 +       n := &NetConf{
73 +               BrName: defaultBrName,
74 +       }
75 +       if err := json.Unmarshal(bytes, n); err != nil {
76 +               return nil, "", fmt.Errorf("failed to load netconf: %v", err)
77 +       }
78 +       return n, n.CNIVersion, nil
79 +}
80 +
81 +func setupVhostUser(args *skel.CmdArgs, types string) error {
82 +    exec.Command("/bin/bash", "/opt/cni/bin/setup_ovsdpdk.sh", args.Netns, args.ContainerID, types).Output()
83 +    return nil
84 +}
85 +
86 +
87 +func cmdAdd(args *skel.CmdArgs) error {
88 +       n, cniVersion, err := loadNetConf(args.StdinData)
89 +       if err != nil {
90 +               return err
91 +       }
92 +
93 +       // run the IPAM plugin and get back the config to apply
94 +       r, err := ipam.ExecAdd(n.IPAM.Type, args.StdinData)
95 +       if err != nil {
96 +               return err
97 +       }
98 +
99 +       // Convert whatever the IPAM result was into the current Result type
100 +       result, err := current.NewResultFromResult(r)
101 +       if err != nil {
102 +               return err
103 +       }
104 +
105 +       if len(result.IPs) == 0 {
106 +               return errors.New("IPAM plugin returned missing IP config")
107 +       }
108 +
109 +        setupVhostUser(args, result.String())
110 +
111 +        return types.PrintResult(result, cniVersion)
112 +}
113 +
114 +func tearDownVhostUser(args *skel.CmdArgs) error {
115 +    exec.Command("/bin/bash", "/opt/cni/bin/teardown_ovsdpdk.sh", args.Netns, args.ContainerID).Output()
116 +    return nil
117 +}
118 +
119 +func cmdDel(args *skel.CmdArgs) error {
120 +        n, _, err := loadNetConf(args.StdinData)
121 +        if err != nil {
122 +                return err
123 +        }
124 +
125 +        if err := ipam.ExecDel(n.IPAM.Type, args.StdinData); err != nil {
126 +                return err
127 +        }
128 +
129 +        tearDownVhostUser(args)
130 +        return err
131 +
132 +}
133 +
134 +func main() {
135 +       skel.PluginMain(cmdAdd, cmdDel, version.All)
136 +}