Header MQTT Protocol

Hello. I’m new to P4 and I’m trying to extract information from the MQTT header.

Initially, I would just like to work with the fixed part, of 2 bytes. I defined:

header mqtt_t {
bit<4> type;
bit<4> flags;
bit<8> remlen;
}

And in the parser:

state tcp {
packet.extract(hdr.tcp);
transition select(hdr.tcp.dstPort) {
TYPE_MQTT: parse_mqtt;
default: accept;
}
}

 state parse_mqtt {
     packet.extract(hdr.mqtt);
     transition accept;
 }

However, when using the values of type and flags for comparison, filtering by message type, for example, does not work.

Can someone help me?

It would help if you explain precisely what you mean by “does not work”. What packets did you try? What happened when your P4 program processed them?

I have a guess what might have gone wrong, but only a guess. TCP is a byte stream protocol. The sending host’s TCP stack decides, among all of the bytes written to the socket by the sending application, how to divide them up into separate packets. In some cases, the host kernel makes an initial decision, and then the sending NIC implements a feature like TCP segmentation offload, which further divides up-to-64KByte sequences of bytes into separate IP packets on the wire.

Thus, application-level headers can appear anywhere in the payload of an individual TCP packet. Not only immediately after the TCP header.

P4 is primarily a packet-by-packet processing language. It wasn’t really designed to easily be a “reassemble the original byte sequence of the TCP stream, and then do fancy analysis on the byte stream” kind of language. I am not saying there aren’t ways to do limited kinds of scans for data within the payload, but they tend to be limited to scanning a fixed small number of bytes in the TCP payload, up to whatever the target supports as a single fixed-length “header”.

Hello Andy.

Sorry for the delayed response but I managed to get the desired behavior.

I had not been able to verify which values were being read in the fields of the MQTT header correctly.

I decided to define specific sizes of the MQTT header fields, with topic and message always the same size, for example, and it worked.

But anyway thank you very much!