[ipoib] Simplify test for received broadcast packets
[ipxe.git] / src / interface / linux / linux_timer.c
1 /*
2 * Copyright (C) 2010 Piotr JaroszyƄski <p.jaroszynski@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 FILE_LICENCE(GPL2_OR_LATER);
20
21 #include <stddef.h>
22 #include <ipxe/timer.h>
23
24 #include <linux_api.h>
25
26 /** @file
27 *
28 * iPXE timer API for linux
29 *
30 */
31
32 /**
33 * Delay for a fixed number of microseconds
34 *
35 * @v usecs Number of microseconds for which to delay
36 */
37 static void linux_udelay(unsigned long usecs)
38 {
39 linux_usleep(usecs);
40 }
41
42 /**
43 * Get number of ticks per second
44 *
45 * @ret ticks_per_sec Number of ticks per second
46 */
47 static unsigned long linux_ticks_per_sec(void)
48 {
49 return 1000;
50 }
51
52 /**
53 * Get current system time in ticks
54 *
55 * linux doesn't provide an easy access to jiffies so implement it by measuring
56 * the time since the first call to this function.
57 *
58 * Since this function is used to seed the (non-cryptographic) random
59 * number generator, we round the start time down to the nearest whole
60 * second. This minimises the chances of generating identical RNG
61 * sequences (and hence identical TCP port numbers, etc) on
62 * consecutive invocations of iPXE.
63 *
64 * @ret ticks Current time, in ticks
65 */
66 static unsigned long linux_currticks(void)
67 {
68 static struct timeval start;
69 static int initialized = 0;
70
71 if (! initialized) {
72 linux_gettimeofday(&start, NULL);
73 initialized = 1;
74 }
75
76 struct timeval now;
77 linux_gettimeofday(&now, NULL);
78
79 unsigned long ticks = (now.tv_sec - start.tv_sec) * linux_ticks_per_sec();
80 ticks += now.tv_usec / (long)(1000000 / linux_ticks_per_sec());
81
82 return ticks;
83 }
84
85 PROVIDE_TIMER(linux, udelay, linux_udelay);
86 PROVIDE_TIMER(linux, currticks, linux_currticks);
87 PROVIDE_TIMER(linux, ticks_per_sec, linux_ticks_per_sec);