/*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <config.h>
#include "socket-util.h"
#include <arpa/inet.h>
+#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
}
}
+static int
+set_dscp(int fd, uint8_t dscp)
+{
+ if (dscp > 63) {
+ return EINVAL;
+ }
+
+ dscp = dscp << 2;
+ if (setsockopt(fd, IPPROTO_IP, IP_TOS, &dscp, sizeof dscp)) {
+ return errno;
+ }
+
+ return 0;
+}
+
static bool
rlim_is_finite(rlim_t limit)
{
* and stores -1 into '*fdp'.
*
* If 'sinp' is non-null, then on success the target address is stored into
- * '*sinp'. */
+ * '*sinp'.
+ *
+ * 'dscp' becomes the DSCP bits in the IP headers for the new connection. It
+ * should be in the range [0, 63] and will automatically be shifted to the
+ * appropriately place in the IP tos field. */
int
inet_open_active(int style, const char *target, uint16_t default_port,
- struct sockaddr_in *sinp, int *fdp)
+ struct sockaddr_in *sinp, int *fdp, uint8_t dscp)
{
struct sockaddr_in sin;
int fd = -1;
}
error = set_nonblocking(fd);
if (error) {
- goto exit_close;
+ goto exit;
+ }
+
+ /* The dscp bits must be configured before connect() to ensure that the TOS
+ * field is set during the connection establishment. If set after
+ * connect(), the handshake SYN frames will be sent with a TOS of 0. */
+ error = set_dscp(fd, dscp);
+ if (error) {
+ VLOG_ERR("%s: socket: %s", target, strerror(error));
+ goto exit;
}
/* Connect. */
error = connect(fd, (struct sockaddr *) &sin, sizeof sin) == 0 ? 0 : errno;
if (error == EINPROGRESS) {
error = EAGAIN;
- } else if (error && error != EAGAIN) {
- goto exit_close;
}
- /* Success: error is 0 or EAGAIN. */
- goto exit;
-
-exit_close:
- close(fd);
exit:
if (!error || error == EAGAIN) {
if (sinp) {
*sinp = sin;
}
- *fdp = fd;
- } else {
- *fdp = -1;
+ } else if (fd >= 0) {
+ close(fd);
}
+ *fdp = fd;
return error;
}
* negative errno value.
*
* If 'sinp' is non-null, then on success the bound address is stored into
- * '*sinp'. */
+ * '*sinp'.
+ *
+ * 'dscp' becomes the DSCP bits in the IP headers for the new connection. It
+ * should be in the range [0, 63] and will automatically be shifted to the
+ * appropriately place in the IP tos field. */
int
inet_open_passive(int style, const char *target, int default_port,
- struct sockaddr_in *sinp)
+ struct sockaddr_in *sinp, uint8_t dscp)
{
struct sockaddr_in sin;
int fd = 0, error;
goto error;
}
+ /* The dscp bits must be configured before connect() to ensure that the TOS
+ * field is set during the connection establishment. If set after
+ * connect(), the handshake SYN frames will be sent with a TOS of 0. */
+ error = set_dscp(fd, dscp);
+ if (error) {
+ VLOG_ERR("%s: socket: %s", target, strerror(error));
+ goto error;
+ }
+
/* Listen. */
if (style == SOCK_STREAM && listen(fd, 10) < 0) {
error = errno;