Rate This Document
Findability
Accuracy
Completeness
Readability

Interface Details

The hydra_core.hdr architecture file provides the packet_in extern object for packet parsing.

extern packet_in {
    void extract<T>(out T hdr, in bit<32> type, in layers_e layer);
    void extractOpt<T>(out T hdr);
    void advance(in bit<32> sizeInBits);
}

Table 1 describes the functions.

Table 1 Function description

Function Name

Input Parameter

Effect

Example

extract

hdr: Packet header defined in the header structure.

Extracts a protocol field from a packet based on the definition in the header structure.

packet.extract(hdr.ethernet, TYPE_ETH, layer.l2);

type: Packet type value. Basic packet types are defined in hydra_core.hdr.

layer: Protocol layer. Its enumeration values are provided in hydra_core.hdr.

extractOpt

hdr: Optional header fields defined in the struct. For example, GRE optional fields.

Extracts optional fields from a packet.

if (hdr.gre.C == 1w1) {

packet.extractOpt(hdr.gre_opt_c);

}

advance

sizeInBits: Length to move, in bits.

Moves the pointer to the current position in the packet parsing process from lower address space to higher address space.

packet.advance(((bit<32>) hdr.inner_ipv4.ihl - 32w5) * 32w32);

hdr_last_layer_get

/

layers_e hdr_last_layer_get();

Obtains the layer of the last packet header parsed by the hardware parser.

hdr_last_layer_type_get

/

bit<16> hdr_last_layer_type_get();

Obtains the protocol type of the last packet header parsed by the hardware parser.

The hydra_externs.hdr architecture file provides various extern functions. You can combine different extern functions in a custom action to implement tunnel encapsulation/decapsulation and packet modification.

Table 2 Function description

Function Name

Parameter

Signature

Description

hdr_packet_delete_from_head

len: Length of the packet header to be deleted.

extern void hdr_packet_delete_from_head(in bit<8> len);

Deletes a packet header from the original packet header, typically used for tunnel decapsulation. The total deleted length cannot exceed the original packet header length and must be no more than 255 bytes.

hdr_packet_insert_from_head

hdr: Packet header to be inserted. The data type is header.

extern void hdr_packet_insert_from_head<T>(in T hdr);

Inserts a packet header into the original packet header, typically used for tunnel encapsulation. For a packet sent to the network, the inserted packet header length cannot exceed 128 bytes. For a packet sent to the host, the inserted packet header length cannot exceed 64 bytes.

hdr_packet_modify

  • field: Packet header field to be modified.
  • value: New value.
extern void hdr_packet_modify<T>(in T field, in T value);

Modifies the specified field in the packet header.

hdr_packet_calc_checksum_incr

  • modified_field: Packet field to be modified.
  • cs_field: Checksum field to be modified.
  • params: Structure used for parameter configuration. Its members are as follows:
    • old_val: Old value of the field to be modified in scenarios where packet header fields are modified and an incremental checksum update is required.
    • new_val: New value of the field.
    • old_cs: Old checksum of the packet header to be modified in scenarios where packet header fields are modified and an incremental checksum update is required.
    • cs_alg: Checksum algorithm. The enumeration values are provided in hydra_externs.hdr.
extern void hdr_packet_calc_checksum_incr<M,C>(in M modified_field,in C cs_field,in packet_incr_cs_param params);

Performs an incremental checksum update for the original packet header.

Example:

Update the checksum after modifying the IPv4 TTL field.

packet_incr_cs_param incr_checksum_para;
incr_checksum_para.old_val = (bit<32>)hdr.ipv4.ttl; // Old value of the field to be modified
incr_checksum_para.new_val = (bit<32>)(hdr.ipv4.ttl - 1); // New value
incr_checksum_para.old_cs = hdr.ipv4.hdr_checksum; // Old checksum of the packet header to be modified
incr_checksum_para.cs_alg = RFC.RFC_1624; // Use the RFC_1624 algorithm.
hdr_packet_modify(hdr.ipv4.ttl, hdr.ipv4.ttl - 1); // Modify the ttl field first.
hdr_packet_calc_checksum_incr(hdr.ipv4.ttl,hdr.ipv4.hdr_checksum,incr_checksum_para); // Update the checksum field.

hdr_packet_calc_inserted_checksum_incr

  • The meanings of the first three parameters are the same as those in the hdr_packet_calc_checksum_incr interface.
  • offset: Offset of the protocol header to be calculated within the inserted packet header.
extern void hdr_packet_calc_inserted_checksum_incr<M, C>(in M modified_field,in C cs_field,in packet_incr_cs_param param,in bit<32> offset);

Performs an incremental checksum update for the inserted packet header. The offset cannot exceed the total length of the inserted packet header.

hdr_packet_calc_checksum

  • cs_field: Its meaning is the same as that in hdr_packet_calc_checksum_incr.
  • param: Structure used for parameter configuration. Its members are as follows:
    • init_value: Initial checksum of the original packet header. It should be set to 0.
    • mask: Specifies the fields in the original packet header to be used for checksum calculation. The mask length is 64 bits, each corresponding to two consecutive bytes in the packet header (total packet header length: 128 bytes). A mask bit of 0 indicates that the corresponding packet header bytes will be included in the calculation. For example, to mask the IPv4 checksum field (at byte 11), set mask to 0x0400000000000000.
    • cs_alg: Checksum algorithm. The enumeration values are provided in hydra_externs.hdr.
extern void hdr_packet_calc_checksum<T>(in T cs_field,in packet_checksum_param param);

Performs a full checksum update for the original packet header.

  • This interface does not support checksum calculation for protocols that include a pseudo-header, such as TCP and UDP.
  • This interface can be called only after all modifications to the packet header fields are completed.

hdr_packet_calc_inserted_checksum

  • cs_field and param: Their meanings are the same as those in hdr_packet_calc_checksum.
  • layer_offset: Offset of the protocol header to be calculated within the inserted packet header.
  • layer_len: Length of the protocol header to be calculated.
extern void hdr_packet_calc_inserted_checksum<T>(in T cs_field,in  packet_checksum_param param,in bit<32> layer_offset,in bit<32>layer_len);

Performs a full checksum update for the inserted packet header.

  • When calculating the checksum for an inserted packet header, the calculation sequence must strictly follow the order from the outer layer to the inner layer.
  • This interface does not support checksum calculation for protocols that include a pseudo-header, such as TCP and UDP.

hdr_ovs_pipeline_action_upcall

/

extern void hdr_ovs_pipeline_action_upcall();

Performs the upcall operation. Note that this operation can be used only in the pre-control process, not in the post-control process.

hdr_ovs_pipeline_action_drop

/

extern void
hdr_ovs_pipeline_action_drop();

Drops a packet. This operation can be used in both the pre-control process and post-control process.

hdr_input_port_id_get

/

extern bit<16> hdr_input_port_id_get();

Obtains the input port ID of the current packet.

Hydra provides the USER_LOG macro in the hydra_log.hdr architecture file for log printing.

#define USER_LOG(type,fmt,args...)hydra_log_##type(FILE_ID,__LINE__,0,fmt,##args)
  • type: Log level. Value options: ERR, WARN, INFO, and DEBUG.
  • fmt: Log information. It is a character string consisting of a maximum of 128 characters.
  • args: Variadic parameter list. A maximum of four parameters can be printed. The placeholder is 0x%x.

Application Scope

  • state logic of the parser
  • action logic
  • apply logic of a control

Example

control MyPrePipe(in headers hdr) {
    apply {
        USER_LOG(ERR, "[DSL][MyPrePipe]hdr.ipv4.ttl  0x%x\n", hdr.ipv4.ttl);
    }
}