From eeca6ddad973f68c631d329ff378fe38cdd80adc Mon Sep 17 00:00:00 2001 From: chantra Date: Thu, 12 Nov 2015 09:55:13 -0500 Subject: [PATCH] [qdisc] refactor all commands to use qdiscModify function --- qdisc_linux.go | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/qdisc_linux.go b/qdisc_linux.go index ecdec6f..d9a8b17 100644 --- a/qdisc_linux.go +++ b/qdisc_linux.go @@ -13,42 +13,37 @@ import ( // QdiscDel will delete a qdisc from the system. // Equivalent to: `tc qdisc del $qdisc` func QdiscDel(qdisc Qdisc) error { - req := nl.NewNetlinkRequest(syscall.RTM_DELQDISC, syscall.NLM_F_ACK) - base := qdisc.Attrs() - msg := &nl.TcMsg{ - Family: nl.FAMILY_ALL, - Ifindex: int32(base.LinkIndex), - Handle: base.Handle, - Parent: base.Parent, - } - req.AddData(msg) - - _, err := req.Execute(syscall.NETLINK_ROUTE, 0) - return err + return qdiscModify(syscall.RTM_DELQDISC, 0, qdisc) } -// QdiscChanf will change a qdisc in place +// QdiscChange will change a qdisc in place // Equivalent to: `tc qdisc change $qdisc` // The parent and handle MUST NOT be changed. func QdiscChange(qdisc Qdisc) error { - return qdiscModify(qdisc, 0) + return qdiscModify(syscall.RTM_NEWQDISC, 0, qdisc) } // QdiscReplace will replace a qdisc to the system. // Equivalent to: `tc qdisc replace $qdisc` // The handle MUST change. func QdiscReplace(qdisc Qdisc) error { - return qdiscModify(qdisc, syscall.NLM_F_CREATE|syscall.NLM_F_REPLACE) + return qdiscModify( + syscall.RTM_NEWQDISC, + syscall.NLM_F_CREATE|syscall.NLM_F_REPLACE, + qdisc) } // QdiscAdd will add a qdisc to the system. // Equivalent to: `tc qdisc add $qdisc` func QdiscAdd(qdisc Qdisc) error { - return qdiscModify(qdisc, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL) + return qdiscModify( + syscall.RTM_NEWQDISC, + syscall.NLM_F_CREATE|syscall.NLM_F_EXCL, + qdisc) } -func qdiscModify(qdisc Qdisc, flags int) error { - req := nl.NewNetlinkRequest(syscall.RTM_NEWQDISC, flags|syscall.NLM_F_ACK) +func qdiscModify(cmd, flags int, qdisc Qdisc) error { + req := nl.NewNetlinkRequest(cmd, flags|syscall.NLM_F_ACK) base := qdisc.Attrs() msg := &nl.TcMsg{ Family: nl.FAMILY_ALL, @@ -58,8 +53,11 @@ func qdiscModify(qdisc Qdisc, flags int) error { } req.AddData(msg) - if err := qdiscPayload(req, qdisc); err != nil { - return err + // When deleting don't bother building the rest of the netlink payload + if cmd != syscall.RTM_DELQDISC { + if err := qdiscPayload(req, qdisc); err != nil { + return err + } } _, err := req.Execute(syscall.NETLINK_ROUTE, 0)