[PATCH 5/8] nl_cb: store nl_cb_type in struct nl_cb
Arend van Spriel
arend at broadcom.com
Thu Sep 5 08:17:01 EDT 2013
On 09/05/2013 02:11 PM, Arend van Spriel wrote:
> The application could use the same handler for multiple
> nl_cb_type events. This patch stores the nl_cb_type in
> the nl_cb struct during the callback. This allows the
> application to obtain that information using the new
> nl_cb_active_type() function. This way the callback
> signature remains as is so existing applications are
> not affected.
I guess my editor got carried away on this one cleaning up whitespace.
Sorry about that.
Regards,
Arend
> Signed-off-by: Arend van Spriel <arend at broadcom.com>
> ---
> include/netlink-private/netlink.h | 7 ++++++-
> include/netlink-private/types.h | 16 +++++++++-------
> include/netlink/handlers.h | 2 ++
> lib/handlers.c | 24 ++++++++++++++++++------
> 4 files changed, 35 insertions(+), 14 deletions(-)
>
> diff --git a/include/netlink-private/netlink.h b/include/netlink-private/netlink.h
> index 2e511bf..25d09ba 100644
> --- a/include/netlink-private/netlink.h
> +++ b/include/netlink-private/netlink.h
> @@ -126,7 +126,12 @@ extern void dump_from_ops(struct nl_object *, struct nl_dump_params *);
>
> static inline int nl_cb_call(struct nl_cb *cb, int type, struct nl_msg *msg)
> {
> - return cb->cb_set[type](msg, cb->cb_args[type]);
> + int ret;
> +
> + cb->cb_active = type;
> + ret = cb->cb_set[type](msg, cb->cb_args[type]);
> + cb->cb_active = __NL_CB_TYPE_MAX;
> + return ret;
> }
>
> #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
> diff --git a/include/netlink-private/types.h b/include/netlink-private/types.h
> index 05a6a45..9f66307 100644
> --- a/include/netlink-private/types.h
> +++ b/include/netlink-private/types.h
> @@ -38,7 +38,7 @@ struct nl_cb
> {
> nl_recvmsg_msg_cb_t cb_set[NL_CB_TYPE_MAX+1];
> void * cb_args[NL_CB_TYPE_MAX+1];
> -
> +
> nl_recvmsg_err_cb_t cb_err;
> void * cb_err_arg;
>
> @@ -60,6 +60,8 @@ struct nl_cb
> struct nl_msg *);
>
> int cb_refcnt;
> + /** indicates the callback that is currently active */
> + enum nl_cb_type cb_active;
> };
>
> struct nl_sock
> @@ -165,7 +167,7 @@ struct rtnl_link
> uint32_t l_txqlen;
> uint32_t l_weight;
> uint32_t l_master;
> - struct nl_addr * l_addr;
> + struct nl_addr * l_addr;
> struct nl_addr * l_bcast;
> char l_qdisc[IFQDISCSIZ];
> struct rtnl_link_map l_map;
> @@ -206,9 +208,9 @@ struct rtnl_neigh
> uint32_t n_ifindex;
> uint16_t n_state;
> uint8_t n_flags;
> - uint8_t n_type;
> + uint8_t n_type;
> struct nl_addr *n_lladdr;
> - struct nl_addr *n_dst;
> + struct nl_addr *n_dst;
> uint32_t n_probes;
> struct rtnl_ncacheinfo n_cacheinfo;
> uint32_t n_state_mask;
> @@ -242,14 +244,14 @@ struct rtnl_addr
> uint8_t a_scope;
> uint32_t a_ifindex;
>
> - struct nl_addr *a_peer;
> + struct nl_addr *a_peer;
> struct nl_addr *a_local;
> struct nl_addr *a_bcast;
> struct nl_addr *a_anycast;
> struct nl_addr *a_multicast;
>
> struct rtnl_addr_cacheinfo a_cacheinfo;
> -
> +
> char a_label[IFNAMSIZ];
> uint32_t a_flag_mask;
> struct rtnl_link *a_link;
> @@ -401,7 +403,7 @@ struct rtnl_neightbl_parms
> * Queue length for the delayed proxy arp requests.
> */
> uint32_t ntp_proxy_qlen;
> -
> +
> /**
> * Mask of available parameter attributes
> */
> diff --git a/include/netlink/handlers.h b/include/netlink/handlers.h
> index dfa2809..e94cd34 100644
> --- a/include/netlink/handlers.h
> +++ b/include/netlink/handlers.h
> @@ -139,6 +139,8 @@ extern void nl_cb_overwrite_send(struct nl_cb *,
> int (*func)(struct nl_sock *,
> struct nl_msg *));
>
> +extern enum nl_cb_type nl_cb_active_type(struct nl_cb *cb);
> +
> #ifdef __cplusplus
> }
> #endif
> diff --git a/lib/handlers.c b/lib/handlers.c
> index e52c850..a6a97bb 100644
> --- a/lib/handlers.c
> +++ b/lib/handlers.c
> @@ -35,7 +35,7 @@ static void print_header_content(FILE *ofd, struct nlmsghdr *n)
> {
> char flags[128];
> char type[32];
> -
> +
> fprintf(ofd, "type=%s length=%u flags=<%s> sequence-nr=%u pid=%u",
> nl_nlmsgtype2str(n->nlmsg_type, type, sizeof(type)),
> n->nlmsg_len, nl_nlmsg_flags2str(n->nlmsg_flags, flags,
> @@ -71,7 +71,7 @@ static int nl_overrun_handler_verbose(struct nl_msg *msg, void *arg)
> fprintf(ofd, "-- Error: Netlink Overrun: ");
> print_header_content(ofd, nlmsg_hdr(msg));
> fprintf(ofd, "\n");
> -
> +
> return NL_STOP;
> }
>
> @@ -107,7 +107,7 @@ static int nl_finish_handler_debug(struct nl_msg *msg, void *arg)
> fprintf(ofd, "-- Debug: End of multipart message block: ");
> print_header_content(ofd, nlmsg_hdr(msg));
> fprintf(ofd, "\n");
> -
> +
> return NL_STOP;
> }
>
> @@ -117,7 +117,7 @@ static int nl_msg_in_handler_debug(struct nl_msg *msg, void *arg)
>
> fprintf(ofd, "-- Debug: Received Message:\n");
> nl_msg_dump(msg, ofd);
> -
> +
> return NL_OK;
> }
>
> @@ -211,6 +211,7 @@ struct nl_cb *nl_cb_alloc(enum nl_cb_kind kind)
> return NULL;
>
> cb->cb_refcnt = 1;
> + cb->cb_active = NL_CB_TYPE_MAX + 1;
>
> for (i = 0; i <= NL_CB_TYPE_MAX; i++)
> nl_cb_set(cb, i, kind, NULL, NULL);
> @@ -229,7 +230,7 @@ struct nl_cb *nl_cb_alloc(enum nl_cb_kind kind)
> struct nl_cb *nl_cb_clone(struct nl_cb *orig)
> {
> struct nl_cb *cb;
> -
> +
> cb = nl_cb_alloc(NL_CB_DEFAULT);
> if (!cb)
> return NULL;
> @@ -261,6 +262,17 @@ void nl_cb_put(struct nl_cb *cb)
> free(cb);
> }
>
> +/**
> + * Obtain type of current active callback
> + * @arg cb callback to query
> + *
> + * @return type or __NL_CB_TYPE_MAX if none active
> + */
> +enum nl_cb_type nl_cb_active_type(struct nl_cb *cb)
> +{
> + return cb->cb_active;
> +}
> +
> /** @} */
>
> /**
> @@ -269,7 +281,7 @@ void nl_cb_put(struct nl_cb *cb)
> */
>
> /**
> - * Set up a callback
> + * Set up a callback
> * @arg cb callback set
> * @arg type callback to modify
> * @arg kind kind of implementation
>
More information about the libnl
mailing list