Commit 06ef4b9b authored by Alessandro Rubini's avatar Alessandro Rubini

general: net: rename some fields, fix alignment

This changes some things to make code more readable (I really didn't
know what the tmp_header was: now it is the "received_ptp_header".
The commit simplifies allocation, because the tx and rx buffers are
now part of ppi itself.

We now have tx_ptp and tx_frame, both pointing within tx_buffer (and
the same for rx). The pointers are the protocol side (aligned) and the
argument passed to send/recv (which may be not be 4-aligned).

At this point nothing changes, i.e. both gnu-linux flavours work and
bare-i386 is not able to received.
parent 51ea99db
......@@ -37,7 +37,7 @@ static int posix_recv_msg(struct pp_instance *ppi, int fd, void *pkt, int len,
struct timeval *tv;
vec[0].iov_base = pkt;
vec[0].iov_len = PP_PACKET_SIZE;
vec[0].iov_len = PP_MAX_FRAME_LENGTH;
memset(&msg, 0, sizeof(msg));
memset(&cmsg_un, 0, sizeof(cmsg_un));
......@@ -353,11 +353,8 @@ int posix_net_init(struct pp_instance *ppi)
{
int i;
ppi->buf_out = calloc(1, PP_PACKET_SIZE + NP(ppi)->ptp_offset);
if (!ppi->buf_out)
return -1;
ppi->buf_out = pp_get_payload(ppi, ppi->buf_out);
/* The buffer is inside ppi, but we need to set pointers and align */
pp_prepare_pointers(ppi);
if (OPTS(ppi)->ethernet_mode) {
PP_PRINTF("posix_net_init IEEE 802.3\n");
......
......@@ -12,7 +12,6 @@
#include <ptpd_netif.h>
int wrpc_errno;
Octet buffer_out[PP_PACKET_SIZE + 14]; /* 14 == ppi->proto_ofst for eth mode */
/* This function should init the minic and get the mac address */
static int wrpc_open_ch(struct pp_instance *ppi)
......@@ -105,10 +104,7 @@ static int wrpc_net_send(struct pp_instance *ppi, void *pkt, int len,
static int wrpc_net_init(struct pp_instance *ppi)
{
ppi->buf_out = buffer_out;
ppi->buf_out = pp_get_payload(ppi, ppi->buf_out);
ppi->buf_out += 4 - (((int)ppi->buf_out) % 4); /* FIXME Alignment */
pp_prepare_pointers(ppi);
wrpc_open_ch(ppi);
return 0;
......
......@@ -72,7 +72,8 @@ enum pp_timeouts {
#define PP_PDELAY_RESP_FOLLOW_UP_LENGTH 54
#define PP_MANAGEMENT_LENGTH 48
#define PP_MINIMUM_LENGTH 44
#define PP_MINIMUM_LENGTH 44
#define PP_MAX_FRAME_LENGTH 200 /* must fit extension and ethhdr */
#define PP_DEFAULT_NEXT_DELAY_MS 1000
......@@ -81,7 +82,6 @@ enum pp_timeouts {
#define PP_PORT_ADDRESS_LENGTH 2
#define PP_UUID_LENGTH 6
#define PP_FLAG_FIELD_LENGTH 2
#define PP_PACKET_SIZE 300
#define PP_EVT_PORT 319
#define PP_GEN_PORT 320
#define PP_DEFAULT_DOMAIN_ADDRESS "224.0.1.129"
......
......@@ -130,6 +130,7 @@ struct pp_net_path {
/* FIXME check if useful Integer32 ucast_addr;*/
Integer32 mcast_addr;
Integer32 peer_mcast_addr;
int ptp_offset;
int inited;
};
......@@ -151,6 +152,14 @@ struct pp_instance {
struct pp_network_operations *n_ops;
struct pp_time_operations *t_ops;
/*
* We host the buffer for this fsm here, tracking both frame
* and payload. send/recv get the frame, pack/unpack the payload
*/
unsigned char tx_buffer[PP_MAX_FRAME_LENGTH];
unsigned char rx_buffer[PP_MAX_FRAME_LENGTH];
void *tx_frame, *rx_frame, *tx_ptp, *rx_ptp;
/* Data sets */
DSDefault *defaultDS; /* page 65 */
DSCurrent *currentDS; /* page 67 */
......@@ -163,7 +172,6 @@ struct pp_instance {
Integer16 foreign_record_best;
Boolean record_update;
struct pp_frgn_master *frgn_master;
Octet *buf_out;
TimeInternal sync_receive_time;
UInteger16 recv_sync_sequence_id;
......@@ -183,7 +191,7 @@ struct pp_instance {
MsgAnnounce announce;
} msg_tmp;
UInteger16 sent_seq[__PP_NR_MESSAGES_TYPES]; /* last sent this type */
MsgHeader msg_tmp_header;
MsgHeader received_ptp_header;
MsgHeader delay_req_hdr;
UInteger32
is_from_cur_par:1,
......@@ -248,6 +256,8 @@ static inline void *pp_get_payload(struct pp_instance *ppi, void *frame_ptr)
return frame_ptr + NP(ppi)->ptp_offset;
}
extern void pp_prepare_pointers(struct pp_instance *ppi);
/*
* Each extension should fill this structure that is used to augment
......
......@@ -7,9 +7,6 @@
#include <ppsi/diag.h>
#include "bare-linux.h"
/* 14 is ptp_offset for ethernet mode */
Octet buffer_out[PP_PACKET_SIZE + 14];
/* FIXME: which socket we receive and send with? */
static int bare_net_recv(struct pp_instance *ppi, void *pkt, int len,
TimeInternal *t)
......@@ -125,8 +122,8 @@ static int bare_open_ch(struct pp_instance *ppi, char *ifname)
static int bare_net_init(struct pp_instance *ppi)
{
ppi->buf_out = buffer_out;
ppi->buf_out = pp_get_payload(ppi, ppi->buf_out);
/* The buffer is inside ppi, but we need to set pointers and align */
pp_prepare_pointers(ppi);
if (OPTS(ppi)->ethernet_mode) {
PP_PRINTF("bare_net_init IEEE 802.3\n");
......
......@@ -39,7 +39,7 @@ static int wr_listening(struct pp_instance *ppi, unsigned char *pkt, int plen)
static int wr_master_msg(struct pp_instance *ppi, unsigned char *pkt, int plen,
int msgtype)
{
MsgHeader *hdr = &ppi->msg_tmp_header;
MsgHeader *hdr = &ppi->received_ptp_header;
MsgSignaling wrsig_msg;
TimeInternal *time = &ppi->last_rcv_time;
......@@ -77,7 +77,7 @@ static int wr_new_slave(struct pp_instance *ppi, unsigned char *pkt, int plen)
static int wr_update_delay(struct pp_instance *ppi)
{
MsgHeader *hdr = &ppi->msg_tmp_header;
MsgHeader *hdr = &ppi->received_ptp_header;
TimeInternal correction_field;
int64_to_TimeInternal(hdr->correctionfield, &correction_field);
......
......@@ -29,7 +29,7 @@ int wr_calibrated(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (plen == 0)
goto out;
if (ppi->msg_tmp_header.messageType == PPM_SIGNALING) {
if (ppi->received_ptp_header.messageType == PPM_SIGNALING) {
msg_unpack_wrsig(ppi, pkt, &wrsig_msg,
&(WR_DSPOR(ppi)->msgTmpWrMessageID));
......
......@@ -31,7 +31,7 @@ int wr_locked(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (plen == 0)
goto out;
if (ppi->msg_tmp_header.messageType == PPM_SIGNALING) {
if (ppi->received_ptp_header.messageType == PPM_SIGNALING) {
msg_unpack_wrsig(ppi, pkt, &wrsig_msg,
&(WR_DSPOR(ppi)->msgTmpWrMessageID));
......
......@@ -28,7 +28,7 @@ int wr_m_lock(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (plen == 0)
goto out;
if (ppi->msg_tmp_header.messageType == PPM_SIGNALING) {
if (ppi->received_ptp_header.messageType == PPM_SIGNALING) {
msg_unpack_wrsig(ppi, pkt, &wrsig_msg,
&(WR_DSPOR(ppi)->msgTmpWrMessageID));
......
......@@ -33,7 +33,7 @@ int wr_present(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (plen == 0)
goto out;
if (ppi->msg_tmp_header.messageType == PPM_SIGNALING) {
if (ppi->received_ptp_header.messageType == PPM_SIGNALING) {
msg_unpack_wrsig(ppi, pkt, &wrsig_msg,
&(WR_DSPOR(ppi)->msgTmpWrMessageID));
......
......@@ -35,7 +35,7 @@ int wr_resp_calib_req(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (plen == 0)
goto out;
if (ppi->msg_tmp_header.messageType == PPM_SIGNALING) {
if (ppi->received_ptp_header.messageType == PPM_SIGNALING) {
msg_unpack_wrsig(ppi, pkt, &wrsig_msg,
&(WR_DSPOR(ppi)->msgTmpWrMessageID));
......
......@@ -56,7 +56,7 @@ void msg_pack_announce_wr_tlv(struct pp_instance *ppi)
UInteger16 wr_flags = 0;
/* Change length */
buf = ppi->buf_out;
buf = ppi->tx_ptp;
*(UInteger16 *)(buf + 2) = htons(WR_ANNOUNCE_LENGTH);
*(UInteger16 *)(buf + 64) = htons(TLV_TYPE_ORG_EXTENSION);
......@@ -118,7 +118,7 @@ int msg_pack_wrsig(struct pp_instance *ppi, Enumeration16 wr_msg_id)
return 0;
}
buf = ppi->buf_out;
buf = ppi->tx_ptp;
/* Changes in header */
*(char *)(buf+0) = *(char *)(buf+0) & 0xF0; /* RAZ messageType */
......
......@@ -256,11 +256,11 @@ UInteger8 bmc_state_decision(struct pp_instance *ppi,
if ((!ppi->number_foreign_records) && (ppi->state == PPS_LISTENING))
return PPS_LISTENING;
copy_d0(ppi, &ppi->msg_tmp_header, &ppi->msg_tmp.announce);
copy_d0(ppi, &ppi->received_ptp_header, &ppi->msg_tmp.announce);
cmpres = bmc_dataset_cmp(ppi,
&ppi->msg_tmp_header,
&ppi->received_ptp_header,
&ppi->msg_tmp.announce,
hdr, ann);
......
......@@ -2,11 +2,37 @@
* Aurelio Colosimo for CERN, 2011 -- GNU LGPL v2.1 or later
* Based on PTPd project v. 2.1.0 (see AUTHORS for details)
*/
#include <ppsi/ppsi.h>
#include <ppsi/diag.h>
#include "common-fun.h"
static void *__align_pointer(void *p)
{
unsigned long ip, align = 0;
ip = (unsigned long)p;
if (ip & 3)
align = 4 - (ip & 3);
return p + align;
}
void pp_prepare_pointers(struct pp_instance *ppi)
{
ppi->tx_ptp = __align_pointer(pp_get_payload(ppi, ppi->tx_buffer));
ppi->rx_ptp = __align_pointer(pp_get_payload(ppi, ppi->rx_buffer));
/* Now that ptp payload is aligned, get back the header */
ppi->tx_frame = pp_get_header(ppi, ppi->tx_ptp);
ppi->rx_frame = pp_get_header(ppi, ppi->rx_ptp);
if (0) { /* enable to verify... it works for me though */
pp_printf("%p -> %p %p\n",
ppi->tx_buffer, ppi->tx_frame, ppi->tx_ptp);
pp_printf("%p -> %p %p\n",
ppi->rx_buffer, ppi->rx_frame, ppi->rx_ptp);
}
}
/* Called by listening, passive, slave, uncalibrated */
int st_com_execute_slave(struct pp_instance *ppi, int check_delayreq)
{
......@@ -67,7 +93,7 @@ static void st_com_add_foreign(struct pp_instance *ppi, unsigned char *buf)
{
int i, j;
int found = 0;
MsgHeader *hdr = &ppi->msg_tmp_header;
MsgHeader *hdr = &ppi->received_ptp_header;
j = ppi->foreign_record_best;
......@@ -133,7 +159,7 @@ static void st_com_add_foreign(struct pp_instance *ppi, unsigned char *buf)
int st_com_slave_handle_announce(struct pp_instance *ppi, unsigned char *buf,
int len)
{
MsgHeader *hdr = &ppi->msg_tmp_header;
MsgHeader *hdr = &ppi->received_ptp_header;
if (len < PP_ANNOUNCE_LENGTH)
return -1;
......@@ -168,7 +194,7 @@ int st_com_slave_handle_sync(struct pp_instance *ppi, unsigned char *buf,
TimeInternal *time;
TimeInternal origin_tstamp;
TimeInternal correction_field;
MsgHeader *hdr = &ppi->msg_tmp_header;
MsgHeader *hdr = &ppi->received_ptp_header;
if (len < PP_SYNC_LENGTH)
return -1;
......@@ -200,7 +226,7 @@ int st_com_slave_handle_sync(struct pp_instance *ppi, unsigned char *buf,
} else {
msg_unpack_sync(buf, &ppi->msg_tmp.sync);
int64_to_TimeInternal(
ppi->msg_tmp_header.correctionfield,
ppi->received_ptp_header.correctionfield,
&correction_field);
display_TimeInternal("Correction field",
......@@ -226,7 +252,7 @@ int st_com_slave_handle_followup(struct pp_instance *ppi, unsigned char *buf,
TimeInternal correction_field;
int ret = 0;
MsgHeader *hdr = &ppi->msg_tmp_header;
MsgHeader *hdr = &ppi->received_ptp_header;
if (len < PP_FOLLOW_UP_LENGTH)
return -1;
......@@ -254,7 +280,7 @@ int st_com_slave_handle_followup(struct pp_instance *ppi, unsigned char *buf,
to_TimeInternal(&precise_orig_timestamp,
&ppi->msg_tmp.follow.preciseOriginTimestamp);
int64_to_TimeInternal(ppi->msg_tmp_header.correctionfield,
int64_to_TimeInternal(ppi->received_ptp_header.correctionfield,
&correction_field);
add_TimeInternal(&correction_field, &correction_field,
......
......@@ -40,7 +40,7 @@ int st_com_slave_handle_followup(struct pp_instance *ppi, unsigned char *buf,
static inline int __send_and_log(struct pp_instance *ppi, int msglen,
int msgtype, int chtype)
{
if (ppi->n_ops->send(ppi, ppi->buf_out, msglen,
if (ppi->n_ops->send(ppi, ppi->tx_ptp, msglen,
&ppi->last_snt_time, chtype, 0) < msglen) {
if (pp_verbose_frames)
PP_PRINTF("%s(%d) Message can't be sent\n",
......
......@@ -83,7 +83,7 @@ static void msg_display_announce(MsgAnnounce *announce)
/* Unpack header from in buffer to msg_tmp_header field */
int msg_unpack_header(struct pp_instance *ppi, void *buf)
{
MsgHeader *hdr = &ppi->msg_tmp_header;
MsgHeader *hdr = &ppi->received_ptp_header;
hdr->transportSpecific = (*(Nibble *) (buf + 0)) >> 4;
hdr->messageType = (*(Enumeration4 *) (buf + 0)) & 0x0F;
......@@ -114,7 +114,7 @@ int msg_unpack_header(struct pp_instance *ppi, void *buf)
* any port, not only the same port, as we can't sync with
* ourself even when we'll run in multi-port mode.
*/
if (!memcmp(ppi->msg_tmp_header.sourcePortIdentity.clockIdentity,
if (!memcmp(ppi->received_ptp_header.sourcePortIdentity.clockIdentity,
DSPOR(ppi)->portIdentity.clockIdentity,
PP_CLOCK_IDENTITY_LENGTH))
return -1;
......@@ -164,7 +164,7 @@ void msg_pack_sync(struct pp_instance *ppi, Timestamp *orig_tstamp)
{
void *buf;
buf = ppi->buf_out;
buf = ppi->tx_ptp;
/* changes in header */
*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
......@@ -208,7 +208,7 @@ int msg_pack_announce(struct pp_instance *ppi)
{
void *buf;
buf = ppi->buf_out;
buf = ppi->tx_ptp;
/* changes in header */
*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
/* RAZ messageType */
......@@ -272,7 +272,7 @@ void msg_pack_follow_up(struct pp_instance *ppi, Timestamp *prec_orig_tstamp)
{
void *buf;
buf = ppi->buf_out;
buf = ppi->tx_ptp;
/* changes in header */
*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
......@@ -321,7 +321,7 @@ void msg_pack_delay_req(struct pp_instance *ppi, Timestamp *orig_tstamp)
{
void *buf;
buf = ppi->buf_out;
buf = ppi->tx_ptp;
/* changes in header */
*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
......@@ -351,7 +351,7 @@ void msg_pack_delay_resp(struct pp_instance *ppi,
{
void *buf;
buf = ppi->buf_out;
buf = ppi->tx_ptp;
/* changes in header */
*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
......
......@@ -81,7 +81,7 @@ int pp_initializing(struct pp_instance *ppi, unsigned char *pkt, int plen)
m1(ppi);
msg_pack_header(ppi, ppi->buf_out);
msg_pack_header(ppi, ppi->tx_ptp); /* This is used for all tx */
if (!opt->master_only)
ppi->next_state = PPS_LISTENING;
......
......@@ -27,7 +27,7 @@ int pp_listening(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (plen == 0)
goto out;
switch (ppi->msg_tmp_header.messageType) {
switch (ppi->received_ptp_header.messageType) {
case PPM_ANNOUNCE:
e = st_com_master_handle_announce(ppi, pkt, plen);
......
......@@ -13,7 +13,6 @@ int pp_master(struct pp_instance *ppi, unsigned char *pkt, int plen)
TimeInternal *time_snt;
int msgtype;
int e = 0; /* error var, to check errors in msg handling */
MsgHeader *hdr = &ppi->msg_tmp_header;
time = &ppi->last_rcv_time;
......@@ -62,7 +61,7 @@ int pp_master(struct pp_instance *ppi, unsigned char *pkt, int plen)
* possibly returning error or eating the message by returning
* PPM_NOTHING_TO_DO
*/
msgtype = ppi->msg_tmp_header.messageType;
msgtype = ppi->received_ptp_header.messageType;
if (pp_hooks.master_msg)
msgtype = pp_hooks.master_msg(ppi, pkt, plen, msgtype);
if (msgtype < 0) {
......@@ -84,7 +83,8 @@ int pp_master(struct pp_instance *ppi, unsigned char *pkt, int plen)
break;
case PPM_DELAY_REQ:
msg_copy_header(&ppi->delay_req_hdr, hdr);
msg_copy_header(&ppi->delay_req_hdr,
&ppi->received_ptp_header);
msg_issue_delay_resp(ppi, time);
break;
......
......@@ -22,7 +22,7 @@ int pp_passive(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (plen == 0)
goto no_incoming_msg;
switch (ppi->msg_tmp_header.messageType) {
switch (ppi->received_ptp_header.messageType) {
case PPM_ANNOUNCE:
e = st_com_master_handle_announce(ppi, pkt, plen);
......
......@@ -11,7 +11,7 @@ int pp_slave(struct pp_instance *ppi, unsigned char *pkt, int plen)
{
int e = 0; /* error var, to check errors in msg handling */
TimeInternal correction_field;
MsgHeader *hdr = &ppi->msg_tmp_header;
MsgHeader *hdr = &ppi->received_ptp_header;
if (ppi->is_new_state) {
DSPOR(ppi)->portState = PPS_SLAVE;
......@@ -37,7 +37,7 @@ int pp_slave(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (plen == 0)
goto out;
switch (ppi->msg_tmp_header.messageType) {
switch (hdr->messageType) {
case PPM_ANNOUNCE:
e = st_com_slave_handle_announce(ppi, pkt, plen);
......
......@@ -13,7 +13,7 @@ int pp_uncalibrated(struct pp_instance *ppi, unsigned char *pkt, int plen)
if (plen == 0)
goto no_incoming_msg;
switch (ppi->msg_tmp_header.messageType) {
switch (ppi->received_ptp_header.messageType) {
case PPM_ANNOUNCE:
e = st_com_slave_handle_announce(ppi, pkt, plen);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment