+struct nl_pool {
+ struct nl_sock *socks[16];
+ int n;
+};
+
+static struct ovs_mutex pool_mutex = OVS_MUTEX_INITIALIZER;
+static struct nl_pool pools[MAX_LINKS] OVS_GUARDED_BY(pool_mutex);
+
+static int
+nl_pool_alloc(int protocol, struct nl_sock **sockp)
+{
+ struct nl_sock *sock = NULL;
+ struct nl_pool *pool;
+
+ ovs_assert(protocol >= 0 && protocol < ARRAY_SIZE(pools));
+
+ ovs_mutex_lock(&pool_mutex);
+ pool = &pools[protocol];
+ if (pool->n > 0) {
+ sock = pool->socks[--pool->n];
+ }
+ ovs_mutex_unlock(&pool_mutex);
+
+ if (sock) {
+ *sockp = sock;
+ return 0;
+ } else {
+ return nl_sock_create(protocol, sockp);
+ }
+}
+
+static void
+nl_pool_release(struct nl_sock *sock)
+{
+ if (sock) {
+ struct nl_pool *pool = &pools[sock->protocol];
+
+ ovs_mutex_lock(&pool_mutex);
+ if (pool->n < ARRAY_SIZE(pool->socks)) {
+ pool->socks[pool->n++] = sock;
+ sock = NULL;
+ }
+ ovs_mutex_unlock(&pool_mutex);
+
+ nl_sock_destroy(sock);
+ }
+}
+
+/* Sends 'request' to the kernel on a Netlink socket for the given 'protocol'
+ * (e.g. NETLINK_ROUTE or NETLINK_GENERIC) and waits for a response. If
+ * successful, returns 0. On failure, returns a positive errno value.
+ *
+ * If 'replyp' is nonnull, then on success '*replyp' is set to the kernel's
+ * reply, which the caller is responsible for freeing with ofpbuf_delete(), and
+ * on failure '*replyp' is set to NULL. If 'replyp' is null, then the kernel's
+ * reply, if any, is discarded.
+ *
+ * Before the message is sent, nlmsg_len in 'request' will be finalized to
+ * match ofpbuf_size(msg), nlmsg_pid will be set to the pid of the socket used
+ * for sending the request, and nlmsg_seq will be initialized.
+ *
+ * The caller is responsible for destroying 'request'.
+ *
+ * Bare Netlink is an unreliable transport protocol. This function layers
+ * reliable delivery and reply semantics on top of bare Netlink.
+ *
+ * In Netlink, sending a request to the kernel is reliable enough, because the
+ * kernel will tell us if the message cannot be queued (and we will in that
+ * case put it on the transmit queue and wait until it can be delivered).
+ *
+ * Receiving the reply is the real problem: if the socket buffer is full when
+ * the kernel tries to send the reply, the reply will be dropped. However, the
+ * kernel sets a flag that a reply has been dropped. The next call to recv
+ * then returns ENOBUFS. We can then re-send the request.
+ *
+ * Caveats:
+ *
+ * 1. Netlink depends on sequence numbers to match up requests and
+ * replies. The sender of a request supplies a sequence number, and
+ * the reply echos back that sequence number.
+ *
+ * This is fine, but (1) some kernel netlink implementations are
+ * broken, in that they fail to echo sequence numbers and (2) this
+ * function will drop packets with non-matching sequence numbers, so
+ * that only a single request can be usefully transacted at a time.
+ *
+ * 2. Resending the request causes it to be re-executed, so the request
+ * needs to be idempotent.
+ */
+int
+nl_transact(int protocol, const struct ofpbuf *request,
+ struct ofpbuf **replyp)
+{
+ struct nl_sock *sock;
+ int error;
+
+ error = nl_pool_alloc(protocol, &sock);
+ if (error) {
+ *replyp = NULL;
+ return error;
+ }
+
+ error = nl_sock_transact(sock, request, replyp);
+
+ nl_pool_release(sock);
+ return error;
+}
+
+/* Sends the 'request' member of the 'n' transactions in 'transactions' on a
+ * Netlink socket for the given 'protocol' (e.g. NETLINK_ROUTE or
+ * NETLINK_GENERIC), in order, and receives responses to all of them. Fills in
+ * the 'error' member of each transaction with 0 if it was successful,
+ * otherwise with a positive errno value. If 'reply' is nonnull, then it will
+ * be filled with the reply if the message receives a detailed reply. In other
+ * cases, i.e. where the request failed or had no reply beyond an indication of
+ * success, 'reply' will be cleared if it is nonnull.
+ *
+ * The caller is responsible for destroying each request and reply, and the
+ * transactions array itself.
+ *
+ * Before sending each message, this function will finalize nlmsg_len in each
+ * 'request' to match the ofpbuf's size, set nlmsg_pid to the pid of the socket
+ * used for the transaction, and initialize nlmsg_seq.
+ *
+ * Bare Netlink is an unreliable transport protocol. This function layers
+ * reliable delivery and reply semantics on top of bare Netlink. See
+ * nl_transact() for some caveats.
+ */
+void
+nl_transact_multiple(int protocol,
+ struct nl_transaction **transactions, size_t n)
+{
+ struct nl_sock *sock;
+ int error;
+
+ error = nl_pool_alloc(protocol, &sock);
+ if (!error) {
+ nl_sock_transact_multiple(sock, transactions, n);
+ nl_pool_release(sock);
+ } else {
+ nl_sock_record_errors__(transactions, n, error);
+ }
+}
+
+\f
+static uint32_t
+nl_sock_allocate_seq(struct nl_sock *sock, unsigned int n)
+{
+ uint32_t seq = sock->next_seq;
+
+ sock->next_seq += n;
+
+ /* Make it impossible for the next request for sequence numbers to wrap
+ * around to 0. Start over with 1 to avoid ever using a sequence number of
+ * 0, because the kernel uses sequence number 0 for notifications. */
+ if (sock->next_seq >= UINT32_MAX / 2) {
+ sock->next_seq = 1;
+ }
+
+ return seq;
+}
+