queue.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2018 Google LLC. All Rights Reserved.
  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. package expr
  15. import (
  16. "encoding/binary"
  17. "github.com/google/nftables/binaryutil"
  18. "github.com/mdlayher/netlink"
  19. "golang.org/x/sys/unix"
  20. )
  21. type QueueAttribute uint16
  22. type QueueFlag uint16
  23. // Possible QueueAttribute values
  24. const (
  25. QueueNum QueueAttribute = unix.NFTA_QUEUE_NUM
  26. QueueTotal QueueAttribute = unix.NFTA_QUEUE_TOTAL
  27. QueueFlags QueueAttribute = unix.NFTA_QUEUE_FLAGS
  28. // TODO: get into x/sys/unix
  29. QueueFlagBypass QueueFlag = 0x01
  30. QueueFlagFanout QueueFlag = 0x02
  31. QueueFlagMask QueueFlag = 0x03
  32. )
  33. type Queue struct {
  34. Num uint16
  35. Total uint16
  36. Flag QueueFlag
  37. }
  38. func (e *Queue) marshal(fam byte) ([]byte, error) {
  39. if e.Total == 0 {
  40. e.Total = 1 // The total default value is 1
  41. }
  42. data, err := netlink.MarshalAttributes([]netlink.Attribute{
  43. {Type: unix.NFTA_QUEUE_NUM, Data: binaryutil.BigEndian.PutUint16(e.Num)},
  44. {Type: unix.NFTA_QUEUE_TOTAL, Data: binaryutil.BigEndian.PutUint16(e.Total)},
  45. {Type: unix.NFTA_QUEUE_FLAGS, Data: binaryutil.BigEndian.PutUint16(uint16(e.Flag))},
  46. })
  47. if err != nil {
  48. return nil, err
  49. }
  50. return netlink.MarshalAttributes([]netlink.Attribute{
  51. {Type: unix.NFTA_EXPR_NAME, Data: []byte("queue\x00")},
  52. {Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: data},
  53. })
  54. }
  55. func (e *Queue) unmarshal(fam byte, data []byte) error {
  56. ad, err := netlink.NewAttributeDecoder(data)
  57. if err != nil {
  58. return err
  59. }
  60. ad.ByteOrder = binary.BigEndian
  61. for ad.Next() {
  62. switch ad.Type() {
  63. case unix.NFTA_QUEUE_NUM:
  64. e.Num = ad.Uint16()
  65. case unix.NFTA_QUEUE_TOTAL:
  66. e.Total = ad.Uint16()
  67. case unix.NFTA_QUEUE_FLAGS:
  68. e.Flag = QueueFlag(ad.Uint16())
  69. }
  70. }
  71. return ad.Err()
  72. }