Commit bb92b0d9 authored by Maciej Lipinski's avatar Maciej Lipinski Committed by Benoit Rat

SW: adding networkTool by Maciej. This is a crappa-code which enables to…

SW: adding networkTool by Maciej. This is a crappa-code which enables to stress-test a bit the switch with heavy traffic, checks frame loss and frame latency
parent 8cf2d196
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.
CC = g++
CFLAGS = -g3 -O0 -lm -w -Wall
TESTER = test
DECODER = networkTool
OBJS = networkTool.o
OUTPUT= $(DECODER)
all : $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(OUTPUT)
clean:
rm -f $(OBJS) $(TESTER) $(DECODER)
The networkTool is my private and crappy code that I created to test reliability mechanims
in a White Rabbit network. I did it in a rush, looking at codes of the others (Alessandro
Rubini and Bartosz Bielawski) and creating something suited to my reqirements based on
ptp-noposix network functions.
DISCLAIMER:
The code is by no means an example of programming practices -- it is crap.
The code is by no means working perfectly -- it has many bugs (known and unknown)
This code is able to make a Gigabit Ethernet Card worth 1000 euro to loose frames, therefore
the results of it should be taken with loads of thoughts and wits and knowing what one is
doing. It is not always the Device Under Test (be it WR Switch) that is the cause of frame
loss.
I provide no support and no warranty for this code. I make it public because it seems to
be useful for people out there. However, anyone who uses this code should be aware what he/she
is doing and she/he shall not complain the code is crap... it is crap and by using this code
you agree that you have no room for complaints, on contrary, you are most welcome to improve
and debug it and submit your modifications to a separate branch. If I consider the changes
useful, I'll merge it to the master.
Short description:
This application can do the following:
- send raw Ethernet frames (single/burst) of different size and with different gap between
them (if burst):
* Transmission time of each frame is timestamped in the S/W and the timestamp included in
the frame,
* Each frame in a burst has a subsequent ID
- receive raw Ethernet frames (single/burst) of expected size :
* Reception time of each frame is tiemstamped
* ID of subsequent frames in a burst is verified to check whether we lost some frames
* latency and reception gap (frequency) is calculated based on measured/received timestamps
* if receiving burst, a report on the number of received/lost frames and average latency/gap
is provided.
If you want to measure latency, it is recommended to use the same PC to transmit and receive
(to have the same timebase).
In order to start:
- make
- ./networkTool -h
In order to send/receive frames you need to run it with superuser rights (sudo)
\ No newline at end of file
/*
* This work is part of the White Rabbit project
*
* Copyright (C) 2012 CERN (www.cern.ch)
* Author: Maciej Lipinski <maciej.lipinski@cern.ch>
* - parts inspired/based on private code by Alessandro Rubini and Bartosz Bielawski
* - network interface taken from ptp-noposix
*
* Released according to the GNU GPL, version 2 or any later version.
*
* Please, see README before using.
*
*/
#include "networkTool.h"
void delay(int d)
{
while (d--)
asm volatile ("nop");
}
my_socket *create_socket(wr_sockaddr_t *bind_addr,RunTimeOpts *rtOpts)
{
struct sockaddr_ll sll;
struct my_socket *s;
struct ifreq f;
int fd;
fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
if(fd < 0)
{
perror("socket()");
return NULL;
}
fcntl(fd, F_SETFL, O_NONBLOCK);
// Put the controller in promiscious mode, so it receives everything
strcpy(f.ifr_name, bind_addr->if_name);
if(ioctl(fd, SIOCGIFFLAGS,&f) < 0) { perror("ioctl()"); return NULL; }
f.ifr_flags |= IFF_PROMISC;
if(ioctl(fd, SIOCSIFFLAGS,&f) < 0) { perror("ioctl()"); return NULL; }
// Find the inteface index
strcpy(f.ifr_name, bind_addr->if_name);
ioctl(fd, SIOCGIFINDEX, &f);
sll.sll_ifindex = f.ifr_ifindex;
sll.sll_family = AF_PACKET;
sll.sll_protocol = htons(bind_addr->ethertype);
sll.sll_halen = 6;
memcpy(sll.sll_addr, bind_addr->mac, 6);
if(bind(fd, (struct sockaddr *)&sll, sizeof(struct sockaddr_ll)) < 0)
{
close(fd);
perror("bind()");
return NULL;
}
s=(struct my_socket*)calloc(sizeof(struct my_socket), 1);
s->if_index = f.ifr_ifindex;
memcpy(s->local_mac, f.ifr_hwaddr.sa_data, 6);
memcpy(&s->bind_addr, bind_addr, sizeof(wr_sockaddr_t));
s->fd = fd;
printf("Created socket at interface %s (number %d) with hardware address: %x%x%x%x%x%x\n",
s->bind_addr.if_name, s->if_index,
s->local_mac[0],
s->local_mac[1],
s->local_mac[2],
s->local_mac[3],
s->local_mac[4],
s->local_mac[5] );
return (my_socket*)s;
}
int rxfrom(my_socket *sock, wr_sockaddr_t *from, void *data, size_t data_length)
{
struct my_socket *s = (struct my_socket *)sock;
struct bench_pkt pkt;
unsigned char payload[ETHER_MTU];
struct msghdr msg;
struct iovec entry;
struct sockaddr_ll from_addr;
struct {
struct cmsghdr cm;
char control[1024];
} control;
struct cmsghdr *cmsg;
size_t len = data_length;// + sizeof(struct ethhdr);
memset(&msg, 0, sizeof(msg));
msg.msg_iov = &entry;
msg.msg_iovlen = 1;
entry.iov_base = &payload;
entry.iov_len = len;
msg.msg_name = (caddr_t)&from_addr;
msg.msg_namelen = sizeof(from_addr);
msg.msg_control = &control;
msg.msg_controllen = sizeof(control);
int ret = recvmsg(s->fd, &msg, MSG_DONTWAIT);
if(ret < 0 && errno == EAGAIN) return 0; // would be blocking
if(ret == -EAGAIN) return 0;
if(ret <= 0) return ret;
memcpy(data, payload, data_length);
printf("rx");
return (ret);
}
int rx_seq_from(my_socket *sock, wr_sockaddr_t *from, int *data, size_t data_length,
RunTimeOpts *rtOpts, long *acc_latency, long *acc_interval, timeval_t *last_rx)
{
struct my_socket *s = (struct my_socket *)sock;
struct bench_pkt pkt;
struct msghdr msg;
struct iovec entry;
struct sockaddr_ll from_addr;
struct {
struct cmsghdr cm;
char control[1024];
} control;
struct cmsghdr *cmsg;
size_t len = data_length;// + sizeof(struct ethhdr);
memset(&msg, 0, sizeof(msg));
msg.msg_iov = &entry;
msg.msg_iovlen = 1;
entry.iov_base = &pkt;
entry.iov_len = len;
msg.msg_name = (caddr_t)&from_addr;
msg.msg_namelen = sizeof(from_addr);
msg.msg_control = &control;
msg.msg_controllen = sizeof(control);
int ret = recvmsg(s->fd, &msg, MSG_DONTWAIT);
gettimeofday(&pkt.rx, NULL);
if (!pkt.size2) pkt.size2 = ret;
if(ret < 0 && errno==EAGAIN) return 0; // would be blocking
if(ret == -EAGAIN) return 0;
if(ret <= 0) return ret;
if((pkt.rx.tv_usec-pkt.tx.tv_usec)<0)
{
pkt.xx.tv_usec = 1000*1000+pkt.rx.tv_usec-pkt.tx.tv_usec;
pkt.xx.tv_sec = pkt.rx.tv_sec-pkt.tx.tv_sec-1;
}
else
{
pkt.xx.tv_usec = pkt.rx.tv_usec - pkt.tx.tv_usec;
pkt.xx.tv_sec = pkt.rx.tv_sec - pkt.tx.tv_sec;
}
(*acc_latency) = (*acc_latency) + (long)pkt.xx.tv_usec;
if(last_rx->tv_sec == 0 && last_rx->tv_usec == 0) //first
{
// first time, do nothing
}
else if((pkt.rx.tv_usec-last_rx->tv_usec)<0)
{
*acc_interval = *acc_interval + (long)(1000*1000+pkt.rx.tv_usec-last_rx->tv_usec)
+ 1000*1000*(long)(pkt.rx.tv_sec-last_rx->tv_sec-1);
}
else
{
*acc_interval = *acc_interval + (long)(pkt.rx.tv_usec-last_rx->tv_usec)
+ 1000*1000*(long)(pkt.rx.tv_sec-last_rx->tv_sec);
}
last_rx->tv_usec = pkt.rx.tv_usec;
last_rx->tv_sec = pkt.rx.tv_sec;
if(rtOpts->show_rxtx_data == DEBUG_DETAIL)
printf("n %4i transmision time: %9li.%06li [s] tx_size: %i rx_size: %i\n",
pkt.seq,
pkt.xx.tv_sec,
pkt.xx.tv_usec,
pkt.size,
pkt.size2);
*data=(int)pkt.seq;
return (ret);
}
int rx_fastest_burst_from(my_socket *sock, wr_sockaddr_t *from, int *data, size_t data_length,
RunTimeOpts *rtOpts, rx_data_t rx_data[])
{
struct my_socket *s = (struct my_socket *)sock;
struct bench_pkt pkt;
struct msghdr msg;
struct iovec entry;
struct sockaddr_ll from_addr;
struct {
struct cmsghdr cm;
char control[1024];
} control;
struct cmsghdr *cmsg;
size_t len = data_length;// + sizeof(struct ethhdr);
signed long timeout = 0;
int ret ;
int rx_msg_id=0;
int total_msg_cnt=0;
int i=0;
int j=0;
int size;
timeval_t last_rx = {0,0};
timeval_t current_rx;
memset(&msg, 0, sizeof(msg));
printf("before while\n");
while(rx_msg_id < rtOpts->pkt_number_in_burst)
{
msg.msg_iov = &entry;
msg.msg_iovlen = 1;
entry.iov_base = &pkt;
entry.iov_len = len;
msg.msg_name = (caddr_t)&from_addr;
msg.msg_namelen = sizeof(from_addr);
msg.msg_control = &control;
msg.msg_controllen = sizeof(control);
ret = recvmsg(s->fd, &msg, MSG_DONTWAIT);
if(ret > 0)
{
timeout = 0;
gettimeofday(&rx_data[total_msg_cnt].rx, NULL);
if (!pkt.size2) pkt.size2 = ret;
size = ret;
rx_msg_id = pkt.seq;
rx_data[total_msg_cnt].rx_seq_id = pkt.seq;
rx_data[total_msg_cnt].tx.tv_usec = pkt.tx.tv_usec;
rx_data[total_msg_cnt].tx.tv_sec = pkt.tx.tv_sec;
// last_rx.tv_sec = rx_data[i].tx.tv_sec;
// printf("rx_msg_id=%d, total_msg_cnt=%d, i=%d,j=%d\n",rx_msg_id,total_msg_cnt,i,j);
rx_msg_id++;
total_msg_cnt++;
i++;
j=j+1;
}
if(timeout > 1000000)
{
printf("timetout\n");
break;
}
else if(rx_msg_id != 0)
timeout++;
}
*data = size;//size
return (total_msg_cnt);
}
int tx_burst_to(my_socket *sock, wr_sockaddr_t *to, int burst_id, int burst_number,
size_t data_length, int pkt_time_interval, RunTimeOpts *rtOpts)
{
struct bench_pkt *p;
int i, delta, slen, pktn, pktt, pkts;
struct my_socket *s = (struct my_socket *)sock;
struct sockaddr_ll sll;
int rval;
int promil = burst_number/1000;
int percent = burst_number/100;
struct timeval tsend, tstamp;
if(data_length > ETHER_MTU-8) return -EINVAL;
if(data_length < sizeof(struct bench_pkt)) return -EINVAL;
/* alloc packet */
p = (bench_pkt*)calloc(1, data_length);
if (!p) exit(5);
p->burst_id = burst_id;
p->size = data_length;
if(data_length < 60) /* pad to the minimum allowed packet size */
data_length = 60;
memset(&sll, 0, sizeof(struct sockaddr_ll));
sll.sll_ifindex = s->if_index;
sll.sll_family = AF_PACKET;
sll.sll_protocol = htons(to->ethertype);
sll.sll_halen = 6;
memcpy(sll.sll_addr, to->mac, sizeof(mac_addr_t));
gettimeofday(&tsend, NULL); /* time of prev-than-first tx */
for (i=0; i<burst_number; i++)
{
p->seq = i;
/* wait tx time */
if(pkt_time_interval)
{
tsend.tv_usec += pkt_time_interval;
if (tsend.tv_usec > 1000*1000)
{
tsend.tv_usec -= 1000*1000;
tsend.tv_sec++;
}
gettimeofday(&tstamp, NULL);
delta = (tsend.tv_sec - tstamp.tv_sec)*1000*1000+tsend.tv_usec - tstamp.tv_usec;
if (delta < 0) delta = 0;
usleep(delta);
}
/* stamp packet and send it */
gettimeofday(&p->tx, NULL);
rval = sendto(s->fd, p, data_length, 0, (struct sockaddr *)&sll,sizeof(struct sockaddr_ll));
if(rval<0)
{
printf("Sending burst failed at frame %d\n",i);
}
if((rtOpts->show_rxtx_data == DEBUG_LIGHT) && (i%percent) == 0)
printf("send %d \%\n",i/percent);
}
return i;
}
/*
* send burst as fast as possible, record time of sending to calculate average frame
* sending intervals
*/
int tx_fasters_burst_to(my_socket *sock, wr_sockaddr_t *to, int burst_id, int burst_number,
size_t data_length, RunTimeOpts *rtOpts, timeval_t tx_timestamps[],
int pkt_time_interval)
{
struct bench_pkt *p;
int i, delta, slen, pktn, pktt, pkts;
struct my_socket *s = (struct my_socket *)sock;
struct sockaddr_ll sll;
int rval;
if(data_length > ETHER_MTU-8) return -EINVAL;
if(data_length < sizeof(struct bench_pkt)) return -EINVAL;
/* alloc packet */
p = (bench_pkt*)calloc(1, data_length);
if (!p) exit(5);
p->burst_id = burst_id;
p->size = data_length;
if(data_length < 60) /* pad to the minimum allowed packet size */
data_length = 60;
memset(&sll, 0, sizeof(struct sockaddr_ll));
sll.sll_ifindex = s->if_index;
sll.sll_family = AF_PACKET;
sll.sll_protocol = htons(to->ethertype);
sll.sll_halen = 6;
memcpy(sll.sll_addr, to->mac, sizeof(mac_addr_t));
for (i=0; i<burst_number; i++)
{
p->seq = i;
/* stamp packet and send it */
gettimeofday(&p->tx, NULL);
rval = sendto(s->fd, p, data_length, 0, (struct sockaddr *)&sll,sizeof(struct sockaddr_ll));
tx_timestamps[i].tv_usec = p->tx.tv_usec;
tx_timestamps[i].tv_sec = p->tx.tv_sec;
if(rval<0)
{
printf("Sending burst failed at frame %d\n",i);
}
delay(pkt_time_interval);
}
return i;
}
int txto(my_socket *sock, wr_sockaddr_t *to, void *data, size_t data_length)
{
struct etherpacket pkt;
struct my_socket *s = (struct my_socket *)sock;
struct sockaddr_ll sll;
int rval;
if(data_length > ETHER_MTU) return -EINVAL;
if(data_length < 60) /* pad to the minimum allowed packet size */
data_length = 60;
memset(&sll, 0, sizeof(struct sockaddr_ll));
sll.sll_ifindex = s->if_index;
sll.sll_family = AF_PACKET;
sll.sll_protocol = htons(to->ethertype);
sll.sll_halen = 6;
memcpy(sll.sll_addr, to->mac, sizeof(mac_addr_t));
rval = sendto(s->fd, data, data_length, 0, (struct sockaddr *)&sll,
sizeof(struct sockaddr_ll));
return rval;
}
int startup(int argc, char **argv, RunTimeOpts *rtOpts)
{
int c,nr;
printf("start\n");
/// default values
rtOpts->rx_do = 0;
rtOpts->rx_burst_do = 0;
rtOpts->tx_burst_do = 0;
rtOpts->tx_do = 0;
rtOpts->ethertype = ETHER_TYPE;
rtOpts->pkt_time_interval = 0;
rtOpts->pkt_number_in_burst = 100;
rtOpts->pkt_payload_size = 500;
rtOpts->show_rxtx_data =DEBUG_NO;
memcpy(rtOpts->dst_mac, DEFAULT_UNICAST_MAC, sizeof(mac_addr_t));
while( (c = getopt(argc, argv, "?r:t:f:s:n:u:mbe:v:z:h:j:q:g:")) != -1 ) {
switch(c) {
case '?':
printf(
"\nUsage: networkTool [OPTION]\n\n"
"\n"
"-? show this page\n"
"\n"
"-r if_name receive frames on interface if_name\n"
"-t if_name transmit signle frame on interface if_name\n"
"-f if_name flood (burst) frames on interface if_name\n"
"-s if_name receive burst of frames on interface if_name\n"
"-n frame_number number of frames in received/transmitted burst\n"
"-j power we send 10^power of frames\n"
"-u number send to unicast \n"
" 0: %x:%x:%x:%x:%x:%x (default)\n"
" 1: %x:%x:%x:%x:%x:%x (eth5)\n"
" 2: %x:%x:%x:%x:%x:%x (eth4_rename)\n"
"-m send to default unicast mac:%x:%x:%x:%x:%x:%x\n"
"-b send to broadcast: FF:FF:FF:FF:FF:FF\n"
"-g MAC send to specified MAC (format: AA:BB:CC:DD:FF:11)\n"
"-e ethertype set ethertype\n"
"-v time_interval set time interval between frames in burts (not optimal, smallest 60[us])\n"
"-q time_interval set time interval between frames in burst (using optimized functions)"
" can be used also during reception then rx_fasters_burst_from() used "
"-z payload_size set payload size\n"
"-h show rx/tx data (e.g.: time)\n"
"\n",
DEFAULT_UNICAST_MAC[0],DEFAULT_UNICAST_MAC[1],DEFAULT_UNICAST_MAC[2],
DEFAULT_UNICAST_MAC[3],DEFAULT_UNICAST_MAC[4],DEFAULT_UNICAST_MAC[5],
UNICAST_MAC_ETH_5[0],UNICAST_MAC_ETH_5[1],UNICAST_MAC_ETH_5[2],
UNICAST_MAC_ETH_5[3],UNICAST_MAC_ETH_5[4],UNICAST_MAC_ETH_5[5],
UNICAST_MAC_ETH_4_RENAME[0],UNICAST_MAC_ETH_4_RENAME[1],UNICAST_MAC_ETH_4_RENAME[2],
UNICAST_MAC_ETH_4_RENAME[3],UNICAST_MAC_ETH_4_RENAME[4],UNICAST_MAC_ETH_4_RENAME[5],
DEFAULT_MULTICAST_MAC[0],DEFAULT_MULTICAST_MAC[1],DEFAULT_MULTICAST_MAC[2],
DEFAULT_MULTICAST_MAC[3],DEFAULT_MULTICAST_MAC[4],DEFAULT_MULTICAST_MAC[5]
);
return 0;
case 'r':
memset(rtOpts->rx_if_name, 0, IFACE_NAME_LEN);
strncpy(rtOpts->rx_if_name, optarg, IFACE_NAME_LEN);
printf("Receive on interface: %s \n",rtOpts->rx_if_name);
rtOpts->rx_do = 1;
break;
case 't':
memset(rtOpts->tx_if_name, 0, IFACE_NAME_LEN);
strncpy(rtOpts->tx_if_name, optarg, IFACE_NAME_LEN);
printf("Transmit on interface: %s \n",rtOpts->tx_if_name);
rtOpts->tx_do = 1;
break;
case 'f':
memset(rtOpts->tx_if_name, 0, IFACE_NAME_LEN);
strncpy(rtOpts->tx_if_name, optarg, IFACE_NAME_LEN);
printf("Transmit burst on interface: %s \n",rtOpts->tx_if_name);
rtOpts->tx_burst_do = 1;
break;
case 's':
memset(rtOpts->rx_if_name, 0, IFACE_NAME_LEN);
strncpy(rtOpts->rx_if_name, optarg, IFACE_NAME_LEN);
printf("Receive burst on interface: %s \n",rtOpts->rx_if_name);
rtOpts->rx_burst_do = 1;
break;
case 'n':
rtOpts->pkt_number_in_burst = strtol(optarg, &optarg, 0);
printf("Number of frames in the burst: %d \n",rtOpts->pkt_number_in_burst);
break;
case 'j':
rtOpts->pkt_number_in_burst = pow(10,strtol(optarg, &optarg, 0));
printf("Number of frames in the burst: %d \n",rtOpts->pkt_number_in_burst);
break;
case 'u':
nr= strtol(optarg, &optarg, 0);
if(nr == 1)
memcpy(rtOpts->dst_mac, UNICAST_MAC_ETH_5, sizeof(mac_addr_t));
else if (nr == 2)
memcpy(rtOpts->dst_mac, UNICAST_MAC_ETH_4_RENAME, sizeof(mac_addr_t));
else
memcpy(rtOpts->dst_mac, DEFAULT_UNICAST_MAC, sizeof(mac_addr_t));
printf("Send/Receive at unicast mac: %x:%x:%x:%x:%x:%x \n",
rtOpts->dst_mac[0],
rtOpts->dst_mac[1],
rtOpts->dst_mac[2],
rtOpts->dst_mac[3],
rtOpts->dst_mac[4],
rtOpts->dst_mac[5]);
break;
case 'm':
memcpy(rtOpts->dst_mac, DEFAULT_MULTICAST_MAC, sizeof(mac_addr_t));
printf("Send/Receive at multicast mac: %x:%x:%x:%x:%x:%x \n",
rtOpts->dst_mac[0],
rtOpts->dst_mac[1],
rtOpts->dst_mac[2],
rtOpts->dst_mac[3],
rtOpts->dst_mac[4],
rtOpts->dst_mac[5]);
break;
case 'b':
memcpy(rtOpts->dst_mac, BROADCAST_MAC, sizeof(mac_addr_t));
printf("Send/Receive at broadcast mac: %x:%x:%x:%x:%x:%x \n",
rtOpts->dst_mac[0],
rtOpts->dst_mac[1],
rtOpts->dst_mac[2],
rtOpts->dst_mac[3],
rtOpts->dst_mac[4],
rtOpts->dst_mac[5]);
break;
case 'e':
rtOpts->ethertype = strtol(optarg, &optarg, 0);
printf("Ethertype: %d\n", rtOpts->ethertype);
break;
case 'v':
rtOpts->pkt_time_interval = strtol(optarg, &optarg, 0);
printf("Time interval between bursts: %d[us]\n", rtOpts->pkt_time_interval);
break;
case 'q':
rtOpts->pkt_time_interval = strtol(optarg, &optarg, 0);
if(rtOpts->tx_burst_do == 1)
{
rtOpts->tx_burst_do = 2; // fasters burst
if(rtOpts->pkt_time_interval == 0)
printf("Time interval between bursts[optimized]: as fast as possible\n");
else
printf("Time interval between bursts[optimized]: %d nops\n",rtOpts->pkt_time_interval);
}
else if(rtOpts->pkt_time_interval==0 && rtOpts->rx_burst_do == 1)
{
rtOpts->rx_burst_do = 2; // fasters burst
printf("Optimized function for burst reception: as fast as possible\n");
}
else
printf("to use -q number, you need to specify -s or -f");
break;
case 'z':
rtOpts->pkt_payload_size = strtol(optarg, &optarg, 0);
printf("Payload size: %d\n", rtOpts->pkt_payload_size);
break;
case 'h':
rtOpts->show_rxtx_data = strtol(optarg, &optarg, 0);
printf("Show rx/tx data (0=no debug, 1=light debug, 2=detail debug\n");
break;
case 'g':
// optarg
printf("test output %s\n",optarg);
sscanf(optarg,"%x:%x:%x:%x:%x:%x",
&rtOpts->dst_mac[0],
&rtOpts->dst_mac[1],
&rtOpts->dst_mac[2],
&rtOpts->dst_mac[3],
&rtOpts->dst_mac[4],
&rtOpts->dst_mac[5]);
printf("Send/Receive at multicast mac: %x:%x:%x:%x:%x:%x \n",
rtOpts->dst_mac[0],
rtOpts->dst_mac[1],
rtOpts->dst_mac[2],
rtOpts->dst_mac[3],
rtOpts->dst_mac[4],
rtOpts->dst_mac[5]);
break;
default:
printf("Wrong arguments\n"
"\nUsage: networkTool [OPTION]\n\n");
return -1;
break;
}
}
if(rtOpts->tx_burst_do == 2 && rtOpts->pkt_number_in_burst > MAX_BURST)
{
rtOpts->pkt_number_in_burst = MAX_BURST;
printf("Cannot flood more then %d frames with the fasters burst\n",MAX_BURST);
}
return 0;
}
int initialize(my_socket_t *socket_tx, my_socket_t *socket_rx, RunTimeOpts *rtOpts)
{
///todo
}
int send_burst(my_socket_t *socket_tx, int burst_id, size_t payload_size, RunTimeOpts *rtOpts)
{
wr_sockaddr_t bindaddr_tx;
int ret;
strcpy(bindaddr_tx.if_name, rtOpts->tx_if_name); //TODO
bindaddr_tx.ethertype = rtOpts->ethertype; // FEC's etherType, fecked
memcpy(bindaddr_tx.mac, rtOpts->dst_mac, sizeof(mac_addr_t));
printf("Burst of frames...:frame number = %d, frame payload = %d [bytes], interval = %d [us]\n",
rtOpts->pkt_number_in_burst,payload_size,rtOpts->pkt_time_interval);
ret = tx_burst_to(socket_tx,&bindaddr_tx,burst_id,rtOpts->pkt_number_in_burst,
payload_size,rtOpts->pkt_time_interval, rtOpts);
if(ret != rtOpts->pkt_number_in_burst)
{
printf("Failed to send a burst of frames:\n");
printf("\tframe number : %d \n",ret);
return -1;
}
else
{
printf("Send a burst of frames:\n");
printf("\tframe number : %d \n",ret);
printf("\tburst id : %d \n",burst_id);
printf("\tframe payload : %d [bytes]\n",payload_size);
printf("\tinterval between frames : %d [us]\n",rtOpts->pkt_time_interval);
}
return 0;
}
int send_fasters_burst(my_socket_t *socket_tx, int burst_id, size_t payload_size, RunTimeOpts *rtOpts)
{
wr_sockaddr_t bindaddr_tx;
// timeval_t tx_timestamps[1000000];
timeval_t *tx_timestamps;
int ret,i;
long acc_interval = 0;
tx_timestamps = (timeval_t*)calloc(MAX_BURST,sizeof(timeval_t));
if (!tx_timestamps) exit(5);
strcpy(bindaddr_tx.if_name, rtOpts->tx_if_name); //TODO
bindaddr_tx.ethertype = rtOpts->ethertype; // FEC's etherType, fecked
memcpy(bindaddr_tx.mac, rtOpts->dst_mac, sizeof(mac_addr_t));
if(rtOpts->pkt_time_interval == 0)
printf("Burst of frames...:frame number = %d, frame payload = %d [bytes], interval = faster\n",
rtOpts->pkt_number_in_burst,payload_size);
else
printf("Burst of frames...:frame number = %d, frame payload = %d [bytes], interval = %d [nops]\n",
rtOpts->pkt_number_in_burst,payload_size, rtOpts->pkt_time_interval);
// usleep(10000);
ret = tx_fasters_burst_to(socket_tx,&bindaddr_tx,burst_id,rtOpts->pkt_number_in_burst,
payload_size, rtOpts,tx_timestamps,rtOpts->pkt_time_interval);
if(ret != rtOpts->pkt_number_in_burst)
{
printf("Failed to send a burst of frames:\n");
printf("\tframe number : %d \n",ret);
return -1;
}
else
{
for(i=1;i<rtOpts->pkt_number_in_burst;i++)
{
if((tx_timestamps[i].tv_usec-tx_timestamps[i-1].tv_usec)<0)
{
acc_interval += (long)(1000*1000+tx_timestamps[i].tv_usec-tx_timestamps[i-1].tv_usec)
+ 1000*1000*(long)(tx_timestamps[i].tv_sec-tx_timestamps[i-1].tv_sec-1);
}
else
{
acc_interval += (long)(tx_timestamps[i].tv_usec-tx_timestamps[i-1].tv_usec)
+ 1000*1000*(long)(tx_timestamps[i].tv_sec-tx_timestamps[i-1].tv_sec);
}
if(rtOpts->show_rxtx_data == DEBUG_DETAIL)
printf("n %4i transmision time: %9li.%06li [s], acc_interval: %ld\n",
i,
tx_timestamps[i].tv_sec,
tx_timestamps[i].tv_usec,
acc_interval);
}
printf("Send a burst of frames:\n");
printf("\tframe number : %d \n",ret);
printf("\tburst id : %d \n",burst_id);
printf("\tframe payload : %d [bytes]\n",payload_size);
printf("\tinterval between frames : %d [us]\n",acc_interval/ret);
// printf("\taccumulated interval : %d [us]\n",acc_interval);
}
return 0;
}
int receive_burst(my_socket_t *socket_rx, RunTimeOpts *rtOpts)
{
timeval_t last_rx = {0,0};
timeval_t current_rx;
long acc_latency = 0;
long acc_interval = 0;
int timeout = 0;
wr_sockaddr_t bindaddr_rx;
int ret, msg_cnt=0;
int size;
int total_msg_cnt = 0;
long total_delay=0;
int seq_id ;
int promil = rtOpts->pkt_number_in_burst/1000;
int percent = rtOpts->pkt_number_in_burst/100;
unsigned char msg_rx[PACKET_SIZE];
strcpy(bindaddr_rx.if_name, rtOpts->rx_if_name); //TODO
bindaddr_rx.ethertype = rtOpts->ethertype; // FEC's etherType, fecked
memcpy(bindaddr_rx.mac, rtOpts->dst_mac, sizeof(mac_addr_t));
printf("Waiting to receive a burst of frames....\n");
while((msg_cnt < rtOpts->pkt_number_in_burst) &&
( (msg_cnt == 0) || (current_rx.tv_sec < (last_rx.tv_sec+5))))
{
ret = rx_seq_from(socket_rx, &bindaddr_rx, &seq_id, PACKET_SIZE,rtOpts,
&acc_latency, &acc_interval, &last_rx);
if(ret > 0)
{
size = ret;
if((rtOpts->show_rxtx_data == DEBUG_LIGHT) && (msg_cnt%percent) == 0)
printf("received %d \%\n",msg_cnt/percent);
if(msg_cnt != seq_id)
{
printf("Lost %d frames [is rx_id = %d, should be seq_id=%d]\n",(seq_id-msg_cnt),seq_id, msg_cnt);
msg_cnt = seq_id;
}
msg_cnt++;
total_msg_cnt++;
}
gettimeofday(&current_rx, NULL);
}
printf("Received a burst of frames:\n");
printf("frame number : %10d \n",total_msg_cnt);
printf("frame size : %10d [bytes]\n",size);
printf("avg interval : %10d [us]\n",acc_interval/total_msg_cnt);
printf("avg latency : %10d [us]\n",acc_latency/total_msg_cnt);
return 0;
}
int receive_fastest_burst(my_socket_t *socket_rx, RunTimeOpts *rtOpts)
{
long acc_latency = 0;
long acc_interval = 0;
wr_sockaddr_t bindaddr_rx;
int ret, msg_cnt=0;
int size,i;
int data;
int total_msg_cnt = 0;
long total_delay=0;
// rx_data_t rx_data[1000];
rx_data_t *rx_data;
rx_data = (rx_data_t*)calloc(MAX_BURST,sizeof(rx_data_t));
if (!rx_data) exit(5);
strcpy(bindaddr_rx.if_name, rtOpts->rx_if_name); //TODO
bindaddr_rx.ethertype = rtOpts->ethertype; // FEC's etherType, fecked
memcpy(bindaddr_rx.mac, rtOpts->dst_mac, sizeof(mac_addr_t));
printf("Waiting to receive a burst of frames as fast as possible....:\n");
ret = rx_fastest_burst_from(socket_rx, &bindaddr_rx, &size, PACKET_SIZE,rtOpts,rx_data);
msg_cnt=1;
for(i=1;i<ret;i++)
{
if((rx_data[i].rx.tv_usec-rx_data[i-1].rx.tv_usec)<0)
{
acc_interval += (long)(1000*1000+rx_data[i].rx.tv_usec-rx_data[i-1].rx.tv_usec)
+ 1000*1000*(long)(rx_data[i].rx.tv_sec-rx_data[i-1].rx.tv_sec-1);
}
else
{
acc_interval += (long)(rx_data[i].rx.tv_usec-rx_data[i-1].rx.tv_usec)
+ 1000*1000*(long)(rx_data[i].rx.tv_sec-rx_data[i-1].rx.tv_sec);
}
if((rx_data[i].rx.tv_usec-rx_data[i].tx.tv_usec)<0)
{
rx_data[i].xx.tv_usec = 1000*1000+rx_data[i].rx.tv_usec-rx_data[i].tx.tv_usec;
rx_data[i].xx.tv_sec = rx_data[i].rx.tv_sec-rx_data[i].tx.tv_sec-1;
}
else
{
rx_data[i].xx.tv_usec = rx_data[i].rx.tv_usec-rx_data[i].tx.tv_usec;
rx_data[i].xx.tv_sec = rx_data[i].rx.tv_sec-rx_data[i].tx.tv_sec;
}
acc_latency = acc_latency + (long)rx_data[i].xx.tv_usec;
if((msg_cnt != rx_data[i].rx_seq_id) || (rx_data[i].rx_seq_id-rx_data[i-1].rx_seq_id) != 1)
{
printf("n %4i lost %5d frame(s). Should be %10d, is %10d \n",i,
rx_data[i].rx_seq_id-rx_data[i-1].rx_seq_id, msg_cnt,rx_data[i].rx_seq_id);
msg_cnt = rx_data[i].rx_seq_id;
}
msg_cnt++;
if(rtOpts->show_rxtx_data == DEBUG_DETAIL)
printf("n %4i transmision time: %9li.%06li [s], latendy: %ld [us]\n",
i,
rx_data[i].tx.tv_sec,
rx_data[i].tx.tv_usec,
rx_data[i].xx.tv_usec);
}
printf("Received a burst of frames:\n");
printf("frame number : %10d \n",ret);
printf("frame size : %10d [bytes]\n",size);
printf("avg interval : %10d [us]\n",acc_interval/ret);
// printf("acc interval : %10d [us]\n",acc_interval);
printf("avg latency : %10d [us]\n",acc_latency/ret);
return 0;
}
int receive_frames(my_socket_t *socket_rx, RunTimeOpts *rtOpts)
{
wr_sockaddr_t bindaddr_rx;
int ret, msg_cnt=0;
unsigned char msg_rx[PACKET_SIZE];
strcpy(bindaddr_rx.if_name, rtOpts->rx_if_name); //TODO
bindaddr_rx.ethertype = rtOpts->ethertype; // FEC's etherType, fecked
memcpy(bindaddr_rx.mac, rtOpts->dst_mac, sizeof(mac_addr_t));
printf("Waiting to receive a frames....\n");
while(1)
{
ret = rxfrom(socket_rx, &bindaddr_rx, (unsigned char*)msg_rx, PACKET_SIZE);
if(ret > 0)
{
printf("Received a frames:\n");
printf("frame number : %d \n",msg_cnt);
printf("frame size : %d \n",ret);
msg_cnt++;
}
}
return 0;
}
int send_frame(my_socket_t *socket_tx, void *data, size_t payload_size, RunTimeOpts *rtOpts)
{
wr_sockaddr_t bindaddr_tx;
int ret;
strcpy(bindaddr_tx.if_name, rtOpts->tx_if_name); //TODO
bindaddr_tx.ethertype = rtOpts->ethertype; // FEC's etherType, fecked
memcpy(bindaddr_tx.mac, rtOpts->dst_mac, sizeof(mac_addr_t));
ret = txto(socket_tx,&bindaddr_tx,data,payload_size);
if(ret != payload_size)
{
printf("Failed to send a frames:\n");
printf("\tframe payload size : %d [bytes] \n",payload_size);
return -1;
}
else
{
printf("Send a frames:\n");
printf("\tframe payload : %d [bytes]\n",payload_size);
}
return 0;
}
int main(int argc, char **argv)
{
struct my_socket *socket_tx, *socket_rx;
wr_sockaddr_t bindaddr_tx, bindaddr_rx;
int recv_msg_size, tx_msg_size, i;
unsigned char msg_rx[PACKET_SIZE];
unsigned char msg_tx[PACKET_SIZE];
int payload_size = 500;
RunTimeOpts rtOpts;
if(startup(argc,argv,&rtOpts)<0)
return -1;
///TODO: put it into function
/// tx
if(rtOpts.tx_do || rtOpts.tx_burst_do)
{
strcpy(bindaddr_tx.if_name, rtOpts.tx_if_name); //TODO
bindaddr_tx.ethertype = rtOpts.ethertype; // FEC's etherType, fecked
memcpy(bindaddr_tx.mac, rtOpts.dst_mac, sizeof(mac_addr_t));
socket_tx = create_socket(&bindaddr_tx, &rtOpts);
if(socket_tx == NULL)
{
printf("problem with socket_tx\n");
return -1;
}
else
{
printf("created socket_tx: %d\n",socket_tx);
}
}
/// rx
if(rtOpts.rx_do || rtOpts.rx_burst_do)
{
strcpy(bindaddr_rx.if_name, rtOpts.rx_if_name); //TODO
bindaddr_rx.ethertype = rtOpts.ethertype; // FEC's etherType, fecked
memcpy(bindaddr_rx.mac, rtOpts.dst_mac, sizeof(mac_addr_t));
socket_rx = create_socket(&bindaddr_rx, &rtOpts);
if(socket_rx == NULL)
{
printf("problem with socket_rx\n");
return -1;
}
{
printf("created socket_rx: %d\n",socket_rx);
}
}
int cnt=1;
if(rtOpts.tx_burst_do == 1)
send_burst(socket_tx, 0, rtOpts.pkt_payload_size, &rtOpts);
if(rtOpts.tx_burst_do == 2)
send_fasters_burst(socket_tx, 0, rtOpts.pkt_payload_size, &rtOpts);
if(rtOpts.tx_do)
{
printf("Single frame...\n");
for(i=0;i<rtOpts.pkt_payload_size;i++)
msg_tx[i] = i;
send_frame(socket_tx, &msg_tx, rtOpts.pkt_payload_size, &rtOpts);
}
if(rtOpts.rx_burst_do == 1)
receive_burst(socket_rx, &rtOpts);
if(rtOpts.rx_burst_do == 2)
{
printf("receive_fastest_burst\n");
receive_fastest_burst(socket_rx, &rtOpts);
}
if(rtOpts.rx_do)
receive_frames(socket_rx, &rtOpts);
return 1;
}
#ifndef _fec_decoder_h
#define _fec_decoder_h
#include <stdio.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <linux/errqueue.h>
#include <linux/sockios.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <asm/types.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <math.h>
#define DEMO__
//#define DEMO__ // makes a binary for the demo
#include <asm/socket.h>
#define PTPD_SOCK_RAW_ETHERNET 1
#define PTPD_SOCK_UDP 2
#define PACKET_SIZE 1518
#define IFACE_NAME_LEN 16
#define ETHER_MTU 1518
#define PACKED __attribute__((packed))
#define DEBUG_DETAIL 2
#define DEBUG_LIGHT 1
#define DEBUG_NO 0
// FECed frame header size [bytes]
#define FEC_HEADE_SIZE 8
#define ETHER_TYPE 0xDEED
#define MAX_BURST 10000000
typedef uint32_t ipv4_addr_t;
typedef char Octet;
typedef uint8_t mac_addr_t[6];
const mac_addr_t FEC_ADDR = {0x76, 0x54, 0xba, 0x98 , 0xfe, 0xdc};
const mac_addr_t DEFAULT_UNICAST_MAC = {0x00,0x01,0x02,0x03,004,0x05 };
const mac_addr_t UNICAST_MAC_ETH_5 = {0x00,0x1b,0x21,0x8e,0xd7,0x44 };
const mac_addr_t UNICAST_MAC_ETH_4_RENAME = {0x00,0x1b,0x21,0x8e,0xd7,0x45 };
const mac_addr_t DEFAULT_MULTICAST_MAC= {0x01,0x01,0x02,0x03,004,0x05 };
const mac_addr_t BROADCAST_MAC = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF };
PACKED struct etherpacket {
struct ethhdr ether;
char data[ETHER_MTU];
};
PACKED struct bench_pkt {
uint32_t burst_id;
uint32_t seq;
uint16_t size, size2;
struct timeval tx;
struct timeval xx;
struct timeval rx;
char payload[0];
};
enum rxfrom_mode {
RETURN_SEQ_NUM,
RETURN_DATA};
typedef struct timeval timeval_t;
typedef void *wr_socket_t;
typedef struct{
int rx_seq_id;
timeval_t rx; //reception timestamp
timeval_t tx; //transmision timestamp
timeval_t xx; //latency time tx-to-rx
} rx_data_t;
typedef struct {
// Network interface name (eth0, ...)
char if_name[IFACE_NAME_LEN];
// Socket family (RAW ethernet/UDP)
int family;
// MAC address
mac_addr_t mac;
// Destination MASC address, filled by recvfrom() function on interfaces bound to multiple addresses
mac_addr_t mac_dest;
// IP address
ipv4_addr_t ip;
// UDP port
uint16_t port;
// RAW ethertype
uint16_t ethertype;
// physical port to bind socket to
uint16_t physical_port;
} wr_sockaddr_t;
struct my_socket {
int fd;
wr_sockaddr_t bind_addr;
mac_addr_t local_mac;
int if_index;
} ;
typedef struct my_socket my_socket_t;
typedef struct {
char rx_if_name[IFACE_NAME_LEN];
char tx_if_name[IFACE_NAME_LEN];
int rx_do;
int rx_burst_do;
int tx_burst_do;
int tx_do;
uint16_t ethertype;
mac_addr_t dst_mac;
int pkt_time_interval;
int pkt_number_in_burst;
size_t pkt_payload_size;
int show_rxtx_data;
} RunTimeOpts;
#endif
\ No newline at end of file
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