Re: [patch iproute2 v5 1/3] lib/libnetlink: Add a function rtnl_talk_msg

2018-01-03 Thread Chris Mi

2018/1/3 12:08, David Ahern:

On 1/2/18 7:55 PM, Chris Mi wrote:

diff --git a/lib/libnetlink.c b/lib/libnetlink.c
index 00e6ce0c..cc02a139 100644
--- a/lib/libnetlink.c
+++ b/lib/libnetlink.c
@@ -581,32 +581,34 @@ static void rtnl_talk_error(struct nlmsghdr *h, struct 
nlmsgerr *err,
strerror(-err->error));
  }
  
-static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,

-  struct nlmsghdr **answer,
-  bool show_rtnl_err, nl_ext_ack_fn_t errfn)
+static int __rtnl_talk_msg(struct rtnl_handle *rtnl, struct msghdr *m,
+  struct nlmsghdr **answer,
+  bool show_rtnl_err, nl_ext_ack_fn_t errfn)
  {
-   int status;
-   unsigned int seq;
-   struct nlmsghdr *h;
+   int iovlen = m->msg_iovlen;
+   unsigned int seq = 0;
+   int i, status;
+   char *buf;
+
struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
-   struct iovec iov = {
-   .iov_base = n,
-   .iov_len = n->nlmsg_len
-   };
+   struct iovec iov, *v;
+   struct nlmsghdr *h;
struct msghdr msg = {
.msg_name = ,
.msg_namelen = sizeof(nladdr),
.msg_iov = ,
.msg_iovlen = 1,
};
-   char *buf;

Reverse xmas tree is the coding standard for net code. Please adhere to
it. Only dependencies between variables are an acceptable exception.

OK, got it.


Some of those (struct nlmsghdr *h and struct iovec *v) can be moved to
the for loop which aligns with your intentions of grouping variables.

Done.


  
-	n->nlmsg_seq = seq = ++rtnl->seq;

-
-   if (answer == NULL)
-   n->nlmsg_flags |= NLM_F_ACK;
+   for (i = 0; i < iovlen; i++) {
+   v = >msg_iov[i];
+   h = v->iov_base;
+   h->nlmsg_seq = seq = ++rtnl->seq;

doesn't seq need to track the recvmsg loop? I think for batching you
want it to start at the first seq number and then in the recvmsg loop
increment it.

Yes, it is a bug. Thanks for your test case.


As it stands this file:
$ cat tc.batch
filter add dev eth2 ingress protocol ip pref 21 flower dst_ip
192.168.1.0/16 action drop
filter add dev eth2 ingress protocol ip pref 22 flower dst_ip
192.168.2.0/16 action drop
filter add dev eth2 ingress protocol ip pref 22 flower dst_ip
192.168.3.0/16 action drop
filter add dev eth2 ingress protocol ip pref 24 flower dst_ip
192.168.4.0/16 action drop
filter add dev eth2 ingress protocol ip pref 25 flower dst_ip
192.168.5.0/16 action drop

does not give me an error message:
$ tc -b tc.batch -bs 5


Yet it failed to insert all filters:
$ tc filter show dev eth2 ingress
filter protocol ip pref 21 flower chain 0
filter protocol ip pref 21 flower chain 0 handle 0x1
   eth_type ipv4
   dst_ip 192.168.1.0/16
   not_in_hw
action order 1: gact action drop
 random type none pass val 0
 index 1 ref 1 bind 1

filter protocol ip pref 22 flower chain 0
filter protocol ip pref 22 flower chain 0 handle 0x1
   eth_type ipv4
   dst_ip 192.168.2.0/16
   not_in_hw
action order 1: gact action drop
 random type none pass val 0
 index 2 ref 1 bind 1

filter protocol ip pref 24 flower chain 0
filter protocol ip pref 24 flower chain 0 handle 0x1
   eth_type ipv4
   dst_ip 192.168.4.0/16
   not_in_hw
action order 1: gact action drop
 random type none pass val 0
 index 3 ref 1 bind 1

filter protocol ip pref 25 flower chain 0
filter protocol ip pref 25 flower chain 0 handle 0x1
   eth_type ipv4
   dst_ip 192.168.5.0/16
   not_in_hw
action order 1: gact action drop
 random type none pass val 0
 index 4 ref 1 bind 1


After fixing it, the test result is:

# tc -b tc.batch -bs 5
RTNETLINK answers: File exists
We have an error talking to the kernel, -1
Command failed 1.txt:0-4

We can't tell exactly which command causes this error, so we give a 
range which is less than the batch size.


Re: [patch iproute2 v5 1/3] lib/libnetlink: Add a function rtnl_talk_msg

2018-01-02 Thread David Ahern
On 1/2/18 7:55 PM, Chris Mi wrote:
> diff --git a/lib/libnetlink.c b/lib/libnetlink.c
> index 00e6ce0c..cc02a139 100644
> --- a/lib/libnetlink.c
> +++ b/lib/libnetlink.c
> @@ -581,32 +581,34 @@ static void rtnl_talk_error(struct nlmsghdr *h, struct 
> nlmsgerr *err,
>   strerror(-err->error));
>  }
>  
> -static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
> -struct nlmsghdr **answer,
> -bool show_rtnl_err, nl_ext_ack_fn_t errfn)
> +static int __rtnl_talk_msg(struct rtnl_handle *rtnl, struct msghdr *m,
> +struct nlmsghdr **answer,
> +bool show_rtnl_err, nl_ext_ack_fn_t errfn)
>  {
> - int status;
> - unsigned int seq;
> - struct nlmsghdr *h;
> + int iovlen = m->msg_iovlen;
> + unsigned int seq = 0;
> + int i, status;
> + char *buf;
> +
>   struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
> - struct iovec iov = {
> - .iov_base = n,
> - .iov_len = n->nlmsg_len
> - };
> + struct iovec iov, *v;
> + struct nlmsghdr *h;
>   struct msghdr msg = {
>   .msg_name = ,
>   .msg_namelen = sizeof(nladdr),
>   .msg_iov = ,
>   .msg_iovlen = 1,
>   };
> - char *buf;

Reverse xmas tree is the coding standard for net code. Please adhere to
it. Only dependencies between variables are an acceptable exception.

Some of those (struct nlmsghdr *h and struct iovec *v) can be moved to
the for loop which aligns with your intentions of grouping variables.

>  
> - n->nlmsg_seq = seq = ++rtnl->seq;
> -
> - if (answer == NULL)
> - n->nlmsg_flags |= NLM_F_ACK;
> + for (i = 0; i < iovlen; i++) {
> + v = >msg_iov[i];
> + h = v->iov_base;
> + h->nlmsg_seq = seq = ++rtnl->seq;

doesn't seq need to track the recvmsg loop? I think for batching you
want it to start at the first seq number and then in the recvmsg loop
increment it.

As it stands this file:
$ cat tc.batch
filter add dev eth2 ingress protocol ip pref 21 flower dst_ip
192.168.1.0/16 action drop
filter add dev eth2 ingress protocol ip pref 22 flower dst_ip
192.168.2.0/16 action drop
filter add dev eth2 ingress protocol ip pref 22 flower dst_ip
192.168.3.0/16 action drop
filter add dev eth2 ingress protocol ip pref 24 flower dst_ip
192.168.4.0/16 action drop
filter add dev eth2 ingress protocol ip pref 25 flower dst_ip
192.168.5.0/16 action drop

does not give me an error message:
$ tc -b tc.batch -bs 5


Yet it failed to insert all filters:
$ tc filter show dev eth2 ingress
filter protocol ip pref 21 flower chain 0
filter protocol ip pref 21 flower chain 0 handle 0x1
  eth_type ipv4
  dst_ip 192.168.1.0/16
  not_in_hw
action order 1: gact action drop
 random type none pass val 0
 index 1 ref 1 bind 1

filter protocol ip pref 22 flower chain 0
filter protocol ip pref 22 flower chain 0 handle 0x1
  eth_type ipv4
  dst_ip 192.168.2.0/16
  not_in_hw
action order 1: gact action drop
 random type none pass val 0
 index 2 ref 1 bind 1

filter protocol ip pref 24 flower chain 0
filter protocol ip pref 24 flower chain 0 handle 0x1
  eth_type ipv4
  dst_ip 192.168.4.0/16
  not_in_hw
action order 1: gact action drop
 random type none pass val 0
 index 3 ref 1 bind 1

filter protocol ip pref 25 flower chain 0
filter protocol ip pref 25 flower chain 0 handle 0x1
  eth_type ipv4
  dst_ip 192.168.5.0/16
  not_in_hw
action order 1: gact action drop
 random type none pass val 0
 index 4 ref 1 bind 1