PageRenderTime 70ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/openvswitch/lib/socket-util.c

https://github.com/kevinfhell/dpdk-ovs
C | 1395 lines | 1026 code | 161 blank | 208 comment | 228 complexity | 301dd0a4d2b7b47ca7e860691dbfd79b MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-3.0, Apache-2.0, LGPL-2.1, GPL-2.0, MIT
  1. /*
  2. * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at:
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <config.h>
  17. #include "socket-util.h"
  18. #include <arpa/inet.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <net/if.h>
  22. #include <netdb.h>
  23. #include <poll.h>
  24. #include <stddef.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/ioctl.h>
  29. #include <sys/resource.h>
  30. #include <sys/socket.h>
  31. #include <sys/stat.h>
  32. #include <sys/uio.h>
  33. #include <sys/un.h>
  34. #include <unistd.h>
  35. #include "dynamic-string.h"
  36. #include "fatal-signal.h"
  37. #include "packets.h"
  38. #include "poll-loop.h"
  39. #include "util.h"
  40. #include "vlog.h"
  41. #if AF_PACKET && LINUX_DATAPATH
  42. #include <linux/if_packet.h>
  43. #endif
  44. #ifdef HAVE_NETLINK
  45. #include "netlink-protocol.h"
  46. #include "netlink-socket.h"
  47. #endif
  48. VLOG_DEFINE_THIS_MODULE(socket_util);
  49. /* #ifdefs make it a pain to maintain code: you have to try to build both ways.
  50. * Thus, this file compiles all of the code regardless of the target, by
  51. * writing "if (LINUX_DATAPATH)" instead of "#ifdef __linux__". */
  52. #ifndef LINUX_DATAPATH
  53. #define LINUX_DATAPATH 0
  54. #endif
  55. #ifndef O_DIRECTORY
  56. #define O_DIRECTORY 0
  57. #endif
  58. static int getsockopt_int(int fd, int level, int option, const char *optname,
  59. int *valuep);
  60. /* Sets 'fd' to non-blocking mode. Returns 0 if successful, otherwise a
  61. * positive errno value. */
  62. int
  63. set_nonblocking(int fd)
  64. {
  65. int flags = fcntl(fd, F_GETFL, 0);
  66. if (flags != -1) {
  67. if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1) {
  68. return 0;
  69. } else {
  70. VLOG_ERR("fcntl(F_SETFL) failed: %s", ovs_strerror(errno));
  71. return errno;
  72. }
  73. } else {
  74. VLOG_ERR("fcntl(F_GETFL) failed: %s", ovs_strerror(errno));
  75. return errno;
  76. }
  77. }
  78. void
  79. xset_nonblocking(int fd)
  80. {
  81. if (set_nonblocking(fd)) {
  82. exit(EXIT_FAILURE);
  83. }
  84. }
  85. int
  86. set_dscp(int fd, uint8_t dscp)
  87. {
  88. int val;
  89. if (dscp > 63) {
  90. return EINVAL;
  91. }
  92. val = dscp << 2;
  93. if (setsockopt(fd, IPPROTO_IP, IP_TOS, &val, sizeof val)) {
  94. return errno;
  95. }
  96. return 0;
  97. }
  98. static bool
  99. rlim_is_finite(rlim_t limit)
  100. {
  101. if (limit == RLIM_INFINITY) {
  102. return false;
  103. }
  104. #ifdef RLIM_SAVED_CUR /* FreeBSD 8.0 lacks RLIM_SAVED_CUR. */
  105. if (limit == RLIM_SAVED_CUR) {
  106. return false;
  107. }
  108. #endif
  109. #ifdef RLIM_SAVED_MAX /* FreeBSD 8.0 lacks RLIM_SAVED_MAX. */
  110. if (limit == RLIM_SAVED_MAX) {
  111. return false;
  112. }
  113. #endif
  114. return true;
  115. }
  116. /* Returns the maximum valid FD value, plus 1. */
  117. int
  118. get_max_fds(void)
  119. {
  120. static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
  121. static int max_fds;
  122. if (ovsthread_once_start(&once)) {
  123. struct rlimit r;
  124. if (!getrlimit(RLIMIT_NOFILE, &r) && rlim_is_finite(r.rlim_cur)) {
  125. max_fds = r.rlim_cur;
  126. } else {
  127. VLOG_WARN("failed to obtain fd limit, defaulting to 1024");
  128. max_fds = 1024;
  129. }
  130. ovsthread_once_done(&once);
  131. }
  132. return max_fds;
  133. }
  134. /* Translates 'host_name', which must be a string representation of an IP
  135. * address, into a numeric IP address in '*addr'. Returns 0 if successful,
  136. * otherwise a positive errno value. */
  137. int
  138. lookup_ip(const char *host_name, struct in_addr *addr)
  139. {
  140. if (!inet_aton(host_name, addr)) {
  141. static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
  142. VLOG_ERR_RL(&rl, "\"%s\" is not a valid IP address", host_name);
  143. return ENOENT;
  144. }
  145. return 0;
  146. }
  147. /* Translates 'host_name', which must be a string representation of an IPv6
  148. * address, into a numeric IPv6 address in '*addr'. Returns 0 if successful,
  149. * otherwise a positive errno value. */
  150. int
  151. lookup_ipv6(const char *host_name, struct in6_addr *addr)
  152. {
  153. if (inet_pton(AF_INET6, host_name, addr) != 1) {
  154. static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
  155. VLOG_ERR_RL(&rl, "\"%s\" is not a valid IPv6 address", host_name);
  156. return ENOENT;
  157. }
  158. return 0;
  159. }
  160. /* Translates 'host_name', which must be a host name or a string representation
  161. * of an IP address, into a numeric IP address in '*addr'. Returns 0 if
  162. * successful, otherwise a positive errno value.
  163. *
  164. * Most Open vSwitch code should not use this because it causes deadlocks:
  165. * getaddrinfo() sends out a DNS request but that starts a new flow for which
  166. * OVS must set up a flow, but it can't because it's waiting for a DNS reply.
  167. * The synchronous lookup also delays other activity. (Of course we can solve
  168. * this but it doesn't seem worthwhile quite yet.) */
  169. int
  170. lookup_hostname(const char *host_name, struct in_addr *addr)
  171. {
  172. struct addrinfo *result;
  173. struct addrinfo hints;
  174. if (inet_aton(host_name, addr)) {
  175. return 0;
  176. }
  177. memset(&hints, 0, sizeof hints);
  178. hints.ai_family = AF_INET;
  179. switch (getaddrinfo(host_name, NULL, &hints, &result)) {
  180. case 0:
  181. *addr = ALIGNED_CAST(struct sockaddr_in *,
  182. result->ai_addr)->sin_addr;
  183. freeaddrinfo(result);
  184. return 0;
  185. #ifdef EAI_ADDRFAMILY
  186. case EAI_ADDRFAMILY:
  187. #endif
  188. case EAI_NONAME:
  189. case EAI_SERVICE:
  190. return ENOENT;
  191. case EAI_AGAIN:
  192. return EAGAIN;
  193. case EAI_BADFLAGS:
  194. case EAI_FAMILY:
  195. case EAI_SOCKTYPE:
  196. return EINVAL;
  197. case EAI_FAIL:
  198. return EIO;
  199. case EAI_MEMORY:
  200. return ENOMEM;
  201. #ifdef EAI_NODATA
  202. case EAI_NODATA:
  203. return ENXIO;
  204. #endif
  205. case EAI_SYSTEM:
  206. return errno;
  207. default:
  208. return EPROTO;
  209. }
  210. }
  211. int
  212. check_connection_completion(int fd)
  213. {
  214. static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 10);
  215. struct pollfd pfd;
  216. int retval;
  217. pfd.fd = fd;
  218. pfd.events = POLLOUT;
  219. do {
  220. retval = poll(&pfd, 1, 0);
  221. } while (retval < 0 && errno == EINTR);
  222. if (retval == 1) {
  223. if (pfd.revents & POLLERR) {
  224. ssize_t n = send(fd, "", 1, MSG_DONTWAIT);
  225. if (n < 0) {
  226. return errno;
  227. } else {
  228. VLOG_ERR_RL(&rl, "poll return POLLERR but send succeeded");
  229. return EPROTO;
  230. }
  231. }
  232. return 0;
  233. } else if (retval < 0) {
  234. VLOG_ERR_RL(&rl, "poll: %s", ovs_strerror(errno));
  235. return errno;
  236. } else {
  237. return EAGAIN;
  238. }
  239. }
  240. /* Drain all the data currently in the receive queue of a datagram socket (and
  241. * possibly additional data). There is no way to know how many packets are in
  242. * the receive queue, but we do know that the total number of bytes queued does
  243. * not exceed the receive buffer size, so we pull packets until none are left
  244. * or we've read that many bytes. */
  245. int
  246. drain_rcvbuf(int fd)
  247. {
  248. int rcvbuf;
  249. rcvbuf = get_socket_rcvbuf(fd);
  250. if (rcvbuf < 0) {
  251. return -rcvbuf;
  252. }
  253. while (rcvbuf > 0) {
  254. /* In Linux, specifying MSG_TRUNC in the flags argument causes the
  255. * datagram length to be returned, even if that is longer than the
  256. * buffer provided. Thus, we can use a 1-byte buffer to discard the
  257. * incoming datagram and still be able to account how many bytes were
  258. * removed from the receive buffer.
  259. *
  260. * On other Unix-like OSes, MSG_TRUNC has no effect in the flags
  261. * argument. */
  262. char buffer[LINUX_DATAPATH ? 1 : 2048];
  263. ssize_t n_bytes = recv(fd, buffer, sizeof buffer,
  264. MSG_TRUNC | MSG_DONTWAIT);
  265. if (n_bytes <= 0 || n_bytes >= rcvbuf) {
  266. break;
  267. }
  268. rcvbuf -= n_bytes;
  269. }
  270. return 0;
  271. }
  272. /* Returns the size of socket 'sock''s receive buffer (SO_RCVBUF), or a
  273. * negative errno value if an error occurs. */
  274. int
  275. get_socket_rcvbuf(int sock)
  276. {
  277. int rcvbuf;
  278. int error;
  279. error = getsockopt_int(sock, SOL_SOCKET, SO_RCVBUF, "SO_RCVBUF", &rcvbuf);
  280. return error ? -error : rcvbuf;
  281. }
  282. /* Reads and discards up to 'n' datagrams from 'fd', stopping as soon as no
  283. * more data can be immediately read. ('fd' should therefore be in
  284. * non-blocking mode.)*/
  285. void
  286. drain_fd(int fd, size_t n_packets)
  287. {
  288. for (; n_packets > 0; n_packets--) {
  289. /* 'buffer' only needs to be 1 byte long in most circumstances. This
  290. * size is defensive against the possibility that we someday want to
  291. * use a Linux tap device without TUN_NO_PI, in which case a buffer
  292. * smaller than sizeof(struct tun_pi) will give EINVAL on read. */
  293. char buffer[128];
  294. if (read(fd, buffer, sizeof buffer) <= 0) {
  295. break;
  296. }
  297. }
  298. }
  299. /* Stores in '*un' a sockaddr_un that refers to file 'name'. Stores in
  300. * '*un_len' the size of the sockaddr_un. */
  301. static void
  302. make_sockaddr_un__(const char *name, struct sockaddr_un *un, socklen_t *un_len)
  303. {
  304. un->sun_family = AF_UNIX;
  305. ovs_strzcpy(un->sun_path, name, sizeof un->sun_path);
  306. *un_len = (offsetof(struct sockaddr_un, sun_path)
  307. + strlen (un->sun_path) + 1);
  308. }
  309. /* Stores in '*un' a sockaddr_un that refers to file 'name'. Stores in
  310. * '*un_len' the size of the sockaddr_un.
  311. *
  312. * Returns 0 on success, otherwise a positive errno value. On success,
  313. * '*dirfdp' is either -1 or a nonnegative file descriptor that the caller
  314. * should close after using '*un' to bind or connect. On failure, '*dirfdp' is
  315. * -1. */
  316. static int
  317. make_sockaddr_un(const char *name, struct sockaddr_un *un, socklen_t *un_len,
  318. int *dirfdp)
  319. {
  320. enum { MAX_UN_LEN = sizeof un->sun_path - 1 };
  321. *dirfdp = -1;
  322. if (strlen(name) > MAX_UN_LEN) {
  323. static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
  324. if (LINUX_DATAPATH) {
  325. /* 'name' is too long to fit in a sockaddr_un, but we have a
  326. * workaround for that on Linux: shorten it by opening a file
  327. * descriptor for the directory part of the name and indirecting
  328. * through /proc/self/fd/<dirfd>/<basename>. */
  329. char *dir, *base;
  330. char *short_name;
  331. int dirfd;
  332. dir = dir_name(name);
  333. base = base_name(name);
  334. dirfd = open(dir, O_DIRECTORY | O_RDONLY);
  335. if (dirfd < 0) {
  336. free(base);
  337. free(dir);
  338. return errno;
  339. }
  340. short_name = xasprintf("/proc/self/fd/%d/%s", dirfd, base);
  341. free(dir);
  342. free(base);
  343. if (strlen(short_name) <= MAX_UN_LEN) {
  344. make_sockaddr_un__(short_name, un, un_len);
  345. free(short_name);
  346. *dirfdp = dirfd;
  347. return 0;
  348. }
  349. free(short_name);
  350. close(dirfd);
  351. VLOG_WARN_RL(&rl, "Unix socket name %s is longer than maximum "
  352. "%d bytes (even shortened)", name, MAX_UN_LEN);
  353. } else {
  354. /* 'name' is too long and we have no workaround. */
  355. VLOG_WARN_RL(&rl, "Unix socket name %s is longer than maximum "
  356. "%d bytes", name, MAX_UN_LEN);
  357. }
  358. return ENAMETOOLONG;
  359. } else {
  360. make_sockaddr_un__(name, un, un_len);
  361. return 0;
  362. }
  363. }
  364. /* Binds Unix domain socket 'fd' to a file with permissions 0700. */
  365. static int
  366. bind_unix_socket(int fd, struct sockaddr *sun, socklen_t sun_len)
  367. {
  368. /* According to _Unix Network Programming_, umask should affect bind(). */
  369. mode_t old_umask = umask(0077);
  370. int error = bind(fd, sun, sun_len) ? errno : 0;
  371. umask(old_umask);
  372. return error;
  373. }
  374. /* Creates a Unix domain socket in the given 'style' (either SOCK_DGRAM or
  375. * SOCK_STREAM) that is bound to '*bind_path' (if 'bind_path' is non-null) and
  376. * connected to '*connect_path' (if 'connect_path' is non-null). If 'nonblock'
  377. * is true, the socket is made non-blocking.
  378. *
  379. * Returns the socket's fd if successful, otherwise a negative errno value. */
  380. int
  381. make_unix_socket(int style, bool nonblock,
  382. const char *bind_path, const char *connect_path)
  383. {
  384. int error;
  385. int fd;
  386. fd = socket(PF_UNIX, style, 0);
  387. if (fd < 0) {
  388. return -errno;
  389. }
  390. /* Set nonblocking mode right away, if we want it. This prevents blocking
  391. * in connect(), if connect_path != NULL. (In turn, that's a corner case:
  392. * it will only happen if style is SOCK_STREAM or SOCK_SEQPACKET, and only
  393. * if a backlog of un-accepted connections has built up in the kernel.) */
  394. if (nonblock) {
  395. error = set_nonblocking(fd);
  396. if (error) {
  397. goto error;
  398. }
  399. }
  400. if (bind_path) {
  401. struct sockaddr_un un;
  402. socklen_t un_len;
  403. int dirfd;
  404. if (unlink(bind_path) && errno != ENOENT) {
  405. VLOG_WARN("unlinking \"%s\": %s\n",
  406. bind_path, ovs_strerror(errno));
  407. }
  408. fatal_signal_add_file_to_unlink(bind_path);
  409. error = make_sockaddr_un(bind_path, &un, &un_len, &dirfd);
  410. if (!error) {
  411. error = bind_unix_socket(fd, (struct sockaddr *) &un, un_len);
  412. }
  413. if (dirfd >= 0) {
  414. close(dirfd);
  415. }
  416. if (error) {
  417. goto error;
  418. }
  419. }
  420. if (connect_path) {
  421. struct sockaddr_un un;
  422. socklen_t un_len;
  423. int dirfd;
  424. error = make_sockaddr_un(connect_path, &un, &un_len, &dirfd);
  425. if (!error
  426. && connect(fd, (struct sockaddr*) &un, un_len)
  427. && errno != EINPROGRESS) {
  428. error = errno;
  429. }
  430. if (dirfd >= 0) {
  431. close(dirfd);
  432. }
  433. if (error) {
  434. goto error;
  435. }
  436. }
  437. return fd;
  438. error:
  439. if (error == EAGAIN) {
  440. error = EPROTO;
  441. }
  442. if (bind_path) {
  443. fatal_signal_unlink_file_now(bind_path);
  444. }
  445. close(fd);
  446. return -error;
  447. }
  448. int
  449. get_unix_name_len(socklen_t sun_len)
  450. {
  451. return (sun_len >= offsetof(struct sockaddr_un, sun_path)
  452. ? sun_len - offsetof(struct sockaddr_un, sun_path)
  453. : 0);
  454. }
  455. ovs_be32
  456. guess_netmask(ovs_be32 ip_)
  457. {
  458. uint32_t ip = ntohl(ip_);
  459. return ((ip >> 31) == 0 ? htonl(0xff000000) /* Class A */
  460. : (ip >> 30) == 2 ? htonl(0xffff0000) /* Class B */
  461. : (ip >> 29) == 6 ? htonl(0xffffff00) /* Class C */
  462. : htonl(0)); /* ??? */
  463. }
  464. /* Parses 'target', which should be a string in the format "<host>[:<port>]".
  465. * <host> is required. If 'default_port' is nonzero then <port> is optional
  466. * and defaults to 'default_port'.
  467. *
  468. * On success, returns true and stores the parsed remote address into '*sinp'.
  469. * On failure, logs an error, stores zeros into '*sinp', and returns false. */
  470. bool
  471. inet_parse_active(const char *target_, uint16_t default_port,
  472. struct sockaddr_in *sinp)
  473. {
  474. char *target = xstrdup(target_);
  475. char *save_ptr = NULL;
  476. const char *host_name;
  477. const char *port_string;
  478. bool ok = false;
  479. /* Defaults. */
  480. sinp->sin_family = AF_INET;
  481. sinp->sin_port = htons(default_port);
  482. /* Tokenize. */
  483. host_name = strtok_r(target, ":", &save_ptr);
  484. port_string = strtok_r(NULL, ":", &save_ptr);
  485. if (!host_name) {
  486. VLOG_ERR("%s: bad peer name format", target_);
  487. goto exit;
  488. }
  489. /* Look up IP, port. */
  490. if (lookup_ip(host_name, &sinp->sin_addr)) {
  491. goto exit;
  492. }
  493. if (port_string && atoi(port_string)) {
  494. sinp->sin_port = htons(atoi(port_string));
  495. } else if (!default_port) {
  496. VLOG_ERR("%s: port number must be specified", target_);
  497. goto exit;
  498. }
  499. ok = true;
  500. exit:
  501. if (!ok) {
  502. memset(sinp, 0, sizeof *sinp);
  503. }
  504. free(target);
  505. return ok;
  506. }
  507. /* Opens a non-blocking IPv4 socket of the specified 'style' and connects to
  508. * 'target', which should be a string in the format "<host>[:<port>]". <host>
  509. * is required. If 'default_port' is nonzero then <port> is optional and
  510. * defaults to 'default_port'.
  511. *
  512. * 'style' should be SOCK_STREAM (for TCP) or SOCK_DGRAM (for UDP).
  513. *
  514. * On success, returns 0 (indicating connection complete) or EAGAIN (indicating
  515. * connection in progress), in which case the new file descriptor is stored
  516. * into '*fdp'. On failure, returns a positive errno value other than EAGAIN
  517. * and stores -1 into '*fdp'.
  518. *
  519. * If 'sinp' is non-null, then on success the target address is stored into
  520. * '*sinp'.
  521. *
  522. * 'dscp' becomes the DSCP bits in the IP headers for the new connection. It
  523. * should be in the range [0, 63] and will automatically be shifted to the
  524. * appropriately place in the IP tos field. */
  525. int
  526. inet_open_active(int style, const char *target, uint16_t default_port,
  527. struct sockaddr_in *sinp, int *fdp, uint8_t dscp)
  528. {
  529. struct sockaddr_in sin;
  530. int fd = -1;
  531. int error;
  532. /* Parse. */
  533. if (!inet_parse_active(target, default_port, &sin)) {
  534. error = EAFNOSUPPORT;
  535. goto exit;
  536. }
  537. /* Create non-blocking socket. */
  538. fd = socket(AF_INET, style, 0);
  539. if (fd < 0) {
  540. VLOG_ERR("%s: socket: %s", target, ovs_strerror(errno));
  541. error = errno;
  542. goto exit;
  543. }
  544. error = set_nonblocking(fd);
  545. if (error) {
  546. goto exit;
  547. }
  548. /* The dscp bits must be configured before connect() to ensure that the TOS
  549. * field is set during the connection establishment. If set after
  550. * connect(), the handshake SYN frames will be sent with a TOS of 0. */
  551. error = set_dscp(fd, dscp);
  552. if (error) {
  553. VLOG_ERR("%s: socket: %s", target, ovs_strerror(error));
  554. goto exit;
  555. }
  556. /* Connect. */
  557. error = connect(fd, (struct sockaddr *) &sin, sizeof sin) == 0 ? 0 : errno;
  558. if (error == EINPROGRESS) {
  559. error = EAGAIN;
  560. }
  561. exit:
  562. if (!error || error == EAGAIN) {
  563. if (sinp) {
  564. *sinp = sin;
  565. }
  566. } else if (fd >= 0) {
  567. close(fd);
  568. fd = -1;
  569. }
  570. *fdp = fd;
  571. return error;
  572. }
  573. /* Parses 'target', which should be a string in the format "[<port>][:<ip>]":
  574. *
  575. * - If 'default_port' is -1, then <port> is required. Otherwise, if
  576. * <port> is omitted, then 'default_port' is used instead.
  577. *
  578. * - If <port> (or 'default_port', if used) is 0, then no port is bound
  579. * and the TCP/IP stack will select a port.
  580. *
  581. * - If <ip> is omitted then the IP address is wildcarded.
  582. *
  583. * If successful, stores the address into '*sinp' and returns true; otherwise
  584. * zeros '*sinp' and returns false. */
  585. bool
  586. inet_parse_passive(const char *target_, int default_port,
  587. struct sockaddr_in *sinp)
  588. {
  589. char *target = xstrdup(target_);
  590. char *string_ptr = target;
  591. const char *host_name;
  592. const char *port_string;
  593. bool ok = false;
  594. int port;
  595. /* Address defaults. */
  596. memset(sinp, 0, sizeof *sinp);
  597. sinp->sin_family = AF_INET;
  598. sinp->sin_addr.s_addr = htonl(INADDR_ANY);
  599. sinp->sin_port = htons(default_port);
  600. /* Parse optional port number. */
  601. port_string = strsep(&string_ptr, ":");
  602. if (port_string && str_to_int(port_string, 10, &port)) {
  603. sinp->sin_port = htons(port);
  604. } else if (default_port < 0) {
  605. VLOG_ERR("%s: port number must be specified", target_);
  606. goto exit;
  607. }
  608. /* Parse optional bind IP. */
  609. host_name = strsep(&string_ptr, ":");
  610. if (host_name && host_name[0] && lookup_ip(host_name, &sinp->sin_addr)) {
  611. goto exit;
  612. }
  613. ok = true;
  614. exit:
  615. if (!ok) {
  616. memset(sinp, 0, sizeof *sinp);
  617. }
  618. free(target);
  619. return ok;
  620. }
  621. /* Opens a non-blocking IPv4 socket of the specified 'style', binds to
  622. * 'target', and listens for incoming connections. Parses 'target' in the same
  623. * way was inet_parse_passive().
  624. *
  625. * 'style' should be SOCK_STREAM (for TCP) or SOCK_DGRAM (for UDP).
  626. *
  627. * For TCP, the socket will have SO_REUSEADDR turned on.
  628. *
  629. * On success, returns a non-negative file descriptor. On failure, returns a
  630. * negative errno value.
  631. *
  632. * If 'sinp' is non-null, then on success the bound address is stored into
  633. * '*sinp'.
  634. *
  635. * 'dscp' becomes the DSCP bits in the IP headers for the new connection. It
  636. * should be in the range [0, 63] and will automatically be shifted to the
  637. * appropriately place in the IP tos field. */
  638. int
  639. inet_open_passive(int style, const char *target, int default_port,
  640. struct sockaddr_in *sinp, uint8_t dscp)
  641. {
  642. bool kernel_chooses_port;
  643. struct sockaddr_in sin;
  644. int fd = 0, error;
  645. unsigned int yes = 1;
  646. if (!inet_parse_passive(target, default_port, &sin)) {
  647. return -EAFNOSUPPORT;
  648. }
  649. /* Create non-blocking socket, set SO_REUSEADDR. */
  650. fd = socket(AF_INET, style, 0);
  651. if (fd < 0) {
  652. error = errno;
  653. VLOG_ERR("%s: socket: %s", target, ovs_strerror(error));
  654. return -error;
  655. }
  656. error = set_nonblocking(fd);
  657. if (error) {
  658. goto error;
  659. }
  660. if (style == SOCK_STREAM
  661. && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
  662. error = errno;
  663. VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s",
  664. target, ovs_strerror(error));
  665. goto error;
  666. }
  667. /* Bind. */
  668. if (bind(fd, (struct sockaddr *) &sin, sizeof sin) < 0) {
  669. error = errno;
  670. VLOG_ERR("%s: bind: %s", target, ovs_strerror(error));
  671. goto error;
  672. }
  673. /* The dscp bits must be configured before connect() to ensure that the TOS
  674. * field is set during the connection establishment. If set after
  675. * connect(), the handshake SYN frames will be sent with a TOS of 0. */
  676. error = set_dscp(fd, dscp);
  677. if (error) {
  678. VLOG_ERR("%s: socket: %s", target, ovs_strerror(error));
  679. goto error;
  680. }
  681. /* Listen. */
  682. if (style == SOCK_STREAM && listen(fd, 10) < 0) {
  683. error = errno;
  684. VLOG_ERR("%s: listen: %s", target, ovs_strerror(error));
  685. goto error;
  686. }
  687. kernel_chooses_port = sin.sin_port == htons(0);
  688. if (sinp || kernel_chooses_port) {
  689. socklen_t sin_len = sizeof sin;
  690. if (getsockname(fd, (struct sockaddr *) &sin, &sin_len) < 0) {
  691. error = errno;
  692. VLOG_ERR("%s: getsockname: %s", target, ovs_strerror(error));
  693. goto error;
  694. }
  695. if (sin.sin_family != AF_INET || sin_len != sizeof sin) {
  696. error = EAFNOSUPPORT;
  697. VLOG_ERR("%s: getsockname: invalid socket name", target);
  698. goto error;
  699. }
  700. if (sinp) {
  701. *sinp = sin;
  702. }
  703. if (kernel_chooses_port) {
  704. VLOG_INFO("%s: listening on port %"PRIu16,
  705. target, ntohs(sin.sin_port));
  706. }
  707. }
  708. return fd;
  709. error:
  710. close(fd);
  711. return -error;
  712. }
  713. /* Returns a readable and writable fd for /dev/null, if successful, otherwise
  714. * a negative errno value. The caller must not close the returned fd (because
  715. * the same fd will be handed out to subsequent callers). */
  716. int
  717. get_null_fd(void)
  718. {
  719. static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
  720. static int null_fd;
  721. if (ovsthread_once_start(&once)) {
  722. null_fd = open("/dev/null", O_RDWR);
  723. if (null_fd < 0) {
  724. int error = errno;
  725. VLOG_ERR("could not open /dev/null: %s", ovs_strerror(error));
  726. null_fd = -error;
  727. }
  728. ovsthread_once_done(&once);
  729. }
  730. return null_fd;
  731. }
  732. int
  733. read_fully(int fd, void *p_, size_t size, size_t *bytes_read)
  734. {
  735. uint8_t *p = p_;
  736. *bytes_read = 0;
  737. while (size > 0) {
  738. ssize_t retval = read(fd, p, size);
  739. if (retval > 0) {
  740. *bytes_read += retval;
  741. size -= retval;
  742. p += retval;
  743. } else if (retval == 0) {
  744. return EOF;
  745. } else if (errno != EINTR) {
  746. return errno;
  747. }
  748. }
  749. return 0;
  750. }
  751. int
  752. write_fully(int fd, const void *p_, size_t size, size_t *bytes_written)
  753. {
  754. const uint8_t *p = p_;
  755. *bytes_written = 0;
  756. while (size > 0) {
  757. ssize_t retval = write(fd, p, size);
  758. if (retval > 0) {
  759. *bytes_written += retval;
  760. size -= retval;
  761. p += retval;
  762. } else if (retval == 0) {
  763. VLOG_WARN("write returned 0");
  764. return EPROTO;
  765. } else if (errno != EINTR) {
  766. return errno;
  767. }
  768. }
  769. return 0;
  770. }
  771. /* Given file name 'file_name', fsyncs the directory in which it is contained.
  772. * Returns 0 if successful, otherwise a positive errno value. */
  773. int
  774. fsync_parent_dir(const char *file_name)
  775. {
  776. int error = 0;
  777. char *dir;
  778. int fd;
  779. dir = dir_name(file_name);
  780. fd = open(dir, O_RDONLY);
  781. if (fd >= 0) {
  782. if (fsync(fd)) {
  783. if (errno == EINVAL || errno == EROFS) {
  784. /* This directory does not support synchronization. Not
  785. * really an error. */
  786. } else {
  787. error = errno;
  788. VLOG_ERR("%s: fsync failed (%s)", dir, ovs_strerror(error));
  789. }
  790. }
  791. close(fd);
  792. } else {
  793. error = errno;
  794. VLOG_ERR("%s: open failed (%s)", dir, ovs_strerror(error));
  795. }
  796. free(dir);
  797. return error;
  798. }
  799. /* Obtains the modification time of the file named 'file_name' to the greatest
  800. * supported precision. If successful, stores the mtime in '*mtime' and
  801. * returns 0. On error, returns a positive errno value and stores zeros in
  802. * '*mtime'. */
  803. int
  804. get_mtime(const char *file_name, struct timespec *mtime)
  805. {
  806. struct stat s;
  807. if (!stat(file_name, &s)) {
  808. mtime->tv_sec = s.st_mtime;
  809. #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  810. mtime->tv_nsec = s.st_mtim.tv_nsec;
  811. #elif HAVE_STRUCT_STAT_ST_MTIMENSEC
  812. mtime->tv_nsec = s.st_mtimensec;
  813. #else
  814. mtime->tv_nsec = 0;
  815. #endif
  816. return 0;
  817. } else {
  818. mtime->tv_sec = mtime->tv_nsec = 0;
  819. return errno;
  820. }
  821. }
  822. void
  823. xpipe(int fds[2])
  824. {
  825. if (pipe(fds)) {
  826. VLOG_FATAL("failed to create pipe (%s)", ovs_strerror(errno));
  827. }
  828. }
  829. void
  830. xpipe_nonblocking(int fds[2])
  831. {
  832. xpipe(fds);
  833. xset_nonblocking(fds[0]);
  834. xset_nonblocking(fds[1]);
  835. }
  836. void
  837. xsocketpair(int domain, int type, int protocol, int fds[2])
  838. {
  839. if (socketpair(domain, type, protocol, fds)) {
  840. VLOG_FATAL("failed to create socketpair (%s)", ovs_strerror(errno));
  841. }
  842. }
  843. static int
  844. getsockopt_int(int fd, int level, int option, const char *optname, int *valuep)
  845. {
  846. static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 10);
  847. socklen_t len;
  848. int value;
  849. int error;
  850. len = sizeof value;
  851. if (getsockopt(fd, level, option, &value, &len)) {
  852. error = errno;
  853. VLOG_ERR_RL(&rl, "getsockopt(%s): %s", optname, ovs_strerror(error));
  854. } else if (len != sizeof value) {
  855. error = EINVAL;
  856. VLOG_ERR_RL(&rl, "getsockopt(%s): value is %u bytes (expected %zu)",
  857. optname, (unsigned int) len, sizeof value);
  858. } else {
  859. error = 0;
  860. }
  861. *valuep = error ? 0 : value;
  862. return error;
  863. }
  864. static void
  865. describe_sockaddr(struct ds *string, int fd,
  866. int (*getaddr)(int, struct sockaddr *, socklen_t *))
  867. {
  868. struct sockaddr_storage ss;
  869. socklen_t len = sizeof ss;
  870. if (!getaddr(fd, (struct sockaddr *) &ss, &len)) {
  871. if (ss.ss_family == AF_INET) {
  872. struct sockaddr_in sin;
  873. memcpy(&sin, &ss, sizeof sin);
  874. ds_put_format(string, IP_FMT":%"PRIu16,
  875. IP_ARGS(sin.sin_addr.s_addr), ntohs(sin.sin_port));
  876. } else if (ss.ss_family == AF_UNIX) {
  877. struct sockaddr_un sun;
  878. const char *null;
  879. size_t maxlen;
  880. memcpy(&sun, &ss, sizeof sun);
  881. maxlen = len - offsetof(struct sockaddr_un, sun_path);
  882. null = memchr(sun.sun_path, '\0', maxlen);
  883. ds_put_buffer(string, sun.sun_path,
  884. null ? null - sun.sun_path : maxlen);
  885. }
  886. #ifdef HAVE_NETLINK
  887. else if (ss.ss_family == AF_NETLINK) {
  888. int protocol;
  889. /* SO_PROTOCOL was introduced in 2.6.32. Support it regardless of the version
  890. * of the Linux kernel headers in use at build time. */
  891. #ifndef SO_PROTOCOL
  892. #define SO_PROTOCOL 38
  893. #endif
  894. if (!getsockopt_int(fd, SOL_SOCKET, SO_PROTOCOL, "SO_PROTOCOL",
  895. &protocol)) {
  896. switch (protocol) {
  897. case NETLINK_ROUTE:
  898. ds_put_cstr(string, "NETLINK_ROUTE");
  899. break;
  900. case NETLINK_GENERIC:
  901. ds_put_cstr(string, "NETLINK_GENERIC");
  902. break;
  903. default:
  904. ds_put_format(string, "AF_NETLINK family %d", protocol);
  905. break;
  906. }
  907. } else {
  908. ds_put_cstr(string, "AF_NETLINK");
  909. }
  910. }
  911. #endif
  912. #if AF_PACKET && LINUX_DATAPATH
  913. else if (ss.ss_family == AF_PACKET) {
  914. struct sockaddr_ll sll;
  915. memcpy(&sll, &ss, sizeof sll);
  916. ds_put_cstr(string, "AF_PACKET");
  917. if (sll.sll_ifindex) {
  918. char name[IFNAMSIZ];
  919. if (if_indextoname(sll.sll_ifindex, name)) {
  920. ds_put_format(string, "(%s)", name);
  921. } else {
  922. ds_put_format(string, "(ifindex=%d)", sll.sll_ifindex);
  923. }
  924. }
  925. if (sll.sll_protocol) {
  926. ds_put_format(string, "(protocol=0x%"PRIu16")",
  927. ntohs(sll.sll_protocol));
  928. }
  929. }
  930. #endif
  931. else if (ss.ss_family == AF_UNSPEC) {
  932. ds_put_cstr(string, "AF_UNSPEC");
  933. } else {
  934. ds_put_format(string, "AF_%d", (int) ss.ss_family);
  935. }
  936. }
  937. }
  938. #ifdef LINUX_DATAPATH
  939. static void
  940. put_fd_filename(struct ds *string, int fd)
  941. {
  942. char buf[1024];
  943. char *linkname;
  944. int n;
  945. linkname = xasprintf("/proc/self/fd/%d", fd);
  946. n = readlink(linkname, buf, sizeof buf);
  947. if (n > 0) {
  948. ds_put_char(string, ' ');
  949. ds_put_buffer(string, buf, n);
  950. if (n > sizeof buf) {
  951. ds_put_cstr(string, "...");
  952. }
  953. }
  954. free(linkname);
  955. }
  956. #endif
  957. /* Returns a malloc()'d string describing 'fd', for use in logging. */
  958. char *
  959. describe_fd(int fd)
  960. {
  961. struct ds string;
  962. struct stat s;
  963. ds_init(&string);
  964. if (fstat(fd, &s)) {
  965. ds_put_format(&string, "fstat failed (%s)", ovs_strerror(errno));
  966. } else if (S_ISSOCK(s.st_mode)) {
  967. describe_sockaddr(&string, fd, getsockname);
  968. ds_put_cstr(&string, "<->");
  969. describe_sockaddr(&string, fd, getpeername);
  970. } else {
  971. ds_put_cstr(&string, (isatty(fd) ? "tty"
  972. : S_ISDIR(s.st_mode) ? "directory"
  973. : S_ISCHR(s.st_mode) ? "character device"
  974. : S_ISBLK(s.st_mode) ? "block device"
  975. : S_ISREG(s.st_mode) ? "file"
  976. : S_ISFIFO(s.st_mode) ? "FIFO"
  977. : S_ISLNK(s.st_mode) ? "symbolic link"
  978. : "unknown"));
  979. #ifdef LINUX_DATAPATH
  980. put_fd_filename(&string, fd);
  981. #endif
  982. }
  983. return ds_steal_cstr(&string);
  984. }
  985. /* Returns the total of the 'iov_len' members of the 'n_iovs' in 'iovs'.
  986. * The caller must ensure that the total does not exceed SIZE_MAX. */
  987. size_t
  988. iovec_len(const struct iovec iovs[], size_t n_iovs)
  989. {
  990. size_t len = 0;
  991. size_t i;
  992. for (i = 0; i < n_iovs; i++) {
  993. len += iovs[i].iov_len;
  994. }
  995. return len;
  996. }
  997. /* Returns true if all of the 'n_iovs' iovecs in 'iovs' have length zero. */
  998. bool
  999. iovec_is_empty(const struct iovec iovs[], size_t n_iovs)
  1000. {
  1001. size_t i;
  1002. for (i = 0; i < n_iovs; i++) {
  1003. if (iovs[i].iov_len) {
  1004. return false;
  1005. }
  1006. }
  1007. return true;
  1008. }
  1009. /* Sends the 'n_iovs' iovecs of data in 'iovs' and the 'n_fds' file descriptors
  1010. * in 'fds' on Unix domain socket 'sock'. Returns the number of bytes
  1011. * successfully sent or -1 if an error occurred. On error, sets errno
  1012. * appropriately. */
  1013. int
  1014. send_iovec_and_fds(int sock,
  1015. const struct iovec *iovs, size_t n_iovs,
  1016. const int fds[], size_t n_fds)
  1017. {
  1018. ovs_assert(sock >= 0);
  1019. if (n_fds > 0) {
  1020. union {
  1021. struct cmsghdr cm;
  1022. char control[CMSG_SPACE(SOUTIL_MAX_FDS * sizeof *fds)];
  1023. } cmsg;
  1024. struct msghdr msg;
  1025. ovs_assert(!iovec_is_empty(iovs, n_iovs));
  1026. ovs_assert(n_fds <= SOUTIL_MAX_FDS);
  1027. memset(&cmsg, 0, sizeof cmsg);
  1028. cmsg.cm.cmsg_len = CMSG_LEN(n_fds * sizeof *fds);
  1029. cmsg.cm.cmsg_level = SOL_SOCKET;
  1030. cmsg.cm.cmsg_type = SCM_RIGHTS;
  1031. memcpy(CMSG_DATA(&cmsg.cm), fds, n_fds * sizeof *fds);
  1032. msg.msg_name = NULL;
  1033. msg.msg_namelen = 0;
  1034. msg.msg_iov = CONST_CAST(struct iovec *, iovs);
  1035. msg.msg_iovlen = n_iovs;
  1036. msg.msg_control = &cmsg.cm;
  1037. msg.msg_controllen = CMSG_SPACE(n_fds * sizeof *fds);
  1038. msg.msg_flags = 0;
  1039. return sendmsg(sock, &msg, 0);
  1040. } else {
  1041. return writev(sock, iovs, n_iovs);
  1042. }
  1043. }
  1044. /* Sends the 'n_iovs' iovecs of data in 'iovs' and the 'n_fds' file descriptors
  1045. * in 'fds' on Unix domain socket 'sock'. If 'skip_bytes' is nonzero, then the
  1046. * first 'skip_bytes' of data in the iovecs are not sent, and none of the file
  1047. * descriptors are sent. The function continues to retry sending until an
  1048. * error (other than EINTR) occurs or all the data and fds are sent.
  1049. *
  1050. * Returns 0 if all the data and fds were successfully sent, otherwise a
  1051. * positive errno value. Regardless of success, stores the number of bytes
  1052. * sent (always at least 'skip_bytes') in '*bytes_sent'. (If at least one byte
  1053. * is sent, then all the fds have been sent.)
  1054. *
  1055. * 'skip_bytes' must be less than or equal to iovec_len(iovs, n_iovs). */
  1056. int
  1057. send_iovec_and_fds_fully(int sock,
  1058. const struct iovec iovs[], size_t n_iovs,
  1059. const int fds[], size_t n_fds,
  1060. size_t skip_bytes, size_t *bytes_sent)
  1061. {
  1062. *bytes_sent = 0;
  1063. while (n_iovs > 0) {
  1064. int retval;
  1065. if (skip_bytes) {
  1066. retval = skip_bytes;
  1067. skip_bytes = 0;
  1068. } else if (!*bytes_sent) {
  1069. retval = send_iovec_and_fds(sock, iovs, n_iovs, fds, n_fds);
  1070. } else {
  1071. retval = writev(sock, iovs, n_iovs);
  1072. }
  1073. if (retval > 0) {
  1074. *bytes_sent += retval;
  1075. while (retval > 0) {
  1076. const uint8_t *base = iovs->iov_base;
  1077. size_t len = iovs->iov_len;
  1078. if (retval < len) {
  1079. size_t sent;
  1080. int error;
  1081. error = write_fully(sock, base + retval, len - retval,
  1082. &sent);
  1083. *bytes_sent += sent;
  1084. retval += sent;
  1085. if (error) {
  1086. return error;
  1087. }
  1088. }
  1089. retval -= len;
  1090. iovs++;
  1091. n_iovs--;
  1092. }
  1093. } else if (retval == 0) {
  1094. if (iovec_is_empty(iovs, n_iovs)) {
  1095. break;
  1096. }
  1097. VLOG_WARN("send returned 0");
  1098. return EPROTO;
  1099. } else if (errno != EINTR) {
  1100. return errno;
  1101. }
  1102. }
  1103. return 0;
  1104. }
  1105. /* Sends the 'n_iovs' iovecs of data in 'iovs' and the 'n_fds' file descriptors
  1106. * in 'fds' on Unix domain socket 'sock'. The function continues to retry
  1107. * sending until an error (other than EAGAIN or EINTR) occurs or all the data
  1108. * and fds are sent. Upon EAGAIN, the function blocks until the socket is
  1109. * ready for more data.
  1110. *
  1111. * Returns 0 if all the data and fds were successfully sent, otherwise a
  1112. * positive errno value. */
  1113. int
  1114. send_iovec_and_fds_fully_block(int sock,
  1115. const struct iovec iovs[], size_t n_iovs,
  1116. const int fds[], size_t n_fds)
  1117. {
  1118. size_t sent = 0;
  1119. for (;;) {
  1120. int error;
  1121. error = send_iovec_and_fds_fully(sock, iovs, n_iovs,
  1122. fds, n_fds, sent, &sent);
  1123. if (error != EAGAIN) {
  1124. return error;
  1125. }
  1126. poll_fd_wait(sock, POLLOUT);
  1127. poll_block();
  1128. }
  1129. }
  1130. /* Attempts to receive from Unix domain socket 'sock' up to 'size' bytes of
  1131. * data into 'data' and up to SOUTIL_MAX_FDS file descriptors into 'fds'.
  1132. *
  1133. * - Upon success, returns the number of bytes of data copied into 'data'
  1134. * and stores the number of received file descriptors into '*n_fdsp'.
  1135. *
  1136. * - On failure, returns a negative errno value and stores 0 in
  1137. * '*n_fdsp'.
  1138. *
  1139. * - On EOF, returns 0 and stores 0 in '*n_fdsp'. */
  1140. int
  1141. recv_data_and_fds(int sock,
  1142. void *data, size_t size,
  1143. int fds[SOUTIL_MAX_FDS], size_t *n_fdsp)
  1144. {
  1145. union {
  1146. struct cmsghdr cm;
  1147. char control[CMSG_SPACE(SOUTIL_MAX_FDS * sizeof *fds)];
  1148. } cmsg;
  1149. struct msghdr msg;
  1150. int retval;
  1151. struct cmsghdr *p;
  1152. size_t i;
  1153. *n_fdsp = 0;
  1154. do {
  1155. struct iovec iov;
  1156. iov.iov_base = data;
  1157. iov.iov_len = size;
  1158. msg.msg_name = NULL;
  1159. msg.msg_namelen = 0;
  1160. msg.msg_iov = &iov;
  1161. msg.msg_iovlen = 1;
  1162. msg.msg_control = &cmsg.cm;
  1163. msg.msg_controllen = sizeof cmsg.control;
  1164. msg.msg_flags = 0;
  1165. retval = recvmsg(sock, &msg, 0);
  1166. } while (retval < 0 && errno == EINTR);
  1167. if (retval <= 0) {
  1168. return retval < 0 ? -errno : 0;
  1169. }
  1170. for (p = CMSG_FIRSTHDR(&msg); p; p = CMSG_NXTHDR(&msg, p)) {
  1171. if (p->cmsg_level != SOL_SOCKET || p->cmsg_type != SCM_RIGHTS) {
  1172. VLOG_ERR("unexpected control message %d:%d",
  1173. p->cmsg_level, p->cmsg_type);
  1174. goto error;
  1175. } else if (*n_fdsp) {
  1176. VLOG_ERR("multiple SCM_RIGHTS received");
  1177. goto error;
  1178. } else {
  1179. size_t n_fds = (p->cmsg_len - CMSG_LEN(0)) / sizeof *fds;
  1180. const int *fds_data = ALIGNED_CAST(const int *, CMSG_DATA(p));
  1181. ovs_assert(n_fds > 0);
  1182. if (n_fds > SOUTIL_MAX_FDS) {
  1183. VLOG_ERR("%zu fds received but only %d supported",
  1184. n_fds, SOUTIL_MAX_FDS);
  1185. for (i = 0; i < n_fds; i++) {
  1186. close(fds_data[i]);
  1187. }
  1188. goto error;
  1189. }
  1190. *n_fdsp = n_fds;
  1191. memcpy(fds, fds_data, n_fds * sizeof *fds);
  1192. }
  1193. }
  1194. return retval;
  1195. error:
  1196. for (i = 0; i < *n_fdsp; i++) {
  1197. close(fds[i]);
  1198. }
  1199. *n_fdsp = 0;
  1200. return EPROTO;
  1201. }
  1202. /* Calls ioctl() on an AF_INET sock, passing the specified 'command' and
  1203. * 'arg'. Returns 0 if successful, otherwise a positive errno value. */
  1204. int
  1205. af_inet_ioctl(unsigned long int command, const void *arg)
  1206. {
  1207. static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
  1208. static int sock;
  1209. if (ovsthread_once_start(&once)) {
  1210. sock = socket(AF_INET, SOCK_DGRAM, 0);
  1211. if (sock < 0) {
  1212. sock = -errno;
  1213. VLOG_ERR("failed to create inet socket: %s", ovs_strerror(errno));
  1214. }
  1215. ovsthread_once_done(&once);
  1216. }
  1217. return (sock < 0 ? -sock
  1218. : ioctl(sock, command, arg) == -1 ? errno
  1219. : 0);
  1220. }
  1221. int
  1222. af_inet_ifreq_ioctl(const char *name, struct ifreq *ifr, unsigned long int cmd,
  1223. const char *cmd_name)
  1224. {
  1225. int error;
  1226. ovs_strzcpy(ifr->ifr_name, name, sizeof ifr->ifr_name);
  1227. error = af_inet_ioctl(cmd, ifr);
  1228. if (error) {
  1229. static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
  1230. VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,
  1231. ovs_strerror(error));
  1232. }
  1233. return error;
  1234. }