flexPTP 1.0
An IEEE 1588 PTP implementation designed for microcontrollers
Loading...
Searching...
No Matches
nsd_linux.c File Reference
#include "../../network_stack_driver.h"
#include "../../ptp_defs.h"
#include "../../task_ptp.h"
#include <arpa/inet.h>
#include <asm-generic/errno-base.h>
#include <asm/socket.h>
#include <fcntl.h>
#include <linux/ethtool.h>
#include <linux/net_tstamp.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <inttypes.h>
#include <features.h>
#include <asm-generic/unistd.h>
#include <bits/time.h>
#include <linux/if_packet.h>
#include <netinet/ether.h>
#include <poll.h>
#include <pthread.h>
#include <sys/timex.h>

Go to the source code of this file.

Macros

#define LINUX_NSD_TS_DEBUG   (0)
 
#define LINUX_NSD_TX_ENQUEUE_DEBUG   (0)
 
#define PHY_FILE_NAME_SIZE   (16)
 
#define PRINT_HWADDR(a)   MSG("%02X:%02X:%02X:%02X:%02X:%02X", a[0], a[1], a[2], a[3], a[4], a[5]);
 
#define NOTIF_QUIT_TRANSCEIVER_THREAD   'Q'
 
#define CLOCKFD   3
 
#define FD_TO_CLOCKID(fd)   ((clockid_t)((((unsigned int)~fd) << 3) | CLOCKFD))
 
#define CLOCKID_TO_FD(clk)   ((unsigned int)~((clk) >> 3))
 
#define NAME_BUF_SIZE   (256)
 
#define MSG_BUF_SIZE   (1600)
 
#define CTRL_BUF_SIZE   (256)
 
#define PPB_TO_TUNING_SCALER   (((double)(1 << 16)) / 1000.0)
 

Functions

static void * nsd_thread (void *arg)
 
static void post_notification (char c)
 
bool linux_nsd_preinit (const char *ifn)
 
void linux_nsd_cleanup (void)
 
static void socket_join_igmp (int fd, in_addr_t igmp_addr)
 
void ptp_nsd_igmp_join_leave (bool join)
 
static int open_udp_socket (in_addr_t s_addr, uint16_t port, const char *hint)
 
static int open_raw_socket (const uint8_t *ethaddr, bool bind_socket, const char *hint)
 
static void enable_timestamping (int sfd)
 
static void clear_revents_by_fd (int fd, unsigned short revent, struct pollfd *pfd, int nfds)
 
void ptp_nsd_init (const NsdInitSettings *init)
 
void ptp_nsd_transmit_msg (RawPtpMessage *pMsg, uint32_t uid)
 
void ptp_nsd_get_interface_address (uint8_t *hwa)
 
void linux_adjust_clock (double tuning_ppb)
 
void linux_set_time (uint32_t seconds, uint32_t nanoseconds)
 
void linux_get_time (TimestampU *pTime)
 

Variables

static int primary_event_fd = -1
 
static int primary_general_fd = -1
 
static int pdelay_event_fd = -1
 
static int pdelay_general_fd = -1
 
static PtpTransportType TP = -1
 
static PtpDelayMechanism DM = -1
 
static uint8_t p2p_8023_primary_dest [6] = {}
 
static uint8_t p2p_8023_pdel_dest [6] = {}
 
static bool identical_p2p_8023_dests = false
 
static const uint8_t zero_mac [ETH_ALEN] = {}
 
static uint16_t if_idx
 
static char if_name [IFNAMSIZ]
 
static uint8_t if_hwaddr [IFHWADDRLEN]
 
static struct sockaddr_in if_ipaddr
 
static uint16_t phc_index
 
static char phc_file_name [(16)]
 
static int phc_fd
 
static clockid_t phc_clkid
 
static int notif_q [2]
 
static int matching_q [2]
 
static pthread_t transceiver_thread
 
static char name_buf [(256)]
 
static char msg_buf [(1600)]
 
static char rx_ctrl_buf [(256)]
 

Macro Definition Documentation

◆ CLOCKFD

#define CLOCKFD   3

Definition at line 79 of file nsd_linux.c.

◆ CLOCKID_TO_FD

#define CLOCKID_TO_FD (   clk)    ((unsigned int)~((clk) >> 3))

Definition at line 81 of file nsd_linux.c.

◆ CTRL_BUF_SIZE

#define CTRL_BUF_SIZE   (256)

Definition at line 397 of file nsd_linux.c.

◆ FD_TO_CLOCKID

#define FD_TO_CLOCKID (   fd)    ((clockid_t)((((unsigned int)~fd) << 3) | CLOCKFD))

Definition at line 80 of file nsd_linux.c.

◆ LINUX_NSD_TS_DEBUG

#define LINUX_NSD_TS_DEBUG   (0)

Definition at line 33 of file nsd_linux.c.

◆ LINUX_NSD_TX_ENQUEUE_DEBUG

#define LINUX_NSD_TX_ENQUEUE_DEBUG   (0)

Definition at line 34 of file nsd_linux.c.

◆ MSG_BUF_SIZE

#define MSG_BUF_SIZE   (1600)

Definition at line 395 of file nsd_linux.c.

◆ NAME_BUF_SIZE

#define NAME_BUF_SIZE   (256)

Definition at line 393 of file nsd_linux.c.

◆ NOTIF_QUIT_TRANSCEIVER_THREAD

#define NOTIF_QUIT_TRANSCEIVER_THREAD   'Q'

Definition at line 72 of file nsd_linux.c.

◆ PHY_FILE_NAME_SIZE

#define PHY_FILE_NAME_SIZE   (16)

Definition at line 58 of file nsd_linux.c.

◆ PPB_TO_TUNING_SCALER

#define PPB_TO_TUNING_SCALER   (((double)(1 << 16)) / 1000.0)

Definition at line 682 of file nsd_linux.c.

◆ PRINT_HWADDR

#define PRINT_HWADDR (   a)    MSG("%02X:%02X:%02X:%02X:%02X:%02X", a[0], a[1], a[2], a[3], a[4], a[5]);

Definition at line 70 of file nsd_linux.c.

Function Documentation

◆ clear_revents_by_fd()

static void clear_revents_by_fd ( int  fd,
unsigned short  revent,
struct pollfd *  pfd,
int  nfds 
)
static

Definition at line 400 of file nsd_linux.c.

◆ enable_timestamping()

static void enable_timestamping ( int  sfd)
static

Definition at line 350 of file nsd_linux.c.

◆ linux_adjust_clock()

void linux_adjust_clock ( double  tuning_ppb)

Adjust the PHC.

Parameters
tuning_ppbclock adjustment

Definition at line 684 of file nsd_linux.c.

◆ linux_get_time()

void linux_get_time ( TimestampU *  pTime)

Query PHC time.

Parameters
pTimepointer to a timestamp object

Definition at line 705 of file nsd_linux.c.

◆ linux_nsd_cleanup()

void linux_nsd_cleanup ( void  )

Clean up the Linux network stack driver.

Definition at line 214 of file nsd_linux.c.

◆ linux_nsd_preinit()

bool linux_nsd_preinit ( const char *  ifn)

Preinitialize the Linux network stack driver.

Parameters
ifnname of the targeted network interface (e.g. eth0)
Returns
initialization successful

Definition at line 83 of file nsd_linux.c.

◆ linux_set_time()

void linux_set_time ( uint32_t  seconds,
uint32_t  nanoseconds 
)

Set PHC time.

Parameters
secondsseconds part
nanosecondsnanoseconds part

Definition at line 698 of file nsd_linux.c.

◆ nsd_thread()

static void * nsd_thread ( void *  arg)
static

Definition at line 408 of file nsd_linux.c.

◆ open_raw_socket()

static int open_raw_socket ( const uint8_t *  ethaddr,
bool  bind_socket,
const char *  hint 
)
static

Definition at line 304 of file nsd_linux.c.

◆ open_udp_socket()

static int open_udp_socket ( in_addr_t  s_addr,
uint16_t  port,
const char *  hint 
)
static

Definition at line 255 of file nsd_linux.c.

◆ post_notification()

static void post_notification ( char  c)
static

Definition at line 74 of file nsd_linux.c.

◆ ptp_nsd_get_interface_address()

void ptp_nsd_get_interface_address ( uint8_t *  hwa)

Fetch the Ethernet network interface hardware address.

Parameters
hwapointer to a 6-byte array where the hardware address is going to be stored to

Definition at line 675 of file nsd_linux.c.

◆ ptp_nsd_igmp_join_leave()

void ptp_nsd_igmp_join_leave ( bool  join)

Join or leave IGMP groups associated with the current PTP profile.

Definition at line 235 of file nsd_linux.c.

◆ ptp_nsd_init()

void ptp_nsd_init ( const NsdInitSettings *  init)

Initialize or reinitialize the Network Stack Driver.

Parameters
initNSD initialization settings

Definition at line 526 of file nsd_linux.c.

◆ ptp_nsd_transmit_msg()

void ptp_nsd_transmit_msg ( RawPtpMessage *  pMsg,
uint32_t  uid 
)

Transmit a PTP message.

Parameters
pMsgpointer to a RawPtpMessage
uidUID of the message

Definition at line 613 of file nsd_linux.c.

◆ socket_join_igmp()

static void socket_join_igmp ( int  fd,
in_addr_t  igmp_addr 
)
static

Definition at line 221 of file nsd_linux.c.

Variable Documentation

◆ DM

PtpDelayMechanism DM = -1
static

Definition at line 44 of file nsd_linux.c.

◆ identical_p2p_8023_dests

bool identical_p2p_8023_dests = false
static

Definition at line 47 of file nsd_linux.c.

◆ if_hwaddr

uint8_t if_hwaddr[IFHWADDRLEN]
static

Definition at line 54 of file nsd_linux.c.

◆ if_idx

uint16_t if_idx
static

Definition at line 52 of file nsd_linux.c.

◆ if_ipaddr

struct sockaddr_in if_ipaddr
static

Definition at line 55 of file nsd_linux.c.

◆ if_name

char if_name[IFNAMSIZ]
static

Definition at line 53 of file nsd_linux.c.

◆ matching_q

int matching_q[2]
static

Definition at line 66 of file nsd_linux.c.

◆ msg_buf

char msg_buf[(1600)]
static

Definition at line 396 of file nsd_linux.c.

◆ name_buf

char name_buf[(256)]
static

Definition at line 394 of file nsd_linux.c.

◆ notif_q

int notif_q[2]
static

Definition at line 65 of file nsd_linux.c.

◆ p2p_8023_pdel_dest

uint8_t p2p_8023_pdel_dest[6] = {}
static

Definition at line 46 of file nsd_linux.c.

◆ p2p_8023_primary_dest

uint8_t p2p_8023_primary_dest[6] = {}
static

Definition at line 45 of file nsd_linux.c.

◆ pdelay_event_fd

int pdelay_event_fd = -1
static

Definition at line 39 of file nsd_linux.c.

◆ pdelay_general_fd

int pdelay_general_fd = -1
static

Definition at line 40 of file nsd_linux.c.

◆ phc_clkid

clockid_t phc_clkid
static

Definition at line 62 of file nsd_linux.c.

◆ phc_fd

int phc_fd
static

Definition at line 61 of file nsd_linux.c.

◆ phc_file_name

char phc_file_name[(16)]
static

Definition at line 60 of file nsd_linux.c.

◆ phc_index

uint16_t phc_index
static

Definition at line 59 of file nsd_linux.c.

◆ primary_event_fd

int primary_event_fd = -1
static

Definition at line 37 of file nsd_linux.c.

◆ primary_general_fd

int primary_general_fd = -1
static

Definition at line 38 of file nsd_linux.c.

◆ rx_ctrl_buf

char rx_ctrl_buf[(256)]
static

Definition at line 398 of file nsd_linux.c.

◆ TP

PtpTransportType TP = -1
static

Definition at line 43 of file nsd_linux.c.

◆ transceiver_thread

pthread_t transceiver_thread
static

Definition at line 67 of file nsd_linux.c.

◆ zero_mac

const uint8_t zero_mac[ETH_ALEN] = {}
static

Definition at line 49 of file nsd_linux.c.