|
| 1 | +// Copyright 2017 DigitalOcean. |
| 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 ovsnl |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "unsafe" |
| 20 | + |
| 21 | + "github.com/digitalocean/go-openvswitch/ovsnl/internal/ovsh" |
| 22 | + "github.com/mdlayher/genetlink" |
| 23 | + "github.com/mdlayher/netlink" |
| 24 | + "github.com/mdlayher/netlink/nlenc" |
| 25 | +) |
| 26 | + |
| 27 | +// A DatapathService provides access to methods which interact with the |
| 28 | +// "ovs_datapath" generic netlink family. |
| 29 | +type DatapathService struct { |
| 30 | + c *Client |
| 31 | + f genetlink.Family |
| 32 | +} |
| 33 | + |
| 34 | +// A Datapath is an Open vSwitch in-kernel datapath. |
| 35 | +type Datapath struct { |
| 36 | + Index int |
| 37 | + Name string |
| 38 | + Features DatapathFeatures |
| 39 | + Stats DatapathStats |
| 40 | + MegaflowStats DatapathMegaflowStats |
| 41 | +} |
| 42 | + |
| 43 | +// DatapathFeatures is a set of bit flags that specify features for a datapath. |
| 44 | +type DatapathFeatures uint32 |
| 45 | + |
| 46 | +// Possible DatapathFeatures flag values. |
| 47 | +const ( |
| 48 | + DatapathFeaturesUnaligned DatapathFeatures = ovsh.DpFUnaligned |
| 49 | + DatapathFeaturesVPortPIDs DatapathFeatures = ovsh.DpFVportPids |
| 50 | +) |
| 51 | + |
| 52 | +// String returns the string representation of a DatapathFeatures. |
| 53 | +func (f DatapathFeatures) String() string { |
| 54 | + names := []string{ |
| 55 | + "unaligned", |
| 56 | + "vportpids", |
| 57 | + } |
| 58 | + |
| 59 | + var s string |
| 60 | + for i, name := range names { |
| 61 | + if f&(1<<uint(i)) != 0 { |
| 62 | + if s != "" { |
| 63 | + s += "|" |
| 64 | + } |
| 65 | + |
| 66 | + s += name |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if s == "" { |
| 71 | + s = "0" |
| 72 | + } |
| 73 | + |
| 74 | + return s |
| 75 | +} |
| 76 | + |
| 77 | +// DatapathStats contains statistics about packets that have passed |
| 78 | +// through a Datapath. |
| 79 | +type DatapathStats struct { |
| 80 | + // Number of flow table matches. |
| 81 | + Hit uint64 |
| 82 | + // Number of flow table misses. |
| 83 | + Missed uint64 |
| 84 | + // Number of misses not sent to userspace. |
| 85 | + Lost uint64 |
| 86 | + // Number of flows present. |
| 87 | + Flows uint64 |
| 88 | +} |
| 89 | + |
| 90 | +// DatapathMegaflowStats contains statistics about mega flow mask |
| 91 | +// usage for a Datapath. |
| 92 | +type DatapathMegaflowStats struct { |
| 93 | + // Number of masks used for flow lookups. |
| 94 | + MaskHits uint64 |
| 95 | + // Number of masks for the datapath. |
| 96 | + Masks uint32 |
| 97 | +} |
| 98 | + |
| 99 | +// List lists all Datapaths in the kernel. |
| 100 | +func (s *DatapathService) List() ([]Datapath, error) { |
| 101 | + req := genetlink.Message{ |
| 102 | + Header: genetlink.Header{ |
| 103 | + Command: ovsh.DpCmdGet, |
| 104 | + Version: uint8(s.f.Version), |
| 105 | + }, |
| 106 | + // Query all datapaths |
| 107 | + Data: nlenc.Uint32Bytes(0), |
| 108 | + } |
| 109 | + |
| 110 | + flags := netlink.HeaderFlagsRequest | netlink.HeaderFlagsDump |
| 111 | + msgs, err := s.c.c.Execute(req, s.f.ID, flags) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + return parseDatapaths(msgs) |
| 117 | +} |
| 118 | + |
| 119 | +// parseDatapaths parses a slice of Datapaths from a slice of generic netlink |
| 120 | +// messages. |
| 121 | +func parseDatapaths(msgs []genetlink.Message) ([]Datapath, error) { |
| 122 | + dps := make([]Datapath, 0, len(msgs)) |
| 123 | + |
| 124 | + for _, m := range msgs { |
| 125 | + if l := len(m.Data); l < sizeofHeader { |
| 126 | + return nil, fmt.Errorf("not enough data for OVS message header: %d bytes", l) |
| 127 | + } |
| 128 | + |
| 129 | + var dp Datapath |
| 130 | + |
| 131 | + // Fetch the header at the beginning of the message. |
| 132 | + h := *(*ovsh.Header)(unsafe.Pointer(&m.Data[:sizeofHeader][0])) |
| 133 | + dp.Index = int(h.Ifindex) |
| 134 | + |
| 135 | + // Skip the header to parse attributes. |
| 136 | + attrs, err := netlink.UnmarshalAttributes(m.Data[sizeofHeader:]) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + |
| 141 | + for _, a := range attrs { |
| 142 | + switch a.Type { |
| 143 | + case ovsh.DpAttrName: |
| 144 | + dp.Name = nlenc.String(a.Data) |
| 145 | + case ovsh.DpAttrUserFeatures: |
| 146 | + dp.Features = DatapathFeatures(nlenc.Uint32(a.Data)) |
| 147 | + case ovsh.DpAttrStats: |
| 148 | + dp.Stats, err = parseDPStats(a.Data) |
| 149 | + if err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + case ovsh.DpAttrMegaflowStats: |
| 153 | + dp.MegaflowStats, err = parseDPMegaflowStats(a.Data) |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + dps = append(dps, dp) |
| 161 | + } |
| 162 | + |
| 163 | + return dps, nil |
| 164 | +} |
| 165 | + |
| 166 | +// parseDPStats converts a byte slice into DatapathStats. |
| 167 | +func parseDPStats(b []byte) (DatapathStats, error) { |
| 168 | + // Verify that the byte slice is the correct length before doing |
| 169 | + // unsafe casts. |
| 170 | + if want, got := sizeofDPStats, len(b); want != got { |
| 171 | + return DatapathStats{}, fmt.Errorf("unexpected datapath stats structure size, want %d, got %d", want, got) |
| 172 | + } |
| 173 | + |
| 174 | + s := *(*ovsh.DPStats)(unsafe.Pointer(&b[0])) |
| 175 | + return DatapathStats{ |
| 176 | + Hit: s.Hit, |
| 177 | + Missed: s.Missed, |
| 178 | + Lost: s.Lost, |
| 179 | + Flows: s.Flows, |
| 180 | + }, nil |
| 181 | +} |
| 182 | + |
| 183 | +// parseDPMegaflowStats converts a byte slice into DatapathMegaflowStats. |
| 184 | +func parseDPMegaflowStats(b []byte) (DatapathMegaflowStats, error) { |
| 185 | + // Verify that the byte slice is the correct length before doing |
| 186 | + // unsafe casts. |
| 187 | + if want, got := sizeofDPMegaflowStats, len(b); want != got { |
| 188 | + return DatapathMegaflowStats{}, fmt.Errorf("unexpected datapath megaflow stats structure size, want %d, got %d", want, got) |
| 189 | + } |
| 190 | + |
| 191 | + s := *(*ovsh.DPMegaflowStats)(unsafe.Pointer(&b[0])) |
| 192 | + |
| 193 | + return DatapathMegaflowStats{ |
| 194 | + MaskHits: s.Mask_hit, |
| 195 | + Masks: s.Masks, |
| 196 | + }, nil |
| 197 | +} |
0 commit comments