Add Flower ip_proto attribute support

This commit is contained in:
Ivan Kolodyazhny 2022-08-29 18:24:32 +03:00 committed by Alessandro Boch
parent 05506ada9f
commit 130828cd57
3 changed files with 53 additions and 0 deletions

View File

@ -66,6 +66,7 @@ type Flower struct {
EncKeyId uint32
SkipHw bool
SkipSw bool
IPProto *nl.IPProto
Actions []Action
}
@ -131,6 +132,10 @@ func (filter *Flower) encode(parent *nl.RtAttr) error {
if filter.EncKeyId != 0 {
parent.AddRtAttr(nl.TCA_FLOWER_KEY_ENC_KEY_ID, htonl(filter.EncKeyId))
}
if filter.IPProto != nil {
ipproto := *filter.IPProto
parent.AddRtAttr(nl.TCA_FLOWER_KEY_IP_PROTO, ipproto.Serialize())
}
var flags uint32 = 0
if filter.SkipHw {
@ -173,6 +178,10 @@ func (filter *Flower) decode(data []syscall.NetlinkRouteAttr) error {
filter.EncDestPort = ntohs(datum.Value)
case nl.TCA_FLOWER_KEY_ENC_KEY_ID:
filter.EncKeyId = ntohl(datum.Value)
case nl.TCA_FLOWER_KEY_IP_PROTO:
val := new(nl.IPProto)
*val = nl.IPProto(datum.Value[0])
filter.IPProto = val
case nl.TCA_FLOWER_ACT:
tables, err := nl.ParseRouteAttr(datum.Value)
if err != nil {

View File

@ -1770,6 +1770,9 @@ func TestFilterFlowerAddDel(t *testing.T) {
testMask := net.CIDRMask(24, 32)
ipproto := new(nl.IPProto)
*ipproto = nl.IPPROTO_ICMP
filter := &Flower{
FilterAttrs: FilterAttrs{
LinkIndex: link.Attrs().Index,
@ -1788,6 +1791,7 @@ func TestFilterFlowerAddDel(t *testing.T) {
EncSrcIPMask: testMask,
EncDestPort: 8472,
EncKeyId: 1234,
IPProto: ipproto,
Actions: []Action{
&MirredAction{
ActionAttrs: ActionAttrs{
@ -1856,6 +1860,10 @@ func TestFilterFlowerAddDel(t *testing.T) {
if filter.EncDestPort != flower.EncDestPort {
t.Fatalf("Flower EncDestPort doesn't match")
}
if flower.IPProto != nil || *filter.IPProto != *flower.IPProto {
t.Fatalf("Flower IPProto doesn't match")
}
mia, ok := flower.Actions[0].(*MirredAction)
if !ok {
t.Fatal("Unable to find mirred action")

View File

@ -2,7 +2,10 @@ package nl
import (
"encoding/binary"
"fmt"
"unsafe"
"golang.org/x/sys/unix"
)
// LinkLayer
@ -1139,3 +1142,36 @@ func DeserializeTcSfqQoptV1(b []byte) *TcSfqQoptV1 {
func (x *TcSfqQoptV1) Serialize() []byte {
return (*(*[SizeofTcSfqQoptV1]byte)(unsafe.Pointer(x)))[:]
}
// IPProto represents Flower ip_proto attribute
type IPProto uint8
const (
IPPROTO_TCP IPProto = unix.IPPROTO_TCP
IPPROTO_UDP IPProto = unix.IPPROTO_UDP
IPPROTO_SCTP IPProto = unix.IPPROTO_SCTP
IPPROTO_ICMP IPProto = unix.IPPROTO_ICMP
IPPROTO_ICMPV6 IPProto = unix.IPPROTO_ICMPV6
)
func (i IPProto) Serialize() []byte {
arr := make([]byte, 1)
arr[0] = byte(i)
return arr
}
func (i IPProto) String() string {
switch i {
case IPPROTO_TCP:
return "tcp"
case IPPROTO_UDP:
return "udp"
case IPPROTO_SCTP:
return "sctp"
case IPPROTO_ICMP:
return "icmp"
case IPPROTO_ICMPV6:
return "icmpv6"
}
return fmt.Sprintf("%d", i)
}