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.
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.
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 |
|
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 |
|
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 |
|
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 |
|
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.
|
hdr_packet_calc_inserted_checksum |
|
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.
|
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);
}
}