+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);
+ }
+}
+
+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;
+}
+
+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;
+}
+