packets.h \
poll-loop.h \
queue.h \
+ random.h \
rconn.h \
socket-util.h \
util.h \
#include <stdint.h>
#include <string.h>
+#include "random.h"
#include "util.h"
#define ETH_ADDR_LEN 6
{
return !memcmp(a, b, ETH_ADDR_LEN);
}
+static inline uint64_t eth_addr_to_uint64(const uint8_t ea[ETH_ADDR_LEN])
+{
+ return (((uint64_t) ea[0] << 40)
+ | ((uint64_t) ea[1] << 32)
+ | ((uint64_t) ea[2] << 24)
+ | ((uint64_t) ea[3] << 16)
+ | ((uint64_t) ea[4] << 8)
+ | ea[5]);
+}
+static inline void eth_addr_from_uint64(uint64_t x, uint8_t ea[ETH_ADDR_LEN])
+{
+ ea[0] = x >> 40;
+ ea[1] = x >> 32;
+ ea[2] = x >> 24;
+ ea[3] = x >> 16;
+ ea[4] = x >> 8;
+ ea[5] = x;
+}
+static inline void eth_addr_random(uint8_t ea[ETH_ADDR_LEN])
+{
+ random_bytes(ea, ETH_ADDR_LEN);
+}
+
#define ETH_ADDR_FMT \
"%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
#define ETH_ADDR_ARGS(ea) \
--- /dev/null
+/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
+ * Junior University
+ *
+ * We are making the OpenFlow specification and associated documentation
+ * (Software) available for public use and benefit with the expectation
+ * that others will use, modify and enhance the Software and contribute
+ * those enhancements back to the community. However, since we would
+ * like to make the Software available for broadest use, with as few
+ * restrictions as possible permission is hereby granted, free of
+ * charge, to any person obtaining a copy of this Software to deal in
+ * the Software under the copyrights without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * The name and trademarks of copyright holder(s) may NOT be used in
+ * advertising or publicity pertaining to the Software or any
+ * derivatives without specific, written prior permission.
+ */
+
+#ifndef RANDOM_H
+#define RANDOM_H 1
+
+#include <stddef.h>
+#include <stdint.h>
+
+void random_bytes(void *, size_t);
+uint32_t random_uint32(void);
+
+#endif /* random.h */
ofp-print.c \
poll-loop.c \
queue.c \
+ random.c \
rconn.c \
socket-util.c \
util.c \
--- /dev/null
+/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
+ * Junior University
+ *
+ * We are making the OpenFlow specification and associated documentation
+ * (Software) available for public use and benefit with the expectation
+ * that others will use, modify and enhance the Software and contribute
+ * those enhancements back to the community. However, since we would
+ * like to make the Software available for broadest use, with as few
+ * restrictions as possible permission is hereby granted, free of
+ * charge, to any person obtaining a copy of this Software to deal in
+ * the Software under the copyrights without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * The name and trademarks of copyright holder(s) may NOT be used in
+ * advertising or publicity pertaining to the Software or any
+ * derivatives without specific, written prior permission.
+ */
+
+#include "random.h"
+
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/time.h>
+
+#include "util.h"
+
+void
+random_init(void)
+{
+ static bool inited = false;
+ if (!inited) {
+ struct timeval tv;
+ inited = true;
+ if (gettimeofday(&tv, NULL) < 0) {
+ fatal(errno, "gettimeofday");
+ }
+ srand(tv.tv_sec ^ tv.tv_usec);
+ }
+}
+
+void
+random_bytes(void *p_, size_t n)
+{
+ uint8_t *p = p_;
+ random_init();
+ while (n--) {
+ *p++ = rand();
+ }
+}
+
+uint32_t
+random_uint32(void)
+{
+ uint32_t x;
+ random_bytes(&x, sizeof x);
+ return x;
+}
return p - dp->ports;
}
-/* Generates a unique datapath id. It incorporates the datapath index
- * and a hardware address, if available. If not, it generates a random
- * one.
- */
+/* Generates and returns a random datapath id. */
static uint64_t
gen_datapath_id(void)
{
- /* Choose a random datapath id. */
- uint64_t id = 0;
- int i;
-
- srand(time(0));
-
- for (i = 0; i < ETH_ADDR_LEN; i++) {
- id |= (uint64_t)(rand() & 0xff) << (8*(ETH_ADDR_LEN-1 - i));
- }
-
- return id;
+ uint8_t ea[ETH_ADDR_LEN];
+ eth_addr_random(ea);
+ return eth_addr_to_uint64(ea);
}
int
#include "socket-util.h"
#include "openflow.h"
#include "ofp-print.h"
+#include "random.h"
#include "vconn.h"
#include "vconn-ssl.h"
\f
/* Generic commands. */
-static uint32_t
-random_xid(void)
-{
- static bool inited = false;
- if (!inited) {
- struct timeval tv;
- inited = true;
- if (gettimeofday(&tv, NULL) < 0) {
- fatal(errno, "gettimeofday");
- }
- srand(tv.tv_sec ^ tv.tv_usec);
- }
- return rand();
-}
-
static void *
alloc_openflow_buffer(size_t openflow_len, uint8_t type,
struct buffer **bufferp)
oh->version = OFP_VERSION;
oh->type = type;
oh->length = 0;
- oh->xid = random_xid();
+ oh->xid = random_uint32();
return oh;
}