Skip to content

Commit

Permalink
feat: switch to ethtype field and remove vlan struct
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Nov 28, 2023
1 parent 560505c commit 85af6b0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 55 deletions.
15 changes: 4 additions & 11 deletions include/zenoh-pico/system/link/raweth.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,31 @@

// Ethernet types
#define _ZP_ETH_TYPE_VLAN 0x8100
#define _ZP_ETH_TYPE_CUTOFF 0x600 // Values above are ethertype

// Address Sizes
#define _ZP_MAC_ADDR_LENGTH 6

// Max frame size
#define _ZP_MAX_ETH_FRAME_SIZE 1500

typedef struct {
uint16_t tpid; // Tag ethertype
uint16_t pcp : 3; // Priority code point
uint16_t dei : 1; // Drop eligible indicator
uint16_t vid : 12; // VLAN id
} _zp_vlan_tag_t;

// Ethernet header structure type
typedef struct {
uint8_t dmac[_ZP_MAC_ADDR_LENGTH]; // Destination mac address
uint8_t smac[_ZP_MAC_ADDR_LENGTH]; // Source mac address
uint16_t length; // Size of frame
uint16_t ethtype; // Ethertype of frame
} _zp_eth_header_t;

typedef struct {
uint8_t dmac[_ZP_MAC_ADDR_LENGTH]; // Destination mac address
uint8_t smac[_ZP_MAC_ADDR_LENGTH]; // Source mac address
_zp_vlan_tag_t tag;
uint16_t length; // Size of frame
uint16_t tag; // Vlan tag
uint16_t ethtype; // Ethertype of frame
} _zp_eth_vlan_header_t;

typedef struct {
_z_sys_net_socket_t _sock;
uint16_t _vlan;
uint16_t ethtype;
uint8_t _dmac[_ZP_MAC_ADDR_LENGTH];
uint8_t _smac[_ZP_MAC_ADDR_LENGTH];
_Bool _has_vlan;
Expand Down
3 changes: 3 additions & 0 deletions include/zenoh-pico/transport/raweth/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ typedef struct {
_Bool _has_vlan;
} _zp_raweth_cfg_entry;

// Ethertype to use in frame
extern const uint16_t _ZP_RAWETH_CFG_ETHTYPE;

// Source mac address
extern const uint8_t _ZP_RAWETH_CFG_SMAC[_ZP_MAC_ADDR_LENGTH];

Expand Down
3 changes: 3 additions & 0 deletions src/transport/raweth/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#if Z_FEATURE_RAWETH_TRANSPORT == 1

// Should be generated
const uint16_t _ZP_RAWETH_CFG_ETHTYPE = 0x72e0;

// Should be generated
const uint8_t _ZP_RAWETH_CFG_SMAC[_ZP_MAC_ADDR_LENGTH] = {0x30, 0x03, 0xc8, 0x37, 0x25, 0xa1};

Expand Down
4 changes: 2 additions & 2 deletions src/transport/raweth/rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static size_t _z_raweth_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z
// Check if header has vlan
_Bool has_vlan = false;
_zp_eth_header_t *header = (_zp_eth_header_t *)buff;
if (header->length > _ZP_ETH_TYPE_CUTOFF) {
if (header->ethtype == _ZP_ETH_TYPE_VLAN) {
has_vlan = true;
}
// Check validity
Expand All @@ -54,7 +54,7 @@ static size_t _z_raweth_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z
return rb;
}

_z_transport_peer_entry_t *_z_find_peer_entry(_z_transport_peer_entry_list_t *l, _z_bytes_t *remote_addr) {
static _z_transport_peer_entry_t *_z_find_peer_entry(_z_transport_peer_entry_list_t *l, _z_bytes_t *remote_addr) {
_z_transport_peer_entry_t *ret = NULL;

_z_transport_peer_entry_list_t *xs = l;
Expand Down
51 changes: 9 additions & 42 deletions src/transport/raweth/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,12 @@ static _z_zint_t __unsafe_z_raweth_get_sn(_z_transport_multicast_t *ztm, z_relia
return sn;
}

/**
* This function is unsafe because it operates in potentially concurrent data.
* Make sure that the following mutexes are locked before calling this function:
* - ztm->_mutex_inner
*/
static void __unsafe_z_raweth_prepare_header(_z_transport_multicast_t *ztm) {
// Reserve space for ethernet header
if (&ztm->_link._socket._raweth._has_vlan) {
_z_wbuf_set_wpos(&ztm->_wbuf, sizeof(_zp_eth_vlan_header_t));
} else {
_z_wbuf_set_wpos(&ztm->_wbuf, sizeof(_zp_eth_header_t));
}
}

/**
* This function is unsafe because it operates in potentially concurrent data.
* Make sure that the following mutexes are locked before calling this function:
* - ztm->_mutex_inner
*/
static int8_t __unsafe_z_raweth_write_header(_z_transport_multicast_t *ztm) {
size_t len = _z_wbuf_len(&ztm->_wbuf);
_z_raweth_socket_t *resocket = &ztm->_link._socket._raweth;

// Write eth header in buffer
Expand All @@ -112,29 +97,17 @@ static int8_t __unsafe_z_raweth_write_header(_z_transport_multicast_t *ztm) {
memset(&header, 0, sizeof(header));
memcpy(&header.dmac, &resocket->_dmac, _ZP_MAC_ADDR_LENGTH);
memcpy(&header.smac, &resocket->_smac, _ZP_MAC_ADDR_LENGTH);
header.tag.tpid = _ZP_ETH_TYPE_VLAN;
header.tag.pcp = 0; // Get from locator/config
header.tag.vid = 0; // Get from locator/config
header.length = (uint16_t)len;
// Save wpos
size_t final_wpos = _z_wbuf_get_wpos(&ztm->_wbuf);
header.tag = _ZP_ETH_TYPE_VLAN;
header.ethtype = _ZP_RAWETH_CFG_ETHTYPE;
// Write header
_z_wbuf_set_wpos(&ztm->_wbuf, 0);
_Z_RETURN_IF_ERR(_z_wbuf_write_bytes(&ztm->_wbuf, (uint8_t *)&header, 0, sizeof(header)));
// Restore wpos
_z_wbuf_set_wpos(&ztm->_wbuf, final_wpos);
} else {
_zp_eth_header_t header;
memcpy(&header.dmac, &resocket->_dmac, _ZP_MAC_ADDR_LENGTH);
memcpy(&header.smac, &resocket->_smac, _ZP_MAC_ADDR_LENGTH);
header.length = (uint16_t)len;
// Save wpos
size_t final_wpos = _z_wbuf_get_wpos(&ztm->_wbuf);
header.ethtype = _ZP_RAWETH_CFG_ETHTYPE;
// Write header
_z_wbuf_set_wpos(&ztm->_wbuf, 0);
_Z_RETURN_IF_ERR(_z_wbuf_write_bytes(&ztm->_wbuf, (uint8_t *)&header, 0, sizeof(header)));
// Restore wpos
_z_wbuf_set_wpos(&ztm->_wbuf, final_wpos);
}
return _Z_RES_OK;
}
Expand Down Expand Up @@ -181,12 +154,10 @@ int8_t _z_raweth_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport_me
#endif
// Set socket info
_zp_raweth_set_socket(NULL, &ztm->_link._socket._raweth);
// Reserve space for eth header
__unsafe_z_raweth_prepare_header(ztm);
// Encode the session message
_Z_RETURN_IF_ERR(_z_transport_message_encode(&ztm->_wbuf, t_msg));
// Write the message header
_Z_RETURN_IF_ERR(__unsafe_z_raweth_write_header(ztm));
// Encode the session message
_Z_RETURN_IF_ERR(_z_transport_message_encode(&ztm->_wbuf, t_msg));
// Send the wbuf on the socket
_Z_RETURN_IF_ERR(_z_raweth_link_send_wbuf(ztm));
// Mark the session that we have transmitted data
Expand Down Expand Up @@ -238,8 +209,8 @@ int8_t _z_raweth_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg,
}
// Set socket info
_zp_raweth_set_socket(keyexpr, &ztm->_link._socket._raweth);
// Reserve space for eth header
__unsafe_z_raweth_prepare_header(ztm);
// Write the eth header
_Z_RETURN_IF_ERR(__unsafe_z_raweth_write_header(ztm));
// Set the frame header
_z_zint_t sn = __unsafe_z_raweth_get_sn(ztm, reliability);
_z_transport_message_t t_msg = _z_t_msg_make_frame_header(sn, reliability);
Expand All @@ -248,8 +219,6 @@ int8_t _z_raweth_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg,
// Encode the network message
ret = _z_network_message_encode(&ztm->_wbuf, n_msg);
if (ret == _Z_RES_OK) {
// Write the eth header
_Z_RETURN_IF_ERR(__unsafe_z_raweth_write_header(ztm));
// Send the wbuf on the socket
_Z_RETURN_IF_ERR(_z_raweth_link_send_wbuf(ztm));
// Mark the session that we have transmitted data
Expand All @@ -267,12 +236,10 @@ int8_t _z_raweth_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg,
sn = __unsafe_z_raweth_get_sn(ztm, reliability);
}
is_first = false;
// Reserve space for eth header
__unsafe_z_raweth_prepare_header(ztm);
// Serialize one fragment
_Z_RETURN_IF_ERR(__unsafe_z_serialize_zenoh_fragment(&ztm->_wbuf, &fbf, reliability, sn));
// Write the eth header
_Z_RETURN_IF_ERR(__unsafe_z_raweth_write_header(ztm));
// Serialize one fragment
_Z_RETURN_IF_ERR(__unsafe_z_serialize_zenoh_fragment(&ztm->_wbuf, &fbf, reliability, sn));
// Send the wbuf on the socket
_Z_RETURN_IF_ERR(_z_raweth_link_send_wbuf(ztm));
// Mark the session that we have transmitted data
Expand Down

0 comments on commit 85af6b0

Please sign in to comment.