diff mbox

lib/vsprintf.c: Add %pMF to format FDDI bit reversed MAC addresses

Message ID 1262888625.10429.23.camel@Joe-Laptop.home
State Superseded, archived
Delegated to: David Miller
Headers show

Commit Message

Joe Perches Jan. 7, 2010, 6:23 p.m. UTC
On Mon, 2010-01-04 at 23:43 +0000, Maciej W. Rozycki wrote:
> The example below shows an address, and the sequence of bits or symbols 
> that would be transmitted when the address is used in the Source Address 
> or Destination Address fields on the MAC header.  The transmission line 
> shows the address bits in the order transmitted, from left to right.  For 
> IEEE 802 LANs these correspond to actual bits on the medium.  The FDDI 
> symbols line shows how the FDDI PHY sends the address bits as encoded 
> symbols.
> 
>         MSB:            35:7B:12:00:00:01
>         Canonical:      AC-DE-48-00-00-80
>         Transmission:   00110101 01111011 00010010 00000000 00000000 00000001
>         FDDI Symbols:   35 7B 12 00 00 01"
> 
> Please note that this address has its group bit clear.
> 
>  This notation is also defined in the "FDDI MEDIA ACCESS CONTROL-2 
> (MAC-2)" (X3T9/92-120) document although that book does not have a need 
> to use the MSB form and it's skipped.

Adds 56 bytes to object size

New:
$ size lib/vsprintf.o
   text	   data	    bss	    dec	    hex	filename
   8714	      0	      2	   8716	   220c	lib/vsprintf.o
old:
$ size lib/vsprintf.o
   text	   data	    bss	    dec	    hex	filename
   8658	      0	      2	   8660	   21d4	lib/vsprintf.o

Signed-off-by: Joe Perches <joe@perches.com>


--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Maciej W. Rozycki Jan. 7, 2010, 8:42 p.m. UTC | #1
On Thu, 7 Jan 2010, Joe Perches wrote:

> On Mon, 2010-01-04 at 23:43 +0000, Maciej W. Rozycki wrote:
> > The example below shows an address, and the sequence of bits or symbols 
> > that would be transmitted when the address is used in the Source Address 
> > or Destination Address fields on the MAC header.  The transmission line 
> > shows the address bits in the order transmitted, from left to right.  For 
> > IEEE 802 LANs these correspond to actual bits on the medium.  The FDDI 
> > symbols line shows how the FDDI PHY sends the address bits as encoded 
> > symbols.
> > 
> >         MSB:            35:7B:12:00:00:01
> >         Canonical:      AC-DE-48-00-00-80
> >         Transmission:   00110101 01111011 00010010 00000000 00000000 00000001
> >         FDDI Symbols:   35 7B 12 00 00 01"
> > 
> > Please note that this address has its group bit clear.
> > 
> >  This notation is also defined in the "FDDI MEDIA ACCESS CONTROL-2 
> > (MAC-2)" (X3T9/92-120) document although that book does not have a need 
> > to use the MSB form and it's skipped.
> 
> Adds 56 bytes to object size
> 
> New:
> $ size lib/vsprintf.o
>    text	   data	    bss	    dec	    hex	filename
>    8714	      0	      2	   8716	   220c	lib/vsprintf.o
> old:
> $ size lib/vsprintf.o
>    text	   data	    bss	    dec	    hex	filename
>    8658	      0	      2	   8660	   21d4	lib/vsprintf.o

 What's the gain?  I'd be rather conservative when taking everybody's 56 
bytes for one or two drivers hardly anybody uses.  The format of MAC 
addresses is unlikely to change, so I'd say the sources can live with 
one or two places where the strings are formatted manually.  Even if the 
drivers lose more than these 56 bytes.

  Maciej
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
=?ISO-8859-2?Q?Micha=B3_Miros=B3aw?= Jan. 7, 2010, 9:13 p.m. UTC | #2
2010/1/7 Maciej W. Rozycki <macro@linux-mips.org>:
> On Thu, 7 Jan 2010, Joe Perches wrote:
>> On Mon, 2010-01-04 at 23:43 +0000, Maciej W. Rozycki wrote:
>> > The example below shows an address, and the sequence of bits or symbols
>> > that would be transmitted when the address is used in the Source Address
>> > or Destination Address fields on the MAC header.  The transmission line
>> > shows the address bits in the order transmitted, from left to right.  For
>> > IEEE 802 LANs these correspond to actual bits on the medium.  The FDDI
>> > symbols line shows how the FDDI PHY sends the address bits as encoded
>> > symbols.
>> >
>> >         MSB:            35:7B:12:00:00:01
>> >         Canonical:      AC-DE-48-00-00-80
>> >         Transmission:   00110101 01111011 00010010 00000000 00000000 00000001
>> >         FDDI Symbols:   35 7B 12 00 00 01"
>> >
>> > Please note that this address has its group bit clear.
>> >
>> >  This notation is also defined in the "FDDI MEDIA ACCESS CONTROL-2
>> > (MAC-2)" (X3T9/92-120) document although that book does not have a need
>> > to use the MSB form and it's skipped.
>>
>> Adds 56 bytes to object size
>>
>> New:
>> $ size lib/vsprintf.o
>>    text          data     bss     dec     hex filename
>>    8714             0       2    8716    220c lib/vsprintf.o
>> old:
>> $ size lib/vsprintf.o
>>    text          data     bss     dec     hex filename
>>    8658             0       2    8660    21d4 lib/vsprintf.o
>
>  What's the gain?  I'd be rather conservative when taking everybody's 56
> bytes for one or two drivers hardly anybody uses.  The format of MAC
> addresses is unlikely to change, so I'd say the sources can live with
> one or two places where the strings are formatted manually.  Even if the
> drivers lose more than these 56 bytes.

Maybe this can be Kconfig-selected by the relevant drivers then?

BTW, the gain is of course consistency and code readability.

Best Regards,
Michał Mirosław
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
=?ISO-8859-2?Q?Micha=B3_Miros=B3aw?= Jan. 7, 2010, 9:18 p.m. UTC | #3
2010/1/7 Joe Perches <joe@perches.com>:
[...]
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index d4996cf..36959cc 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -25,6 +25,7 @@
>  #include <linux/kallsyms.h>
>  #include <linux/uaccess.h>
>  #include <linux/ioport.h>
> +#include <linux/bitrev.h>
>  #include <net/addrconf.h>
>
>  #include <asm/page.h>          /* for PAGE_SIZE */
> @@ -675,17 +676,37 @@ static char *resource_string(char *buf, char *end, struct resource *res,
>        return string(buf, end, sym, spec);
>  }
>
> +static u8 mac_byte(u8 val)
> +{
> +       return val;
> +}
> +
> +static u8 mac_byte_rev(u8 val)
> +{
> +       return bitrev8(val);
> +}
> +
>  static char *mac_address_string(char *buf, char *end, u8 *addr,
>                                struct printf_spec spec, const char *fmt)
>  {
>        char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
>        char *p = mac_addr;
>        int i;
> +       u8 (*mac_byte_fn)(u8 val);
> +       char separator;
> +
> +       if (fmt[1] == 'F') {            /* FDDI canonical format */
> +               mac_byte_fn = mac_byte_rev;
> +               separator = '-';
> +       } else {
> +               mac_byte_fn = mac_byte;
> +               separator  = ':';
> +       }
>
>        for (i = 0; i < 6; i++) {
> -               p = pack_hex_byte(p, addr[i]);
> +               p = pack_hex_byte(p, mac_byte_fn(addr[i]));
>                if (fmt[0] == 'M' && i != 5)
> -                       *p++ = ':';
> +                       *p++ = separator;
>        }
>        *p = '\0';
>
[...]

Something like the following might be smaller and faster - no
functions and their calls (just an idea);

int rev = (fmt[1] == 'F');
...
p = pack_hex_byte(p, rev ? bitrev8(addr[i]) : addr[i]);

Best Regards,
Michał Mirosław
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Joe Perches Jan. 7, 2010, 9:36 p.m. UTC | #4
On Thu, 2010-01-07 at 22:18 +0100, Michał Mirosław wrote: 
> Something like the following might be smaller and faster - no
> functions and their calls
> int rev = (fmt[1] == 'F');
> p = pack_hex_byte(p, rev ? bitrev8(addr[i]) : addr[i]);

Right, better.  Just 6 bytes (x86).  Resubmitting.


--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
David Miller Jan. 7, 2010, 10:09 p.m. UTC | #5
From: "Maciej W. Rozycki" <macro@linux-mips.org>
Date: Thu, 7 Jan 2010 20:42:12 +0000 (GMT)

>  What's the gain?  I'd be rather conservative when taking everybody's 56 
> bytes for one or two drivers hardly anybody uses.  The format of MAC 
> addresses is unlikely to change, so I'd say the sources can live with 
> one or two places where the strings are formatted manually.  Even if the 
> drivers lose more than these 56 bytes.

We gain consistency.  You know, the part that's completely bolixed
right now?

I'm applying Joe's patch and followon patches to make the driver's
use the new printf format.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Simon Horman Jan. 8, 2010, 12:08 a.m. UTC | #6
On Thu, Jan 07, 2010 at 02:09:42PM -0800, David Miller wrote:
> From: "Maciej W. Rozycki" <macro@linux-mips.org>
> Date: Thu, 7 Jan 2010 20:42:12 +0000 (GMT)
> 
> >  What's the gain?  I'd be rather conservative when taking everybody's 56 
> > bytes for one or two drivers hardly anybody uses.  The format of MAC 
> > addresses is unlikely to change, so I'd say the sources can live with 
> > one or two places where the strings are formatted manually.  Even if the 
> > drivers lose more than these 56 bytes.
> 
> We gain consistency.  You know, the part that's completely bolixed
> right now?
> 
> I'm applying Joe's patch and followon patches to make the driver's
> use the new printf format.

In the vein of this, would it be worth adding some modifier to %i4 to
print addresses for ftp helpers? That is, %u,%u,%u,%u. I think
there are currently two users of that format string after Joe's latest
round of updates.

The users are net/netfilter/ipvs/ip_vs_ftp.c and
net/ipv4/netfilter/nf_nat_ftp.c. Prior to Joe's latest
changes ip_vs_ftp.c used %d,%d,%d,%d.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Joe Perches Jan. 8, 2010, 1:51 a.m. UTC | #7
On Fri, 2010-01-08 at 11:08 +1100, Simon Horman wrote:
> In the vein of this, would it be worth adding some modifier to %i4 to
> print addresses for ftp helpers? That is, %u,%u,%u,%u. I think
> there are currently two users of that format string after Joe's latest
> round of updates.

I think not.  I think the comma separated uses
are unusual enough to not support in lib/vsprintf.

I did submit a patch to support format strings
for le32 and u32 ipv4 addresses.

> The users are net/netfilter/ipvs/ip_vs_ftp.c and
> net/ipv4/netfilter/nf_nat_ftp.c. Prior to Joe's latest
> changes ip_vs_ftp.c used %d,%d,%d,%d.

I submitted a couple of patches that should help.

o eventually remove NIPQUAD and NIPQUAD_FMT
o consolidate the 2 netfilter "%u,%u,%u,%u" uses
  to a single function



--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Simon Horman Jan. 8, 2010, 2:48 a.m. UTC | #8
On Thu, Jan 07, 2010 at 05:51:48PM -0800, Joe Perches wrote:
> On Fri, 2010-01-08 at 11:08 +1100, Simon Horman wrote:
> > In the vein of this, would it be worth adding some modifier to %i4 to
> > print addresses for ftp helpers? That is, %u,%u,%u,%u. I think
> > there are currently two users of that format string after Joe's latest
> > round of updates.
> 
> I think not.  I think the comma separated uses
> are unusual enough to not support in lib/vsprintf.

Sounds reasonable.

> I did submit a patch to support format strings
> for le32 and u32 ipv4 addresses.
> 
> > The users are net/netfilter/ipvs/ip_vs_ftp.c and
> > net/ipv4/netfilter/nf_nat_ftp.c. Prior to Joe's latest
> > changes ip_vs_ftp.c used %d,%d,%d,%d.
> 
> I submitted a couple of patches that should help.
> 
> o eventually remove NIPQUAD and NIPQUAD_FMT
> o consolidate the 2 netfilter "%u,%u,%u,%u" uses
>   to a single function

--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Andrew Morton Jan. 19, 2010, 10:57 a.m. UTC | #9
On Thu, 07 Jan 2010 10:23:45 -0800 Joe Perches <joe@perches.com> wrote:

> Adds 56 bytes to object size

You might want to consider making some of the net-specific code in
vsprintf.c go away if CONFIG_NET=n.

--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index d4996cf..36959cc 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -25,6 +25,7 @@ 
 #include <linux/kallsyms.h>
 #include <linux/uaccess.h>
 #include <linux/ioport.h>
+#include <linux/bitrev.h>
 #include <net/addrconf.h>
 
 #include <asm/page.h>		/* for PAGE_SIZE */
@@ -675,17 +676,37 @@  static char *resource_string(char *buf, char *end, struct resource *res,
 	return string(buf, end, sym, spec);
 }
 
+static u8 mac_byte(u8 val)
+{
+	return val;
+}
+
+static u8 mac_byte_rev(u8 val)
+{
+	return bitrev8(val);
+}
+
 static char *mac_address_string(char *buf, char *end, u8 *addr,
 				struct printf_spec spec, const char *fmt)
 {
 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
 	char *p = mac_addr;
 	int i;
+	u8 (*mac_byte_fn)(u8 val);
+	char separator;
+
+	if (fmt[1] == 'F') {		/* FDDI canonical format */
+		mac_byte_fn = mac_byte_rev;
+		separator = '-';
+	} else {
+		mac_byte_fn = mac_byte;
+		separator  = ':';
+	}
 
 	for (i = 0; i < 6; i++) {
-		p = pack_hex_byte(p, addr[i]);
+		p = pack_hex_byte(p, mac_byte_fn(addr[i]));
 		if (fmt[0] == 'M' && i != 5)
-			*p++ = ':';
+			*p++ = separator;
 	}
 	*p = '\0';
 
@@ -896,6 +917,10 @@  static char *uuid_string(char *buf, char *end, const u8 *addr,
  * - 'M' For a 6-byte MAC address, it prints the address in the
  *       usual colon-separated hex notation
  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
+ * - 'MF' For a 6-byte MAC FDDI address, it prints the address
+ *       with a dash-separated hex notation with bit reversed bytes
+ * - 'mF' For a 6-byte MAC FDDI address, it prints the address
+ *       in hex notation without separators with bit reversed bytes
  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
@@ -939,6 +964,7 @@  static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 		return resource_string(buf, end, ptr, spec, fmt);
 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
 	case 'm':			/* Contiguous: 000102030405 */
+					/* [mM]F (FDDI, bit reversed) */
 		return mac_address_string(buf, end, ptr, spec, fmt);
 	case 'I':			/* Formatted IP supported
 					 * 4:	1.2.3.4