@@ -2599,3 +2599,135 @@ func TestFilterChainAddDel(t *testing.T) {
25992599 t .Fatal ("Failed to remove qdisc" )
26002600 }
26012601}
2602+
2603+ func TestFilterSampleAddDel (t * testing.T ) {
2604+ minKernelRequired (t , 4 , 11 )
2605+ if _ , err := GenlFamilyGet ("psample" ); err != nil {
2606+ t .Skip ("psample genetlink family unavailable - is CONFIG_PSAMPLE enabled?" )
2607+ }
2608+
2609+ tearDown := setUpNetlinkTest (t )
2610+ defer tearDown ()
2611+ if err := LinkAdd (& Ifb {LinkAttrs {Name : "foo" }}); err != nil {
2612+ t .Fatal (err )
2613+ }
2614+ link , err := LinkByName ("foo" )
2615+ if err != nil {
2616+ t .Fatal (err )
2617+ }
2618+ if err := LinkSetUp (link ); err != nil {
2619+ t .Fatal (err )
2620+ }
2621+
2622+ qdisc := & Ingress {
2623+ QdiscAttrs : QdiscAttrs {
2624+ LinkIndex : link .Attrs ().Index ,
2625+ Handle : MakeHandle (0xffff , 0 ),
2626+ Parent : HANDLE_INGRESS ,
2627+ },
2628+ }
2629+ if err := QdiscAdd (qdisc ); err != nil {
2630+ t .Fatal (err )
2631+ }
2632+ qdiscs , err := SafeQdiscList (link )
2633+ if err != nil {
2634+ t .Fatal (err )
2635+ }
2636+
2637+ found := false
2638+ for _ , v := range qdiscs {
2639+ if _ , ok := v .(* Ingress ); ok {
2640+ found = true
2641+ break
2642+ }
2643+ }
2644+ if ! found {
2645+ t .Fatal ("Qdisc is the wrong type" )
2646+ }
2647+
2648+ sample := NewSampleAction ()
2649+ sample .Group = 7
2650+ sample .Rate = 12
2651+ sample .TruncSize = 200
2652+
2653+ classId := MakeHandle (1 , 1 )
2654+ filter := & MatchAll {
2655+ FilterAttrs : FilterAttrs {
2656+ LinkIndex : link .Attrs ().Index ,
2657+ Parent : MakeHandle (0xffff , 0 ),
2658+ Priority : 1 ,
2659+ Protocol : unix .ETH_P_ALL ,
2660+ },
2661+ ClassId : classId ,
2662+ Actions : []Action {
2663+ sample ,
2664+ },
2665+ }
2666+
2667+ if err := FilterAdd (filter ); err != nil {
2668+ t .Fatal (err )
2669+ }
2670+
2671+ filters , err := FilterList (link , MakeHandle (0xffff , 0 ))
2672+ if err != nil {
2673+ t .Fatal (err )
2674+ }
2675+ if len (filters ) != 1 {
2676+ t .Fatal ("Failed to add filter" )
2677+ }
2678+ mf , ok := filters [0 ].(* MatchAll )
2679+ if ! ok {
2680+ t .Fatal ("Filter is the wrong type" )
2681+ }
2682+
2683+ if len (mf .Actions ) < 1 {
2684+ t .Fatalf ("Too few Actions in filter" )
2685+ }
2686+ if mf .ClassId != classId {
2687+ t .Fatalf ("ClassId of the filter is the wrong value" )
2688+ }
2689+
2690+ lsample , ok := mf .Actions [0 ].(* SampleAction )
2691+ if ! ok {
2692+ t .Fatal ("Unable to find sample action" )
2693+ }
2694+ if lsample .Group != sample .Group {
2695+ t .Fatalf ("Inconsistent sample action group" )
2696+ }
2697+ if lsample .Rate != sample .Rate {
2698+ t .Fatalf ("Inconsistent sample action rate" )
2699+ }
2700+ if lsample .TruncSize != sample .TruncSize {
2701+ t .Fatalf ("Inconsistent sample truncation size" )
2702+ }
2703+
2704+ if err := FilterDel (filter ); err != nil {
2705+ t .Fatal (err )
2706+ }
2707+ filters , err = FilterList (link , MakeHandle (0xffff , 0 ))
2708+ if err != nil {
2709+ t .Fatal (err )
2710+ }
2711+ if len (filters ) != 0 {
2712+ t .Fatal ("Failed to remove filter" )
2713+ }
2714+
2715+ if err := QdiscDel (qdisc ); err != nil {
2716+ t .Fatal (err )
2717+ }
2718+ qdiscs , err = SafeQdiscList (link )
2719+ if err != nil {
2720+ t .Fatal (err )
2721+ }
2722+
2723+ found = false
2724+ for _ , v := range qdiscs {
2725+ if _ , ok := v .(* Ingress ); ok {
2726+ found = true
2727+ break
2728+ }
2729+ }
2730+ if found {
2731+ t .Fatal ("Failed to remove qdisc" )
2732+ }
2733+ }
0 commit comments