Set Meter Rates in Controller

Hey everyone So I want to set the flow rates in meter in my controller.
Does anyone have any idea how to do it?

I don’t know if there is already a python implementation for it but you could implement the grpc call yourself similar to the WriteTableEntry method in tutorials/utils/p4runtime_lib/switch.py at master · p4lang/tutorials · GitHub

See P4Runtime Specification

In section 9.4

(Assuming you are using bmv2 and p4runtime)

Hey, @SteffenLindner I want to know if I want to create ‘n’ meters in my p4 switch and I want to set different rates for each how should I do it.

Like I know I can use indexed metering but I am not sure how to proceed with it.

like if this creates a meter parameter

meter(32w16384, MeterType.packets) my_meter;

and this set the action for it

action m_action(bit<32> meter_index) {
        my_meter.execute_meter<bit<32>>(meter_index, meta.meter_tag);
    }

So will multiple meters look like this
meter(32w16384, MeterType.packets) my_meter1;
meter(32w16384, MeterType.packets) my_meter2;
meter(32w16384, MeterType.packets) my_metern;

and if yes then how will the acion change?

Also my_meter1 has a width of 32 bits and a maximum rate of 16,384 packets.
so is it related to the pir,cir or not?

If you want to apply multiple meters to the same packet, then you need multiple meter instances. If you only want to apply a single meter instance to a packet, but with different rates (depending on some information, eg packet type) then a single meter instance is enough and you just need to configure different rates for the different meter indices. In that case, you distinguish different meter „instances“ through the meter index.

The 32w16384 is the number of meter entries that you can configure (see p4c/p4include/v1model.p4 at b6ad83a391331e6456dd2422efbe934e996765b1 · p4lang/p4c · GitHub ) and has nothing to do with the bucket size (aka burst). The cir, pir, etc need to be configured by the control plane.

I actually want to classify the packets like some packets need to be send with different rates and some packets with other rates.

If you only want to apply a single meter instance to a packet, but with different rates (depending on some information, eg packet type) then a single meter instance is enough and you just need to configure different rates for the different meter indices. In that case, you distinguish different meter „instances“ through the meter index.

In this I am not getting how to use the multiple instances through the meter index so if you can explain in simple terms on how it works.

Let’s assume you want to meter ipv4 and ipv6 packets with different rates.

Then you have a table that matches on the ethertype and has a single action do_meter that uses your meter instance. The action has a parameter index (as in your example). Your control plane writes a table entry for ethertype 0x800 (ipv4) with index 1, and a entry for ethertype 0x86DD (ipv6) with index 2.

Further, the control plane configures the meter instance at entry 1 with the rate for ipv4, and at entry 2 for ipv6.

You only need a single meter instance and distinguish different rates through the index.

Or do you want to classify packets based on their rate ? If so, please explain your problem statement in more detail.

(I’m on mobile atm that’s why there are no complete code examples)

See I am trying to send the packets the to the controller then apply some logic and based on the logic output I want to send the packets to the switch then forward through the ports with different rates lets suppose 3-4 different rates so what would be the simplest solution for this.

And thanks for the answers even when you are on mobile.

If possible when you are on the PC can explain with a simple code or just give me the gist of it.

I‘d say a simple approach would be to equip the packets with an additional header on your controller that indicates the index that should be used at the meter instance. Then you can work with that header in the data plane as usual.

In the dataplane you basically do as described and instead of matching on the ethertype you match on your new header. Or even simpler, you don’t need any table and use your new header directly to index the meter. Eg your controller adds a header with a field index, and you directly use that field to access your meter.

What order of magnitude of the rate are we talking about? Your controller is only able to handle very low data rates when you send EVERY packet to the controller. So depending on your exact use case, this approach might not be feasible.