Commit 3879284d authored by Peter Maydell's avatar Peter Maydell
Browse files

Merge remote-tracking branch 'remotes/berrange/tags/pull-qio-2017-01-23-2' into staging



Merge io/ 2017-01-23

# gpg: Signature made Mon 23 Jan 2017 15:56:14 GMT
# gpg:                using RSA key 0xBE86EBB415104FDF
# gpg: Good signature from "Daniel P. Berrange <dan@berrange.com>"
# gpg:                 aka "Daniel P. Berrange <berrange@redhat.com>"
# Primary key fingerprint: DAF3 A6FD B26B 6291 2D0E  8E3F BE86 EBB4 1510 4FDF

* remotes/berrange/tags/pull-qio-2017-01-23-2:
  io: introduce a DNS resolver API
  io: remove Error parameter from QIOTask thread worker
  io: change the QIOTask callback signature
  io: add ability to associate an error with a task
  io: add ability to associate an opaque "result" with with a task
  io: fix typo in docs for QIOTask
  io: stop incrementing reference in qio_task_get_source
  sockets: add ability to disable DNS resolution for InetSocketAddress

Signed-off-by: default avatarPeter Maydell <peter.maydell@linaro.org>
parents 598cf1c8 c1b412f1
Loading
Loading
Loading
Loading
+228 −0
Original line number Diff line number Diff line
/*
 * QEMU DNS resolver
 *
 * Copyright (c) 2016-2017 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef QIO_DNS_RESOLVER_H
#define QIO_DNS_RESOLVER_H

#include "qemu-common.h"
#include "qom/object.h"
#include "io/task.h"

#define TYPE_QIO_DNS_RESOLVER "qio-dns-resolver"
#define QIO_DNS_RESOLVER(obj)                                    \
    OBJECT_CHECK(QIODNSResolver, (obj), TYPE_QIO_DNS_RESOLVER)
#define QIO_DNS_RESOLVER_CLASS(klass)                                    \
    OBJECT_CLASS_CHECK(QIODNSResolverClass, klass, TYPE_QIO_DNS_RESOLVER)
#define QIO_DNS_RESOLVER_GET_CLASS(obj)                                  \
    OBJECT_GET_CLASS(QIODNSResolverClass, obj, TYPE_QIO_DNS_RESOLVER)

typedef struct QIODNSResolver QIODNSResolver;
typedef struct QIODNSResolverClass QIODNSResolverClass;

/**
 * QIODNSResolver:
 *
 * The QIODNSResolver class provides a framework for doing
 * DNS resolution on SocketAddress objects, independently
 * of socket creation.
 *
 * <example>
 *   <title>Resolving addresses synchronously</title>
 *   <programlisting>
 *    int mylisten(SocketAddress *addr, Error **errp) {
 *      QIODNSResolver *resolver = qio_dns_resolver_get_instance();
 *      SocketAddress **rawaddrs = NULL;
 *      size_t nrawaddrs = 0;
 *      Error *err = NULL;
 *      QIOChannel **socks = NULL;
 *      size_t nsocks = 0;
 *
 *      if (qio_dns_resolver_lookup_sync(dns, addr, &nrawaddrs,
 *                                       &rawaddrs, errp) < 0) {
 *          return -1;
 *      }
 *
 *      for (i = 0; i < nrawaddrs; i++) {
 *         QIOChannel *sock = qio_channel_new();
 *         Error *local_err = NULL;
 *         qio_channel_listen_sync(sock, rawaddrs[i], &local_err);
 *         if (local_err) {
 *            error_propagate(&err, local_err);
 *         } else {
 *            socks = g_renew(QIOChannelSocket *, socks, nsocks + 1);
 *            socks[nsocks++] = sock;
 *         }
 *         qapi_free_SocketAddress(rawaddrs[i]);
 *      }
 *      g_free(rawaddrs);
 *
 *      if (nsocks == 0) {
 *         error_propagate(errp, err);
 *      } else {
 *         error_free(err);
 *      }
 *   }
 *   </programlisting>
 * </example>
 *
 * <example>
 *   <title>Resolving addresses asynchronously</title>
 *   <programlisting>
 *    typedef struct MyListenData {
 *       Error *err;
 *       QIOChannelSocket **socks;
 *       size_t nsocks;
 *    } MyListenData;
 *
 *    void mylistenresult(QIOTask *task, void *opaque) {
 *      MyListenData *data = opaque;
 *      QIODNSResolver *resolver =
 *         QIO_DNS_RESOLVER(qio_task_get_source(task);
 *      SocketAddress **rawaddrs = NULL;
 *      size_t nrawaddrs = 0;
 *      Error *err = NULL;
 *
 *      if (qio_task_propagate_error(task, &data->err)) {
 *         return;
 *      }
 *
 *      qio_dns_resolver_lookup_result(resolver, task,
 *                                     &nrawaddrs, &rawaddrs);
 *
 *      for (i = 0; i < nrawaddrs; i++) {
 *         QIOChannel *sock = qio_channel_new();
 *         Error *local_err = NULL;
 *         qio_channel_listen_sync(sock, rawaddrs[i], &local_err);
 *         if (local_err) {
 *            error_propagate(&err, local_err);
 *         } else {
 *            socks = g_renew(QIOChannelSocket *, socks, nsocks + 1);
 *            socks[nsocks++] = sock;
 *         }
 *         qapi_free_SocketAddress(rawaddrs[i]);
 *      }
 *      g_free(rawaddrs);
 *
 *      if (nsocks == 0) {
 *         error_propagate(&data->err, err);
 *      } else {
 *         error_free(err);
 *      }
 *    }
 *
 *    void mylisten(SocketAddress *addr, MyListenData *data) {
 *      QIODNSResolver *resolver = qio_dns_resolver_get_instance();
 *      qio_dns_resolver_lookup_async(dns, addr,
 *                                    mylistenresult, data, NULL);
 *    }
 *   </programlisting>
 * </example>
 */
struct QIODNSResolver {
    Object parent;
};

struct QIODNSResolverClass {
    ObjectClass parent;
};


/**
 * qio_dns_resolver_get_instance:
 *
 * Get the singleton dns resolver instance. The caller
 * does not own a reference on the returned object.
 *
 * Returns: the single dns resolver instance
 */
QIODNSResolver *qio_dns_resolver_get_instance(void);

/**
 * qio_dns_resolver_lookup_sync:
 * @resolver: the DNS resolver instance
 * @addr: the address to resolve
 * @naddr: pointer to hold number of resolved addresses
 * @addrs: pointer to hold resolved addresses
 * @errp: pointer to NULL initialized error object
 *
 * This will attempt to resolve the address provided
 * in @addr. If resolution succeeds, @addrs will be filled
 * with all the resolved addresses. @naddrs will specify
 * the number of entries allocated in @addrs. The caller
 * is responsible for freeing each entry in @addrs, as
 * well as @addrs itself. @naddrs is guaranteed to be
 * greater than zero on success.
 *
 * DNS resolution will be done synchronously so execution
 * of the caller may be blocked for an arbitrary length
 * of time.
 *
 * Returns: 0 if resolution was successful, -1 on error
 */
int qio_dns_resolver_lookup_sync(QIODNSResolver *resolver,
                                 SocketAddress *addr,
                                 size_t *naddrs,
                                 SocketAddress ***addrs,
                                 Error **errp);

/**
 * qio_dns_resolver_lookup_async:
 * @resolver: the DNS resolver instance
 * @addr: the address to resolve
 * @func: the callback to invoke on lookup completion
 * @opaque: data blob to pass to @func
 * @notify: the callback to free @opaque, or NULL
 *
 * This will attempt to resolve the address provided
 * in @addr. The callback @func will be invoked when
 * resolution has either completed or failed. On
 * success, the @func should call the method
 * qio_dns_resolver_lookup_result() to obtain the
 * results.
 *
 * DNS resolution will be done asynchronously so execution
 * of the caller will not be blocked.
 */
void qio_dns_resolver_lookup_async(QIODNSResolver *resolver,
                                   SocketAddress *addr,
                                   QIOTaskFunc func,
                                   gpointer opaque,
                                   GDestroyNotify notify);

/**
 * qio_dns_resolver_lookup_result:
 * @resolver: the DNS resolver instance
 * @task: the task object to get results for
 * @naddr: pointer to hold number of resolved addresses
 * @addrs: pointer to hold resolved addresses
 *
 * This method should be called from the callback passed
 * to qio_dns_resolver_lookup_async() in order to obtain
 * results.  @addrs will be filled with all the resolved
 * addresses. @naddrs will specify the number of entries
 * allocated in @addrs. The caller is responsible for
 * freeing each entry in @addrs, as well as @addrs itself.
 */
void qio_dns_resolver_lookup_result(QIODNSResolver *resolver,
                                    QIOTask *task,
                                    size_t *naddrs,
                                    SocketAddress ***addrs);

#endif /* QIO_DNS_RESOLVER_H */
+111 −43
Original line number Diff line number Diff line
@@ -26,12 +26,10 @@

typedef struct QIOTask QIOTask;

typedef void (*QIOTaskFunc)(Object *source,
                            Error *err,
typedef void (*QIOTaskFunc)(QIOTask *task,
                            gpointer opaque);

typedef int (*QIOTaskWorker)(QIOTask *task,
                             Error **errp,
typedef void (*QIOTaskWorker)(QIOTask *task,
                              gpointer opaque);

/**
@@ -44,12 +42,12 @@ typedef int (*QIOTaskWorker)(QIOTask *task,
 * a public API which accepts a task callback:
 *
 * <example>
 *   <title>Task callback function signature</title>
 *   <title>Task function signature</title>
 *   <programlisting>
 *  void myobject_operation(QMyObject *obj,
 *                          QIOTaskFunc *func,
 *                          gpointer opaque,
 *                          GDestroyNotify *notify);
 *                          GDestroyNotify notify);
 *   </programlisting>
 * </example>
 *
@@ -57,17 +55,41 @@ typedef int (*QIOTaskWorker)(QIOTask *task,
 * is data to pass to it. The optional 'notify' function is used
 * to free 'opaque' when no longer needed.
 *
 * Now, lets say the implementation of this method wants to set
 * a timer to run once a second checking for completion of some
 * activity. It would do something like
 * When the operation completes, the 'func' callback will be
 * invoked, allowing the calling code to determine the result
 * of the operation. An example QIOTaskFunc implementation may
 * look like
 *
 * <example>
 *   <title>Task callback function implementation</title>
 *   <title>Task callback implementation</title>
 *   <programlisting>
 *  static void myobject_operation_notify(QIOTask *task,
 *                                        gpointer opaque)
 *  {
 *      Error *err = NULL;
 *      if (qio_task_propagate_error(task, &err)) {
 *          ...deal with the failure...
 *          error_free(err);
 *      } else {
 *          QMyObject *src = QMY_OBJECT(qio_task_get_source(task));
 *          ...deal with the completion...
 *      }
 *  }
 *   </programlisting>
 * </example>
 *
 * Now, lets say the implementation of the method using the
 * task wants to set a timer to run once a second checking
 * for completion of some activity. It would do something
 * like
 *
 * <example>
 *   <title>Task function implementation</title>
 *   <programlisting>
 *    void myobject_operation(QMyObject *obj,
 *                            QIOTaskFunc *func,
 *                            gpointer opaque,
 *                            GDestroyNotify *notify)
 *                            GDestroyNotify notify)
 *    {
 *      QIOTask *task;
 *
@@ -102,8 +124,8 @@ typedef int (*QIOTaskWorker)(QIOTask *task,
 *
 *      ...check something important...
 *       if (err) {
 *           qio_task_abort(task, err);
 *           error_free(task);
 *           qio_task_set_error(task, err);
 *           qio_task_complete(task);
 *           return FALSE;
 *       } else if (...work is completed ...) {
 *           qio_task_complete(task);
@@ -115,6 +137,10 @@ typedef int (*QIOTaskWorker)(QIOTask *task,
 *   </programlisting>
 * </example>
 *
 * The 'qio_task_complete' call in this method will trigger
 * the callback func 'myobject_operation_notify' shown
 * earlier to deal with the results.
 *
 * Once this function returns false, object_unref will be called
 * automatically on the task causing it to be released and the
 * ref on QMyObject dropped too.
@@ -136,25 +162,23 @@ typedef int (*QIOTaskWorker)(QIOTask *task,
 * socket listen using QIOTask would require:
 *
 * <example>
 *    static int myobject_listen_worker(QIOTask *task,
 *                                      Error **errp,
 *    static void myobject_listen_worker(QIOTask *task,
 *                                       gpointer opaque)
 *    {
 *       QMyObject obj = QMY_OBJECT(qio_task_get_source(task));
 *       SocketAddress *addr = opaque;
 *       Error *err = NULL;
 *
 *       obj->fd = socket_listen(addr, errp);
 *       if (obj->fd < 0) {
 *          return -1;
 *       }
 *       return 0;
 *       obj->fd = socket_listen(addr, &err);
 *
         qio_task_set_error(task, err);
 *    }
 *
 *    void myobject_listen_async(QMyObject *obj,
 *                               SocketAddress *addr,
 *                               QIOTaskFunc *func,
 *                               gpointer opaque,
 *                               GDestroyNotify *notify)
 *                               GDestroyNotify notify)
 *    {
 *      QIOTask *task;
 *      SocketAddress *addrCopy;
@@ -187,8 +211,8 @@ typedef int (*QIOTaskWorker)(QIOTask *task,
 * 'err' attribute in the task object to determine if
 * the operation was successful or not.
 *
 * The returned task will be released when one of
 * qio_task_abort() or qio_task_complete() are invoked.
 * The returned task will be released when qio_task_complete()
 * is invoked.
 *
 * Returns: the task struct
 */
@@ -204,10 +228,8 @@ QIOTask *qio_task_new(Object *source,
 * @opaque: opaque data to pass to @worker
 * @destroy: function to free @opaque
 *
 * Run a task in a background thread. If @worker
 * returns 0 it will call qio_task_complete() in
 * the main event thread context. If @worker
 * returns -1 it will call qio_task_abort() in
 * Run a task in a background thread. When @worker
 * returns it will call qio_task_complete() in
 * the main event thread context.
 */
void qio_task_run_in_thread(QIOTask *task,
@@ -219,34 +241,80 @@ void qio_task_run_in_thread(QIOTask *task,
 * qio_task_complete:
 * @task: the task struct
 *
 * Mark the operation as successfully completed
 * and free the memory for @task.
 * Invoke the completion callback for @task and
 * then free its memory.
 */
void qio_task_complete(QIOTask *task);


/**
 * qio_task_abort:
 * qio_task_set_error:
 * @task: the task struct
 * @err: the error to record for the operation
 *
 * Mark the operation as failed, with @err providing
 * details about the failure. The @err may be freed
 * afer the function returns, as the notification
 * callback is invoked synchronously. The @task will
 * be freed when this call completes.
 * @err: pointer to the error, or NULL
 *
 * Associate an error with the task, which can later
 * be retrieved with the qio_task_propagate_error()
 * method. This method takes ownership of @err, so
 * it is not valid to access it after this call
 * completes. If @err is NULL this is a no-op. If
 * this is call multiple times, only the first
 * provided @err will be recorded, later ones will
 * be discarded and freed.
 */
void qio_task_abort(QIOTask *task,
void qio_task_set_error(QIOTask *task,
                        Error *err);


/**
 * qio_task_propagate_error:
 * @task: the task struct
 * @errp: pointer to a NULL-initialized error object
 *
 * Propagate the error associated with @task
 * into @errp.
 *
 * Returns: true if an error was propagated, false otherwise
 */
bool qio_task_propagate_error(QIOTask *task,
                              Error **errp);


/**
 * qio_task_set_result_pointer:
 * @task: the task struct
 * @result: pointer to the result data
 *
 * Associate an opaque result with the task,
 * which can later be retrieved with the
 * qio_task_get_result_pointer() method
 *
 */
void qio_task_set_result_pointer(QIOTask *task,
                                 gpointer result,
                                 GDestroyNotify notify);


/**
 * qio_task_get_result_pointer:
 * @task: the task struct
 *
 * Retrieve the opaque result data associated
 * with the task, if any.
 *
 * Returns: the task result, or NULL
 */
gpointer qio_task_get_result_pointer(QIOTask *task);


/**
 * qio_task_get_source:
 * @task: the task struct
 *
 * Get the source object associated with the background
 * task. This returns a new reference to the object,
 * which the caller must released with object_unref()
 * when no longer required.
 * task. The caller does not own a reference on the
 * returned Object, and so should call object_ref()
 * if it wants to keep the object pointer outside the
 * lifetime of the QIOTask object.
 *
 * Returns: the source object
 */
+2 −0
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ int socket_set_fast_reuse(int fd);
 */
typedef void NonBlockingConnectHandler(int fd, Error *err, void *opaque);

int inet_ai_family_from_address(InetSocketAddress *addr,
                                Error **errp);
InetSocketAddress *inet_parse(const char *str, Error **errp);
int inet_connect(const char *str, Error **errp);
int inet_connect_saddr(InetSocketAddress *saddr, Error **errp,
+1 −0
Original line number Diff line number Diff line
@@ -7,4 +7,5 @@ io-obj-y += channel-tls.o
io-obj-y += channel-watch.o
io-obj-y += channel-websock.o
io-obj-y += channel-util.o
io-obj-y += dns-resolver.o
io-obj-y += task.o
+16 −28
Original line number Diff line number Diff line
@@ -156,20 +156,16 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
}


static int qio_channel_socket_connect_worker(QIOTask *task,
                                             Error **errp,
static void qio_channel_socket_connect_worker(QIOTask *task,
                                              gpointer opaque)
{
    QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
    SocketAddress *addr = opaque;
    int ret;
    Error *err = NULL;

    ret = qio_channel_socket_connect_sync(ioc,
                                          addr,
                                          errp);
    qio_channel_socket_connect_sync(ioc, addr, &err);

    object_unref(OBJECT(ioc));
    return ret;
    qio_task_set_error(task, err);
}


@@ -219,20 +215,16 @@ int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
}


static int qio_channel_socket_listen_worker(QIOTask *task,
                                            Error **errp,
static void qio_channel_socket_listen_worker(QIOTask *task,
                                             gpointer opaque)
{
    QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
    SocketAddress *addr = opaque;
    int ret;
    Error *err = NULL;

    ret = qio_channel_socket_listen_sync(ioc,
                                         addr,
                                         errp);
    qio_channel_socket_listen_sync(ioc, addr, &err);

    object_unref(OBJECT(ioc));
    return ret;
    qio_task_set_error(task, err);
}


@@ -295,22 +287,18 @@ static void qio_channel_socket_dgram_worker_free(gpointer opaque)
    g_free(data);
}

static int qio_channel_socket_dgram_worker(QIOTask *task,
                                           Error **errp,
static void qio_channel_socket_dgram_worker(QIOTask *task,
                                            gpointer opaque)
{
    QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
    struct QIOChannelSocketDGramWorkerData *data = opaque;
    int ret;
    Error *err = NULL;

    /* socket_dgram() blocks in DNS lookups, so we must use a thread */
    ret = qio_channel_socket_dgram_sync(ioc,
                                        data->localAddr,
                                        data->remoteAddr,
                                        errp);
    qio_channel_socket_dgram_sync(ioc, data->localAddr,
                                  data->remoteAddr, &err);

    object_unref(OBJECT(ioc));
    return ret;
    qio_task_set_error(task, err);
}


Loading