link.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. package netlink
  2. import (
  3. "fmt"
  4. "net"
  5. "syscall"
  6. )
  7. // Link represents a link device from netlink. Shared link attributes
  8. // like name may be retrieved using the Attrs() method. Unique data
  9. // can be retrieved by casting the object to the proper type.
  10. type Link interface {
  11. Attrs() *LinkAttrs
  12. Type() string
  13. }
  14. type (
  15. NsPid int
  16. NsFd int
  17. )
  18. // LinkAttrs represents data shared by most link types
  19. type LinkAttrs struct {
  20. Index int
  21. MTU int
  22. TxQLen int // Transmit Queue Length
  23. Name string
  24. HardwareAddr net.HardwareAddr
  25. Flags net.Flags
  26. ParentIndex int // index of the parent link device
  27. MasterIndex int // must be the index of a bridge
  28. Namespace interface{} // nil | NsPid | NsFd
  29. Alias string
  30. }
  31. // NewLinkAttrs returns LinkAttrs structure filled with default values
  32. func NewLinkAttrs() LinkAttrs {
  33. return LinkAttrs{
  34. TxQLen: -1,
  35. }
  36. }
  37. // Device links cannot be created via netlink. These links
  38. // are links created by udev like 'lo' and 'etho0'
  39. type Device struct {
  40. LinkAttrs
  41. }
  42. func (device *Device) Attrs() *LinkAttrs {
  43. return &device.LinkAttrs
  44. }
  45. func (device *Device) Type() string {
  46. return "device"
  47. }
  48. // Dummy links are dummy ethernet devices
  49. type Dummy struct {
  50. LinkAttrs
  51. }
  52. func (dummy *Dummy) Attrs() *LinkAttrs {
  53. return &dummy.LinkAttrs
  54. }
  55. func (dummy *Dummy) Type() string {
  56. return "dummy"
  57. }
  58. // Ifb links are advanced dummy devices for packet filtering
  59. type Ifb struct {
  60. LinkAttrs
  61. }
  62. func (ifb *Ifb) Attrs() *LinkAttrs {
  63. return &ifb.LinkAttrs
  64. }
  65. func (ifb *Ifb) Type() string {
  66. return "ifb"
  67. }
  68. // Bridge links are simple linux bridges
  69. type Bridge struct {
  70. LinkAttrs
  71. }
  72. func (bridge *Bridge) Attrs() *LinkAttrs {
  73. return &bridge.LinkAttrs
  74. }
  75. func (bridge *Bridge) Type() string {
  76. return "bridge"
  77. }
  78. // Vlan links have ParentIndex set in their Attrs()
  79. type Vlan struct {
  80. LinkAttrs
  81. VlanId int
  82. }
  83. func (vlan *Vlan) Attrs() *LinkAttrs {
  84. return &vlan.LinkAttrs
  85. }
  86. func (vlan *Vlan) Type() string {
  87. return "vlan"
  88. }
  89. type MacvlanMode uint16
  90. const (
  91. MACVLAN_MODE_DEFAULT MacvlanMode = iota
  92. MACVLAN_MODE_PRIVATE
  93. MACVLAN_MODE_VEPA
  94. MACVLAN_MODE_BRIDGE
  95. MACVLAN_MODE_PASSTHRU
  96. MACVLAN_MODE_SOURCE
  97. )
  98. // Macvlan links have ParentIndex set in their Attrs()
  99. type Macvlan struct {
  100. LinkAttrs
  101. Mode MacvlanMode
  102. }
  103. func (macvlan *Macvlan) Attrs() *LinkAttrs {
  104. return &macvlan.LinkAttrs
  105. }
  106. func (macvlan *Macvlan) Type() string {
  107. return "macvlan"
  108. }
  109. // Macvtap - macvtap is a virtual interfaces based on macvlan
  110. type Macvtap struct {
  111. Macvlan
  112. }
  113. func (macvtap Macvtap) Type() string {
  114. return "macvtap"
  115. }
  116. type TuntapMode uint16
  117. const (
  118. TUNTAP_MODE_TUN TuntapMode = syscall.IFF_TUN
  119. TUNTAP_MODE_TAP TuntapMode = syscall.IFF_TAP
  120. )
  121. // Tuntap links created via /dev/tun/tap, but can be destroyed via netlink
  122. type Tuntap struct {
  123. LinkAttrs
  124. Mode TuntapMode
  125. }
  126. func (tuntap *Tuntap) Attrs() *LinkAttrs {
  127. return &tuntap.LinkAttrs
  128. }
  129. func (tuntap *Tuntap) Type() string {
  130. return "tuntap"
  131. }
  132. // Veth devices must specify PeerName on create
  133. type Veth struct {
  134. LinkAttrs
  135. PeerName string // veth on create only
  136. }
  137. func (veth *Veth) Attrs() *LinkAttrs {
  138. return &veth.LinkAttrs
  139. }
  140. func (veth *Veth) Type() string {
  141. return "veth"
  142. }
  143. // GenericLink links represent types that are not currently understood
  144. // by this netlink library.
  145. type GenericLink struct {
  146. LinkAttrs
  147. LinkType string
  148. }
  149. func (generic *GenericLink) Attrs() *LinkAttrs {
  150. return &generic.LinkAttrs
  151. }
  152. func (generic *GenericLink) Type() string {
  153. return generic.LinkType
  154. }
  155. type Vxlan struct {
  156. LinkAttrs
  157. VxlanId int
  158. VtepDevIndex int
  159. SrcAddr net.IP
  160. Group net.IP
  161. TTL int
  162. TOS int
  163. Learning bool
  164. Proxy bool
  165. RSC bool
  166. L2miss bool
  167. L3miss bool
  168. UDPCSum bool
  169. NoAge bool
  170. GBP bool
  171. Age int
  172. Limit int
  173. Port int
  174. PortLow int
  175. PortHigh int
  176. }
  177. func (vxlan *Vxlan) Attrs() *LinkAttrs {
  178. return &vxlan.LinkAttrs
  179. }
  180. func (vxlan *Vxlan) Type() string {
  181. return "vxlan"
  182. }
  183. type IPVlanMode uint16
  184. const (
  185. IPVLAN_MODE_L2 IPVlanMode = iota
  186. IPVLAN_MODE_L3
  187. IPVLAN_MODE_MAX
  188. )
  189. type IPVlan struct {
  190. LinkAttrs
  191. Mode IPVlanMode
  192. }
  193. func (ipvlan *IPVlan) Attrs() *LinkAttrs {
  194. return &ipvlan.LinkAttrs
  195. }
  196. func (ipvlan *IPVlan) Type() string {
  197. return "ipvlan"
  198. }
  199. // BondMode type
  200. type BondMode int
  201. func (b BondMode) String() string {
  202. s, ok := bondModeToString[b]
  203. if !ok {
  204. return fmt.Sprintf("BondMode(%d)", b)
  205. }
  206. return s
  207. }
  208. // StringToBondMode returns bond mode, or uknonw is the s is invalid.
  209. func StringToBondMode(s string) BondMode {
  210. mode, ok := StringToBondModeMap[s]
  211. if !ok {
  212. return BOND_MODE_UNKNOWN
  213. }
  214. return mode
  215. }
  216. // Possible BondMode
  217. const (
  218. BOND_MODE_802_3AD BondMode = iota
  219. BOND_MODE_BALANCE_RR
  220. BOND_MODE_ACTIVE_BACKUP
  221. BOND_MODE_BALANCE_XOR
  222. BOND_MODE_BROADCAST
  223. BOND_MODE_BALANCE_TLB
  224. BOND_MODE_BALANCE_ALB
  225. BOND_MODE_UNKNOWN
  226. )
  227. var bondModeToString = map[BondMode]string{
  228. BOND_MODE_802_3AD: "802.3ad",
  229. BOND_MODE_BALANCE_RR: "balance-rr",
  230. BOND_MODE_ACTIVE_BACKUP: "active-backup",
  231. BOND_MODE_BALANCE_XOR: "balance-xor",
  232. BOND_MODE_BROADCAST: "broadcast",
  233. BOND_MODE_BALANCE_TLB: "balance-tlb",
  234. BOND_MODE_BALANCE_ALB: "balance-alb",
  235. }
  236. var StringToBondModeMap = map[string]BondMode{
  237. "802.3ad": BOND_MODE_802_3AD,
  238. "balance-rr": BOND_MODE_BALANCE_RR,
  239. "active-backup": BOND_MODE_ACTIVE_BACKUP,
  240. "balance-xor": BOND_MODE_BALANCE_XOR,
  241. "broadcast": BOND_MODE_BROADCAST,
  242. "balance-tlb": BOND_MODE_BALANCE_TLB,
  243. "balance-alb": BOND_MODE_BALANCE_ALB,
  244. }
  245. // BondArpValidate type
  246. type BondArpValidate int
  247. // Possible BondArpValidate value
  248. const (
  249. BOND_ARP_VALIDATE_NONE BondArpValidate = iota
  250. BOND_ARP_VALIDATE_ACTIVE
  251. BOND_ARP_VALIDATE_BACKUP
  252. BOND_ARP_VALIDATE_ALL
  253. )
  254. // BondPrimaryReselect type
  255. type BondPrimaryReselect int
  256. // Possible BondPrimaryReselect value
  257. const (
  258. BOND_PRIMARY_RESELECT_ALWAYS BondPrimaryReselect = iota
  259. BOND_PRIMARY_RESELECT_BETTER
  260. BOND_PRIMARY_RESELECT_FAILURE
  261. )
  262. // BondArpAllTargets type
  263. type BondArpAllTargets int
  264. // Possible BondArpAllTargets value
  265. const (
  266. BOND_ARP_ALL_TARGETS_ANY BondArpAllTargets = iota
  267. BOND_ARP_ALL_TARGETS_ALL
  268. )
  269. // BondFailOverMac type
  270. type BondFailOverMac int
  271. // Possible BondFailOverMac value
  272. const (
  273. BOND_FAIL_OVER_MAC_NONE BondFailOverMac = iota
  274. BOND_FAIL_OVER_MAC_ACTIVE
  275. BOND_FAIL_OVER_MAC_FOLLOW
  276. )
  277. // BondXmitHashPolicy type
  278. type BondXmitHashPolicy int
  279. func (b BondXmitHashPolicy) String() string {
  280. s, ok := bondXmitHashPolicyToString[b]
  281. if !ok {
  282. return fmt.Sprintf("XmitHashPolicy(%d)", b)
  283. }
  284. return s
  285. }
  286. // StringToBondXmitHashPolicy returns bond lacp arte, or uknonw is the s is invalid.
  287. func StringToBondXmitHashPolicy(s string) BondXmitHashPolicy {
  288. lacp, ok := StringToBondXmitHashPolicyMap[s]
  289. if !ok {
  290. return BOND_XMIT_HASH_POLICY_UNKNOWN
  291. }
  292. return lacp
  293. }
  294. // Possible BondXmitHashPolicy value
  295. const (
  296. BOND_XMIT_HASH_POLICY_LAYER2 BondXmitHashPolicy = iota
  297. BOND_XMIT_HASH_POLICY_LAYER3_4
  298. BOND_XMIT_HASH_POLICY_LAYER2_3
  299. BOND_XMIT_HASH_POLICY_ENCAP2_3
  300. BOND_XMIT_HASH_POLICY_ENCAP3_4
  301. BOND_XMIT_HASH_POLICY_UNKNOWN
  302. )
  303. var bondXmitHashPolicyToString = map[BondXmitHashPolicy]string{
  304. BOND_XMIT_HASH_POLICY_LAYER2: "layer2",
  305. BOND_XMIT_HASH_POLICY_LAYER3_4: "layer3+4",
  306. BOND_XMIT_HASH_POLICY_LAYER2_3: "layer2+3",
  307. BOND_XMIT_HASH_POLICY_ENCAP2_3: "encap2+3",
  308. BOND_XMIT_HASH_POLICY_ENCAP3_4: "encap3+4",
  309. }
  310. var StringToBondXmitHashPolicyMap = map[string]BondXmitHashPolicy{
  311. "layer2": BOND_XMIT_HASH_POLICY_LAYER2,
  312. "layer3+4": BOND_XMIT_HASH_POLICY_LAYER3_4,
  313. "layer2+3": BOND_XMIT_HASH_POLICY_LAYER2_3,
  314. "encap2+3": BOND_XMIT_HASH_POLICY_ENCAP2_3,
  315. "encap3+4": BOND_XMIT_HASH_POLICY_ENCAP3_4,
  316. }
  317. // BondLacpRate type
  318. type BondLacpRate int
  319. func (b BondLacpRate) String() string {
  320. s, ok := bondLacpRateToString[b]
  321. if !ok {
  322. return fmt.Sprintf("LacpRate(%d)", b)
  323. }
  324. return s
  325. }
  326. // StringToBondLacpRate returns bond lacp arte, or uknonw is the s is invalid.
  327. func StringToBondLacpRate(s string) BondLacpRate {
  328. lacp, ok := StringToBondLacpRateMap[s]
  329. if !ok {
  330. return BOND_LACP_RATE_UNKNOWN
  331. }
  332. return lacp
  333. }
  334. // Possible BondLacpRate value
  335. const (
  336. BOND_LACP_RATE_SLOW BondLacpRate = iota
  337. BOND_LACP_RATE_FAST
  338. BOND_LACP_RATE_UNKNOWN
  339. )
  340. var bondLacpRateToString = map[BondLacpRate]string{
  341. BOND_LACP_RATE_SLOW: "slow",
  342. BOND_LACP_RATE_FAST: "fast",
  343. }
  344. var StringToBondLacpRateMap = map[string]BondLacpRate{
  345. "slow": BOND_LACP_RATE_SLOW,
  346. "fast": BOND_LACP_RATE_FAST,
  347. }
  348. // BondAdSelect type
  349. type BondAdSelect int
  350. // Possible BondAdSelect value
  351. const (
  352. BOND_AD_SELECT_STABLE BondAdSelect = iota
  353. BOND_AD_SELECT_BANDWIDTH
  354. BOND_AD_SELECT_COUNT
  355. )
  356. // BondAdInfo
  357. type BondAdInfo struct {
  358. AggregatorId int
  359. NumPorts int
  360. ActorKey int
  361. PartnerKey int
  362. PartnerMac net.HardwareAddr
  363. }
  364. // Bond representation
  365. type Bond struct {
  366. LinkAttrs
  367. Mode BondMode
  368. ActiveSlave int
  369. Miimon int
  370. UpDelay int
  371. DownDelay int
  372. UseCarrier int
  373. ArpInterval int
  374. ArpIpTargets []net.IP
  375. ArpValidate BondArpValidate
  376. ArpAllTargets BondArpAllTargets
  377. Primary int
  378. PrimaryReselect BondPrimaryReselect
  379. FailOverMac BondFailOverMac
  380. XmitHashPolicy BondXmitHashPolicy
  381. ResendIgmp int
  382. NumPeerNotif int
  383. AllSlavesActive int
  384. MinLinks int
  385. LpInterval int
  386. PackersPerSlave int
  387. LacpRate BondLacpRate
  388. AdSelect BondAdSelect
  389. // looking at iproute tool AdInfo can only be retrived. It can't be set.
  390. AdInfo *BondAdInfo
  391. }
  392. func NewLinkBond(atr LinkAttrs) *Bond {
  393. return &Bond{
  394. LinkAttrs: atr,
  395. Mode: -1,
  396. ActiveSlave: -1,
  397. Miimon: -1,
  398. UpDelay: -1,
  399. DownDelay: -1,
  400. UseCarrier: -1,
  401. ArpInterval: -1,
  402. ArpIpTargets: nil,
  403. ArpValidate: -1,
  404. ArpAllTargets: -1,
  405. Primary: -1,
  406. PrimaryReselect: -1,
  407. FailOverMac: -1,
  408. XmitHashPolicy: -1,
  409. ResendIgmp: -1,
  410. NumPeerNotif: -1,
  411. AllSlavesActive: -1,
  412. MinLinks: -1,
  413. LpInterval: -1,
  414. PackersPerSlave: -1,
  415. LacpRate: -1,
  416. AdSelect: -1,
  417. }
  418. }
  419. // Flag mask for bond options. Bond.Flagmask must be set to on for option to work.
  420. const (
  421. BOND_MODE_MASK uint64 = 1 << (1 + iota)
  422. BOND_ACTIVE_SLAVE_MASK
  423. BOND_MIIMON_MASK
  424. BOND_UPDELAY_MASK
  425. BOND_DOWNDELAY_MASK
  426. BOND_USE_CARRIER_MASK
  427. BOND_ARP_INTERVAL_MASK
  428. BOND_ARP_VALIDATE_MASK
  429. BOND_ARP_ALL_TARGETS_MASK
  430. BOND_PRIMARY_MASK
  431. BOND_PRIMARY_RESELECT_MASK
  432. BOND_FAIL_OVER_MAC_MASK
  433. BOND_XMIT_HASH_POLICY_MASK
  434. BOND_RESEND_IGMP_MASK
  435. BOND_NUM_PEER_NOTIF_MASK
  436. BOND_ALL_SLAVES_ACTIVE_MASK
  437. BOND_MIN_LINKS_MASK
  438. BOND_LP_INTERVAL_MASK
  439. BOND_PACKETS_PER_SLAVE_MASK
  440. BOND_LACP_RATE_MASK
  441. BOND_AD_SELECT_MASK
  442. )
  443. // Attrs implementation.
  444. func (bond *Bond) Attrs() *LinkAttrs {
  445. return &bond.LinkAttrs
  446. }
  447. // Type implementation fro Vxlan.
  448. func (bond *Bond) Type() string {
  449. return "bond"
  450. }
  451. // GreTap devices must specify LocalIP and RemoteIP on create
  452. type Gretap struct {
  453. LinkAttrs
  454. IKey uint32
  455. OKey uint32
  456. EncapSport uint16
  457. EncapDport uint16
  458. Local net.IP
  459. Remote net.IP
  460. IFlags uint16
  461. OFlags uint16
  462. PMtuDisc uint8
  463. Ttl uint8
  464. Tos uint8
  465. EncapType uint16
  466. EncapFlags uint16
  467. Link uint32
  468. }
  469. func (gretap *Gretap) Attrs() *LinkAttrs {
  470. return &gretap.LinkAttrs
  471. }
  472. func (gretap *Gretap) Type() string {
  473. return "gretap"
  474. }
  475. // iproute2 supported devices;
  476. // vlan | veth | vcan | dummy | ifb | macvlan | macvtap |
  477. // bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan |
  478. // gre | gretap | ip6gre | ip6gretap | vti | nlmon |
  479. // bond_slave | ipvlan