2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-22 09:58:01 +00:00

55 Commits

Author SHA1 Message Date
Ilya Maximets
1de4a08c22 json: Use functions to access json arrays.
Internal implementation of JSON array will be changed in the future
commits.  Add access functions that users can rely on instead of
accessing the internals of 'struct json' directly and convert all the
users.  Structure fields are intentionally renamed to make sure that
no code is using the old fields directly.

json_array() function is removed, as not needed anymore.  Added new
functions:  json_array_size(), json_array_at(), json_array_set()
and json_array_pop().  These are enough to cover all the use cases
within OVS.

The change is fairly large, however, IMO, it's a much overdue cleanup
that we need even without changing the underlying implementation.

Acked-by: Mike Pattrick <mkp@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2025-06-30 16:53:56 +02:00
Ilya Maximets
9669b50f56 json: Store short strings in-place.
The 'struct json' contains a union and the largest element of that
union is 'struct json_array', which takes 24 bytes.  It means, that a
lot of space in this structure remains unused whenever the type is not
JSON_ARRAY.

For example, the 'string' pointer used for JSON_STRING only takes 8
bytes on a 64-bit system leaving 24 - 8 = 16 bytes unused.  There is
also a 4-byte hole between the 'type' and the 'count'.

A pretty common optimization technique for storing strings is to
store short ones in place of the pointer and only allocate dynamically
the larger strings that do not fit.  In our case, we have even larger
space of 24 bytes to work with.  So, we could use all 24 bytes to
store the strings (23 string bytes + '\0') and use the 4 byte unused
space outside the union to store the storage type.

This approach should allow us to save on memory allocation for short
strings and also save on accesses to them, as the content will fit
into the same cache line as the 'struct json' itself.

In practice, large OVN databases tend to operate with quite large
strings.  For example, all the logical flow matches and actions in
OVN Southbound database would not fit.  However, this approach still
allows to improve performance with large OVN databases.

With 350MB OVN Northbound database with 12M atoms:

                         Before        After       Improvement
 ovsdb-client dump      18.6 sec      16.6 sec       10.7 %
 Compaction             14.0 sec      13.4 sec        4.2 %
 Memory usage (RSS)     2.28 GB       2.05 GB        10.0 %

With 615MB OVN Southbound database with 23M atoms:

                         Before        After       Improvement
 ovsdb-client dump      46.1 sec      43.7 sec        5.2 %
 Compaction             34.8 sec      32.5 sec        6.6 %
 Memory usage (RSS)     5.29 GB       4.80 GB         9.3 %

In the results above, 'ovsdb-client dump' is measuring how log it
takes for the server to prepare and send a reply, 'Memory usage (RSS)'
reflects the RSS of the ovsdb-server after loading the full database.
ovn-heater tests report similar reduction in CPU and memory usage
on heavy operations like compaction.

Acked-by: Mike Pattrick <mkp@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2025-06-30 16:53:56 +02:00
Ilya Maximets
6c48b29f52 json: Always use the json_string() method to access the strings.
We'll be changing the way strings are stored, so the direct access
will not be safe anymore.  Change all the users to use the proper
API as they should have been doing anyway.  This also means splitting
the handling of strings and serialized objects in most cases as
they will be treated differently.

The only code outside of json implementation for which direct access
is preserved is substitute_uuids() in test-ovsdb.c.  It's an unusual
string manipulation that is only needed for the testing, so doesn't
seem worthy adding a new API function.  We could introduce something
like json_string_replace() if this use case will appear somewhere
else in the future.

Acked-by: Mike Pattrick <mkp@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2025-06-30 16:53:56 +02:00
Dmitry Porokh
421c94ee14 ovsdb: Introduce and use specialized uuid print functions.
According to profiling data, converting UUIDs to strings is a frequent
operation in some workloads. This typically results in a call to
xasprintf(), which internally calls vsnprintf() twice, first to
calculate the required buffer size, and then to format the string.

This patch introduces specialized functions for printing UUIDs, which
both reduces code duplication and improves performance.

For example, on my laptop, 10,000,000 calls to the new uuid_to_string()
function takes 1296 ms, while the same number of xasprintf() calls using
UUID_FMT take 2498 ms.

Signed-off-by: Dmitry Porokh <dporokh@nvidia.com>
Signed-off-by: Eelco Chaudron <echaudro@redhat.com>
2025-05-08 09:28:21 +02:00
Frode Nordahl
36bad31829 json: Add yielding json create/destroy functions.
Creating and destroying JSON objects may be time consuming.

Add json_serialized_object_create_with_yield() and
json_destroy_with_yield() functions that make use of the
cooperative multitasking module to yield during processing,
allowing time sensitive tasks in other parts of the program
to be completed during processing.

We keep these new functions private to OVS by adding a new
lib/json.h header file.

The include guard in the public include/openvswitch/json.h is
updated to contain the OPENVSWITCH prefix to be in line with the
other public header files, allowing us to use the non-prefixed
version in our private lib/json.h.

Signed-off-by: Frode Nordahl <frode.nordahl@canonical.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-01-17 14:41:18 +01:00
Ilya Maximets
3ff980c854 ovsdb: replication: Isolate databases from each other.
Refactoring of the replication code, so each database is handled
separately from each other.  Supposed to work the same way as before
with the only difference that each backup database will have its own
connection to the source and will have its own state machine.

From the user's perspective, the only visible difference is that
ovsdb-server/sync-status appctl now shows the status of each
database separately.

If one of the connections is permanently broken, all the databases
will be switched to active.  This is done in order to preserve the
old behavior where we had only one connection.

Acked-by: Dumitru Ceara <dceara@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-01-15 21:18:29 +01:00
Ilya Maximets
0b21e23431 json: Fix deep copy of objects and arrays.
When reference counting for json objects was introduced the
old json_clone() function became json_deep_clone(), but it
still calls shallow json_clone() while cloning objects and
arrays not really producing a deep copy.

Fixing that by making other functions to perform a deep copy
as well.  There are no users for this functionality inside
OVS right now, but OVS exports this functionality externally.

'ovstest test-json' extended to test both versions of a clone
on provided inputs.

Fixes: 9854d473adea ("json: Use reference counting in JSON objects")
Acked-by: Dumitru Ceara <dceara@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2022-10-11 21:10:46 +02:00
Rosemarie O'Riorden
2f16123c1b json: Improve string parsing.
To parse a json string prior to this change, json_lex_input is called
with each character of the string. If the character needs to be copied
to the buffer, it is copied individually. This is an expensive
operation, as often there are multiple characters in a row
that need to be copied, and copying memory in blocks is more efficient
than byte by byte. To improve this, the string is now copied
in blocks with an offset counter. A copy is performed when the parser
state equals done.

Functions that are called for each character use a lot of CPU cycles.
Making these functions inline greatly reduces the cycles used and
improves overall performance. Since json_lex_input was only needed in
one place, it doesn't have to be its own function.

There is also a conditional that checks if the current character is a
new line, which is quite unlikely. When this was examined with perf, the
comparison had a very high CPU cycle usage. To improve this, the
OVS_UNLIKELY macro was used, which forces the compiler to switch the
order of the instructions.

Here is the improvement seen in the json-string-benchmark test:

  SIZE      Q  S         BEFORE       AFTER      CHANGE
--------------------------------------------------------
100000      0  0 :      0.842 ms     0.489 ms   -41.9 %
100000      2  1 :      0.917 ms     0.535 ms   -41.7 %
100000      10 1 :      1.063 ms     0.656 ms   -38.3 %
10000000    0  0 :     85.328 ms    49.878 ms   -41.5 %
10000000    2  1 :     92.555 ms    54.778 ms   -40.8 %
10000000    10 1 :    106.728 ms    66.735 ms   -37.5 %
100000000   0  0 :    955.375 ms   621.950 ms   -34.9 %
100000000   2  1 :   1031.700 ms   665.200 ms   -35.5 %
100000000   10 1 :   1189.300 ms   796.050 ms   -33.0 %

Here Q is probability (%) for a character to be a '\"' and
S is probability (%) to be a special character ( < 32).

Signed-off-by: Rosemarie O'Riorden <roriorden@redhat.com>
Acked-by: Dumitru Ceara <dceara@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2022-04-04 22:52:12 +02:00
Adrian Moreno
9e56549c2b hmap: use short version of safe loops if possible.
Using SHORT version of the *_SAFE loops makes the code cleaner and less
error prone. So, use the SHORT version and remove the extra variable
when possible for hmap and all its derived types.

In order to be able to use both long and short versions without changing
the name of the macro for all the clients, overload the existing name
and select the appropriate version depending on the number of arguments.

Acked-by: Dumitru Ceara <dceara@redhat.com>
Acked-by: Eelco Chaudron <echaudro@redhat.com>
Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2022-03-30 16:59:02 +02:00
Ilya Maximets
9d29990c21 json: Inline clone and destroy functions.
With the next commit reference counting of json objects will take
significant part of the CPU time for ovsdb-server.  Inlining them
to reduce the cost of a function call.

Acked-by: Mike Pattrick <mkp@redhat.com>
Acked-by: Dumitru Ceara <dceara@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2021-11-30 13:33:43 +01:00
Ilya Maximets
b0bca6f27a json: Add support for partially serialized json objects.
Introducing a new json type JSON_SERIALIZED_OBJECT.  It's not an
actual type that can be seen in a json message on a wire, but
internal type that is intended to hold a serialized version of
some other json object.  For this reason it's defined after the
JSON_N_TYPES to not confuse parsers and other parts of the code
that relies on compliance with RFC 4627.

With this JSON type internal users may construct large JSON objects,
parts of which are already serialized.  This way, while serializing
the larger object, data from JSON_SERIALIZED_OBJECT can be added
directly to the result, without additional processing.

This will be used by next commits to add pre-serialized JSON data
to the raft_header structure, that can be converted to a JSON
before writing the file transaction on disk or sending to other
servers.  Same technique can also be used to pre-serialize json_cache
for ovsdb monitors, this should allow to not perform serialization
for every client and will save some more memory.

Since serialized JSON is just a string, reusing the 'json->string'
pointer for it.

Acked-by: Dumitru Ceara <dceara@redhat.com>
Acked-by: Han Zhou <hzhou@ovn.org>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2021-08-31 22:19:46 +02:00
Ilya Maximets
748010ff30 json: Optimize string serialization.
Current string serialization code puts all characters one by one.
This is slow because dynamic string needs to perform length checks
on every ds_put_char() and it's also doesn't allow compiler to use
better memory copy operations, i.e. doesn't allow copying few bytes
at once.

Special symbols are rare in a typical database.  Quotes are frequent,
but not too frequent.  In databases created by ovn-kubernetes, for
example, usually there are at least 10 to 50 chars between quotes.
So, it's better to count characters that doesn't require escaping
and use fast data copy for the whole sequential block.

Testing with a synthetic benchmark (included) on my laptop shows
following performance improvement:

   Size      Q  S       Before       After       Diff
 -----------------------------------------------------
 100000      0  0 :    0.227 ms     0.142 ms   -37.4 %
 100000      2  1 :    0.277 ms     0.186 ms   -32.8 %
 100000      10 1 :    0.361 ms     0.309 ms   -14.4 %
 10000000    0  0 :   22.720 ms    12.160 ms   -46.4 %
 10000000    2  1 :   27.470 ms    19.300 ms   -29.7 %
 10000000    10 1 :   37.950 ms    31.250 ms   -17.6 %
 100000000   0  0 :  239.600 ms   126.700 ms   -47.1 %
 100000000   2  1 :  292.400 ms   188.600 ms   -35.4 %
 100000000   10 1 :  387.700 ms   321.200 ms   -17.1 %

Here Q - probability (%) for a character to be a '\"' and
S - probability (%) to be a special character ( < 32).

Testing with a closer to real world scenario shows overall decrease
of the time needed for database compaction by ~5-10 %.  And this
change also decreases CPU consumption in general, because string
serialization is used in many different places including ovsdb
monitors and raft.

Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
Acked-by: Numan Siddique <numans@ovn.org>
Acked-by: Dumitru Ceara <dceara@redhat.com>
2021-08-31 19:04:08 +02:00
Ben Pfaff
f1a57715f9 json: Avoid signed integer overflow in parsing exponents.
This can't cause a crash and doesn't seem relevant to normal operation.

Reported-at: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=9044
Signed-off-by: Ben Pfaff <blp@ovn.org>
Acked-by: Justin Pettit <jpettit@ovn.org>
2018-07-05 15:08:20 -07:00
Ben Pfaff
fa37affad3 Embrace anonymous unions.
Several OVS structs contain embedded named unions, like this:

struct {
    ...
    union {
        ...
    } u;
};

C11 standardized a feature that many compilers already implemented
anyway, where an embedded union may be unnamed, like this:

struct {
    ...
    union {
        ...
    };
};

This is more convenient because it allows the programmer to omit "u."
in many places.  OVS already used this feature in several places.  This
commit embraces it in several others.

Signed-off-by: Ben Pfaff <blp@ovn.org>
Acked-by: Justin Pettit <jpettit@ovn.org>
Tested-by: Alin Gabriel Serdean <aserdean@ovn.org>
Acked-by: Alin Gabriel Serdean <aserdean@ovn.org>
2018-05-25 13:36:05 -07:00
Ben Pfaff
828129d927 json: Avoid extra memory allocation and string copy parsing object members.
Until now, every time the JSON parser added an object member, it made an
extra copy of the member name and then freed the original copy.  This is
wasteful, so this commit eliminates the extra copy.

Signed-off-by: Ben Pfaff <blp@ovn.org>
Reviewed-by: Yifeng Sun <pkusunyifeng@gmail.com>
2018-03-31 12:22:58 -07:00
Ben Pfaff
f15a2320cc json: Make it safe to pass null pointers to json_equal().
Signed-off-by: Ben Pfaff <blp@ovn.org>
Reviewed-by: Yifeng Sun <pkusunyifeng@gmail.com>
Acked-by: Justin Pettit <jpettit@ovn.org>
2018-02-06 11:17:04 -08:00
Ben Pfaff
52a9c55d55 json: New function json_object_put_format().
This will acquire users in an upcoming commit.

Signed-off-by: Ben Pfaff <blp@ovn.org>
Acked-by: Russell Bryant <russell@ovn.org>
2017-10-24 16:09:17 -07:00
Ben Pfaff
d697750af6 json: New function json_nullable_clone().
Signed-off-by: Ben Pfaff <blp@ovn.org>
Acked-by: Russell Bryant <russell@ovn.org>
2017-10-24 16:09:16 -07:00
Joe Stringer
7dd9c9a220 json: Fix non-static json char lookup table.
This warning breaks the build on travis:
lib/json.c:1627:12: error: symbol 'chars_escaping' was not declared.
Should it be static?

CC: Esteban Rodriguez Betancourt <estebarb@hpe.com>
Reported-At: https://travis-ci.org/openvswitch/ovs/jobs/165300417
Fixes: 644ecb10a661 ("json: Serialize strings using a lookup table")
Signed-off-by: Joe Stringer <joe@ovn.org>
Acked-by: Ben Pfaff <blp@ovn.org>
2016-10-06 11:57:53 +09:00
Rodriguez Betancourt, Esteban
644ecb10a6 json: Serialize strings using a lookup table
The existing implementation uses a switch with
many conditions, that when compiled is translated
to a not optimal series of conditional jumps.

With a lookup table the generated code has less conditional jumps,
that should translate in improving the CPU ability to predict the
jumps.

Performance Comparison:
All the timings are in nanoseconds, "OVS Master" corresponds to 13a1d36.
N is the number of repetitions

Serialize vswitch.ovsschema
N        OVS Master  Lookup Table    Difference    Diff per op
1           233182        200369       32813        32813
10         2724931       1919168      805763        80576.3
100       22802794      24406648    -1603854       -16038.54
1000     253645888     206259760    47386128        47386.128
10000   2352245703    1906839780   445405923        44540.5923
100000 23967770920   19012178655  4955592265        49555.92265

Serialize echo example
N        OVS Master  Lookup Table    Difference    Diff per op
1            3857        12565         -8708        -8708
10          17403         7312         10091         1009.1
100         57859        56613          1246           12.46
1000       592310       528110         64200           64.2
10000     6096334      5576109        520225           52.0225
100000   60970439     58477626       2492813           24.92813

Serialize mutate example
N        OVS Master  Lookup Table    Difference    Diff per op
1            7115          19051         -11936      -11936
10          34110          39561          -5451        -545.1
100        296613         298645          -2032        -20.32
1000      3510499        2930588         579911        579.911
10000    33898710       30278631        3620079        362.0079
100000  305069356      280622992       24446364        244.46364

Signed-off-by: Esteban Rodriguez Betancourt <estebarb@hpe.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
2016-10-05 10:10:03 -07:00
Rodriguez Betancourt, Esteban
9854d473ad json: Use reference counting in JSON objects
After profiling OVSDB insert performance it was found
that some significant portion of its time OVSDB is
calling the function json_clone.

Also, the current usages of json_clone never modify the json,
just keeps it to prevent it to be freed.

With that in mind the struct json, json_create, json_clone
and json_destroy were modified to keep a count of how many
references of the json struct are left. Only when that count
reaches zero the json struct is freed.

The old "json_clone" function was renamed as "json_deep_clone".

Some examples of the performance difference:

In these tests a test table with 4 columns (string, string,
bool, integer) was used. All the tests used "commit block".

*** 50 process each inserting 1000 rows ***

Master OVS
Test Duration                   131 seconds
Average Inserts Per second      746.2687 inserts/s
Average Insert Duration         134.1382 ms
Minimal Insert Duration           0.166202 ms
Maximum Insert Duration	        489.8593 ms

JSON GC Patch
Test Duration                   86 seconds
Average Inserts Per second    1176 inserts/s
Average Insert Duration         82.26761 ms
Minimal Insert Duration          0.165448 ms
Maximum Insert Duration        751.2111 ms

*** 5 process each inserting 10000 rows ***

Master OVS
Test Duration                      8 seconds
Average Inserts Per second      7142.857 inserts/s
Average Insert Duration            0.656431 ms
Minimal Insert Duration            0.125197 ms
Maximum Insert Duration           11.93203 ms

JSON GC Patch
Test Duration                      7 seconds
Average Inserts Per second      8333.333 inserts/s
Average Insert Duration            0.55688 ms
Minimal Insert Duration            0.143233 ms
Maximum Insert Duration           26.26319 ms

Signed-off-by: Esteban Rodriguez Betancourt <estebarb@hpe.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
2016-10-04 16:04:25 -07:00
Terry Wilson
ee89ea7b47 json: Move from lib to include/openvswitch.
To easily allow both in- and out-of-tree building of the Python
wrapper for the OVS JSON parser (e.g. w/ pip), move json.h to
include/openvswitch. This also requires moving lib/{hmap,shash}.h.

Both hmap.h and shash.h were #include-ing "util.h" even though the
headers themselves did not use anything from there, but rather from
include/openvswitch/util.h. Fixing that required including util.h
in several C files mostly due to OVS_NOT_REACHED and things like
xmalloc.

Signed-off-by: Terry Wilson <twilson@redhat.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
2016-07-22 17:09:17 -07:00
Ben Warren
3e8a2ad145 Move lib/dynamic-string.h to include/openvswitch directory
Signed-off-by: Ben Warren <ben@skyportsystems.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
2016-03-19 10:02:12 -07:00
Ben Pfaff
03ce866e44 Merge "master" into "ovn".
This allows OVN to take advantage of the client scalability changes
that have been committed to ovsdb-server on master recently.

Conflicts:
	Makefile.am
	lib/learn.c
2015-06-13 18:12:08 -07:00
Ben Pfaff
5aa7f168b1 json: Fix error message for corner case in json_string_unescape().
The error message should not include bytes already copied from the input
string.

Found by inspection.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Alex Wang <alexw@nicira.com>
2015-05-29 15:17:24 -07:00
Ben Pfaff
3b6267714b json: New function json_string_escape().
This saves some cut-and-paste duplicated code elsewhere and will have
additional users in upcoming commits.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Russell Bryant <rbryant@redhat.com>
2015-04-20 14:02:48 -07:00
Thomas Graf
cab5044987 lib: Move compiler.h to <openvswitch/compiler.h>
The following macros are renamed to avoid conflicts with other headers:
 * WARN_UNUSED_RESULT to OVS_WARN_UNUSED_RESULT
 * PRINTF_FORMAT to OVS_PRINTF_FORMAT
 * NO_RETURN to OVS_NO_RETURN

Signed-off-by: Thomas Graf <tgraf@noironetworks.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2014-12-15 14:14:47 +01:00
Ben Pfaff
0429d9599c util: Make hexits_value() support 64-bit integers too.
Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
2014-10-08 14:47:22 -07:00
Thomas Graf
f5c117132d json: Fix leaked nodes in json_hash_object()
nodes is allocated through shash_sort() but never freed.

Signed-off-by: Thomas Graf <tgraf@noironetworks.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2014-08-26 09:08:52 -07:00
Ben Pfaff
7b7c2a463d json: Fix parsing of strings that end with a backslash.
json_string_unescape() flagged a backslash at the end of a string as an
error, but of course "\\" is a valid string.  This fixes the problem.

VMware-BZ: #1275208
Reported-by: Michael Hu <mhu@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Alex Wang <alexw@nicira.com>
2014-06-25 11:39:25 -07:00
Ben Pfaff
a11fdef12e Revert "json: New function json_serialized_length()."
This reverts commit 1600fa6853872e16130366351a2c14f6fa8b547c.

Connections that queue up too much data, because they are monitoring a
table that is changing quickly and failing to keep up with the updates,
cause problems with buffer management.  Since commit 60533a405b2e
(jsonrpc-server: Disconnect connections that queue too much data.),
ovsdb-server has dealt with them by disconnecting the connection and
letting them start up again with a fresh copy of the database.  However,
this is not ideal because of situations where disconnection happens
repeatedly.  For example:

     - A manager toggles a column back and forth between two or more values
       quickly (in which case the data transmitted over the monitoring
       connections always increases quickly, without bound).

     - A manager repeatedly extends the contents of some column in some row
       (in which case the data transmitted over the monitoring connection
       grows with O(n**2) in the length of the string).

A better way to deal with this problem is to combine updates when they are
sent to the monitoring connection, if that connection is not keeping up.
In both the above cases, this reduces the data that must be sent to a
manageable amount.  An upcoming patch implements this new way.  This commit
reverts part of the previous solution that disconnects backlogged
connections, since it is no longer useful.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Andy Zhou <azhou@nicira.com>
2014-04-03 07:53:54 -07:00
Harold Lim
428b2eddc9 Rename NOT_REACHED to OVS_NOT_REACHED
This allows other libraries to use util.h that has already
defined NOT_REACHED.

Signed-off-by: Harold Lim <haroldl@vmware.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-12-17 13:16:39 -08:00
Ben Pfaff
10a89ef04d Replace all uses of strerror() by ovs_strerror(), for thread safety.
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-06-28 16:09:38 -07:00
YAMAMOTO Takashi
e091ef84ca some ctype related casts to suppress gcc warnings on NetBSD
where it can't be EOF, cast a value to unsigned char before passing it
to ctype functions to avoid unintended sign extension.

Signed-off-by: YAMAMOTO Takashi <yamt@mwd.biglobe.ne.jp>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-04-22 08:52:24 -07:00
Ben Pfaff
1600fa6853 json: New function json_serialized_length().
This will be used for the first time in an upcoming commit.

Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-04-01 13:16:59 -07:00
Ben Pfaff
cb22974d77 Replace most uses of assert by ovs_assert.
This is a straight search-and-replace, except that I also removed #include
<assert.h> from each file where there were no assert calls left.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Ethan Jackson <ethan@nicira.com>
2013-01-16 16:03:37 -08:00
Ben Pfaff
ebc56baa41 util: New macro CONST_CAST.
Casts are sometimes necessary.  One common reason that they are necessary
is for discarding a "const" qualifier.  However, this can impede
maintenance: if the type of the expression being cast changes, then the
presence of the cast can hide a necessary change in the code that does the
cast.  Using CONST_CAST, instead of a bare cast, makes these changes
visible.

Inspired by my own work elsewhere:
http://git.savannah.gnu.org/cgit/pspp.git/tree/src/libpspp/cast.h#n80

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-08-03 13:33:13 -07:00
Raju Subramanian
e0edde6fee Global replace of Nicira Networks.
Replaced all instances of Nicira Networks(, Inc) to Nicira, Inc.

Feature #10593
Signed-off-by: Raju Subramanian <rsubramanian@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-05-02 17:08:02 -07:00
Ben Pfaff
c640c04f64 json: Correct position tracking in JSON parser implementations.
When json_lex_input() returns false, the parser does not consume the byte
passed in.  That byte will get processed again in the next iteration of
the json_parser_feed() loop.  Therefore, until now, this code has
double-counted bytes that cause a false return from json_lex_input().

This fixes the problem.  Every input byte is now counted only once.

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-04-26 09:48:28 -07:00
Ben Pfaff
805e8f4e27 Remove unnecessary #include directives. 2011-05-16 13:40:47 -07:00
Ben Pfaff
bf9712678f util: Add function hexits_value() for parsing multiple hex digits.
Suggested-by: Justin Pettit <jpettit@nicira.com>
2010-11-15 10:18:10 -08:00
Ben Pfaff
2a022368f4 Avoid shadowing local variable names.
All of these changes avoid using the same name for two local variables
within a same function.  None of them are actual bugs as far as I can tell,
but any of them could be confusing to the casual reader.

The one in lib/ovsdb-idl.c is particularly brilliant: inner and outer
loops both using (different) variables named 'i'.

Found with GCC -Wshadow.
2010-09-20 09:39:54 -07:00
Ben Pfaff
20063bd1c8 json: Remove unused return value from json_parser_push().
No point in returning a value that no caller uses.
2010-08-25 14:55:47 -07:00
Ben Pfaff
051a77d677 json: Remove write-only variable from json_lex_number(). 2010-08-25 14:55:47 -07:00
Ben Pfaff
597cf5a1c1 json: Better handle JSON objects with duplicate names.
RFC 4627 (which defines JSON) says:

    The names within an object SHOULD be unique.

In my view, this means that the treatment of duplicate names within a
JSON object is more or less up to the implementation.  Until now, the OVS
JSON parser has dealt with duplicates fairly badly: they all get shoved
into the hash table and you get one or the other value semi-randomly
(typically the one added later).  This commit makes the behavior
predictable: old values are deleted and replaced by newer values.
2010-06-30 16:49:01 -07:00
Ben Pfaff
4bda8288fb json: Fix typo in error message. 2010-03-03 09:55:01 -08:00
Ben Pfaff
d951f1c78c json: Fix memory leak when nesting depth is exceeded.
This is probably not an important memory leak, since it is only on a rare
error path, but it is best to fix it anyway.

Found with valgrind.
2010-02-02 15:21:09 -08:00
Ben Pfaff
36d802ae1f json: New function json_to_ds().
Some upcoming code wants to serialize JSON into a "struct ds" dynamic
string buffer, so expose an interface to do this.

This commit doesn't change much, but it renames some functions internal
to json.c to make the naming more consistent.

Also, make jsonrpc_log_msg() use this new function, since it is a more
straightforward way to do what it wants.
2010-01-26 09:49:30 -08:00
Ben Pfaff
7d23a63aa4 json: Export function to parse an individual JSON string.
The JSON syntax for strings is very reasonable.  An upcoming commit will
have a need for a string parser, so make the JSON string parser available
for that.

Also, this change improves the error message for strings that end in the
middle of a \u sequence, so update the tests to match.
2010-01-26 09:47:54 -08:00
Ben Pfaff
5562d6f55e test-json: Avoid use of /dev/stdin to fix builds in limited chroots.
The chroots in which we often build Open vSwitch don't have /proc and
thus cannot support /dev/stdin, because on Linux that is a symlink to
/proc/self/fd/0.  So avoid using /dev/stdin in the testsuite.
2009-12-17 15:50:01 -08:00