Skip to content

Commit 7fbdcf1

Browse files
save it just in case, as VM under risk
1 parent c223d04 commit 7fbdcf1

7 files changed

Lines changed: 729 additions & 17 deletions

File tree

common/COMPILE_README

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
1) Using ~/source/sonic-buildimage/c_build.sh
2+
2) It uses _upd_cmn.sh to update files
3+
3) On failure, go to ~/source/sonic-buildimage/target/debs/bullseye
4+
4) open libswsscommon_1.0.0_amd64.deb.log
5+
6+
Get failing command. Run from inside src/sonic-swss-common/common. Interestingly c compilation alone works fine outside slave too.
7+
NOTE: From within slave you can't copy files from another repo as root of slave is at ~/src/sonic-buildimage
8+
9+
/bin/bash ../libtool --tag=CXX --mode=compile g++ -DHAVE_CONFIG_H -I. -I.. -I .. -g -DNDEBUG -ansi -fPIC -std=c++11 -Wall -Wcast-align -Wcast-qual -Wconversion -Wdisabled-optimization -Werror -Wextra -Wfloat-equal -Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimport -Winit-self -Winvalid-pch -Wlong-long -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wno-aggregate-return -Wno-padded -Wno-switch-enum -Wno-unused-parameter -Wpacked -Wpointer-arith -Wredundant-decls -Wshadow -Wstack-protector -Wstrict-aliasing=3 -Wswitch -Wswitch-default -Wunreachable-code -Wunused -Wvariadic-macros -Wno-write-strings -Wno-missing-format-attribute -Wno-long-long -Wdate-time -D_FORTIFY_SOURCE=2 -g -DNDEBUG -ansi -fPIC -std=c++11 -Wall -Wcast-align -Wcast-qual -Wconversion -Wdisabled-optimization -Werror -Wextra -Wfloat-equal -Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimport -Winit-self -Winvalid-pch -Wlong-long -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wno-aggregate-return -Wno-padded -Wno-switch-enum -Wno-unused-parameter -Wpacked -Wpointer-arith -Wredundant-decls -Wshadow -Wstack-protector -Wstrict-aliasing=3 -Wswitch -Wswitch-default -Wunreachable-code -Wunused -Wvariadic-macros -Wno-write-strings -Wno-missing-format-attribute -Wno-long-long -I/usr/include/libnl3 -g -O2 -ffile-prefix-map=/sonic/src/sonic-swss-common=. -fstack-protector-strong -Wformat -Werror=format-security -c -o libswsscommon_la-events_common.lo `test -f 'events_common.cpp' || echo './'`events_common.cpp:wq
10+
11+
12+
13+
14+
15+
16+
localadmin@c7cbc0ba3b64:/sonic$ cat c_build.sh
17+
#! /bin/bash
18+
19+
set -x
20+
21+
./_upd_cmn.sh
22+
23+
rm -f target/debs/bullseye/libswsscommon*
24+
rm -f target/sonic-broadcom.bin target/sonic-aboot-broadcom.swi
25+
# KEEP_SLAVE_ON=yes SONIC_BUILD_JOBS=7 PASSWORD=password make target/debs/bullseye/libswsscommon_1.0.0_amd64.deb
26+
KEEP_SLAVE_ON=yes make target/debs/bullseye/libswsscommon_1.0.0_amd64.deb
27+
# make target/debs/bullseye/libswsscommon_1.0.0_amd64.deb
28+
#SONIC_BUILD_JOBS=7 PASSWORD=password make target/sonic-broadcom.bin
29+
# SONIC_BUILD_JOBS=7 PASSWORD=password make target/sonic-aboot-broadcom.swi
30+
31+
32+
localadmin@c7cbc0ba3b64:/sonic$ cat _upd_cmn.sh
33+
#! /bin/bash
34+
35+
SRCD="/home/localadmin/source/fork/syslog_telemetry/remanava/sonic-swss-common/common"
36+
DSTD="./src/sonic-swss-common/common"
37+
38+
for i in Makefile.am events.h events_common.cpp events_common.h
39+
do
40+
echo "copying $i"
41+
cp ${SRCD}/$i ${DSTD}/$i
42+
done
43+
localadmin@c7cbc0ba3b64:/sonic$

common/events.cpp

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#include "events_pi.h"
2+
3+
lst_publishers_t EventPublisher::s_publishers = lst_publishers_t();
4+
5+
EventPublisher::EventPublisher(const string source) :
6+
m_zmq_ctx(NULL), m_socket(NULL), m_sequence(0), m_init(false)
7+
{
8+
uuuid_clear(m_runtime_id);
9+
10+
}
11+
12+
int EventPublisher::init(const string event_source, int block_ms)
13+
{
14+
m_zmq_ctx = zmq_ctx_new();
15+
m_socket = zmq_socket (m_zmq_ctx, ZMQ_PUB);
16+
17+
int rc = zmq_connect (m_socket, get_config(XSUB_END_KEY));
18+
RET_ON_ERR(rc == 0, "Publisher fails to connect %s", get_config(XSUB_END_KEY));
19+
20+
// REQ socket is connected and a message is sent & received, more to
21+
// ensure PUB socket had enough time to establish connection.
22+
// Any message published before connection establishment is dropped.
23+
//
24+
event_service m_event_svc;
25+
rc = m_event_svc.init_client(block_ms);
26+
RET_ON_ERR (rc == 0, "Failed to init event service");
27+
28+
rc = m_event_svc.echo_send("hello");
29+
RET_ON_ERR (rc == 0, "Failed to echo send in event service");
30+
31+
zmq_msg_init_size(&m_zmsg_source, event_source.size());
32+
memcpy((char *)zmq_msg_data(&m_zmsg_source), event_source, event_source.size());
33+
34+
m_event_source(event_source);
35+
36+
uuid_t id;
37+
uuid_generate(id);
38+
uuid_unparse(id, m_runtime_id);
39+
40+
m_init = true;
41+
out:
42+
return m_init ? 0 : -1;
43+
}
44+
45+
EventPublisher::~EventPublisher()
46+
{
47+
zmq_msg_close(&m_zmsg_source);
48+
zmq_close(m_socket);
49+
zmq_close(m_echo_socket);
50+
zmq_ctx_term(m_zmq_ctx);
51+
}
52+
53+
54+
void
55+
EventPublisher::event_publish(const string tag, const event_params_t *params)
56+
{
57+
zmq_msg_t msg;
58+
59+
if (m_event_service.is_active()) {
60+
string s;
61+
62+
/* Failure is no-op; The eventd service my be down
63+
* NOTE: This call atmost blocks for block_ms milliseconds
64+
* as provided in publisher init.
65+
*/
66+
m_event_service.echo_receive(s);
67+
}
68+
69+
string param_str;
70+
if ((params != NULL) && (param->find(event_ts) != params->end())) {
71+
72+
param_str = serialize(*params);
73+
}
74+
else {
75+
event_params_t evt_params = *params;
76+
evt_params[event_ts] = get_timestamp();
77+
param_str = serialize(evt_params);
78+
}
79+
80+
map_str_str_t event_str = { { m_event_source + ":" + tag, param_str}};
81+
82+
++m_sequence;
83+
internal_event_t event_data;
84+
event_data[EVENT_STR_DATA] = serialize(event_str);
85+
{
86+
stringstream ss;
87+
88+
ss << m_sequence;
89+
90+
event_data[EVENT_SEQUENCE] = ss.str();
91+
}
92+
event_data[EVENT_RUNTIME_ID] = m_runtime_id;
93+
94+
map_to_zmsg(event_data, msg);
95+
96+
// Send the message
97+
// First part -- The event-source/pattern
98+
// Second part -- Metadata
99+
// Third part -- Params, if given
100+
//
101+
RET_ON_ERR(zmq_msg_send (&m_zmsg_source, m_socket, ZMQ_SNDMORE) != -1,
102+
"Failed to publish part 1 to %s", XSUB_PATH);
103+
RET_ON_ERR(zmq_msg_send (&msg, m_socket, 0) != -1,
104+
"Failed to publish part 2 to %s", XSUB_PATH);
105+
out:
106+
zmq_msg_close(&msg_metadata);
107+
108+
}
109+
110+
event_handle_t
111+
events_init_publisher(const string event_source, int block_millisecs)
112+
{
113+
event_handle_t ret = NULL;
114+
lst_publishers_t::iterator it = s_publishers.find(event_source);
115+
if (it != s_publishers.end()) {
116+
// Pre-exists
117+
ret = it->second;
118+
}
119+
else {
120+
EventPublisher *p = new EventPublisher();
121+
122+
int rc = p->init(event_source, block_millisecs);
123+
124+
if (rc != 0) {
125+
delete p;
126+
}
127+
else {
128+
ret = p;
129+
s_publishers[key] = ret;
130+
}
131+
}
132+
return ret;
133+
}
134+
135+
void
136+
events_deinit_publisher(event_handle_t &handle)
137+
{
138+
lst_publishers_t::iterator it;
139+
EventPublisher *pub = dynamic_cast<EventPublisher *>(&handle);
140+
141+
for(it=s_publishers.begin(); it != s_publishers.end(); ++it) {
142+
if (it->second == handle) {
143+
break;
144+
}
145+
}
146+
147+
if (it != s_publishers.end() {
148+
s_publishers.erase(it);
149+
150+
delete pub;
151+
}
152+
handle = NULL;
153+
154+
}
155+
156+
void
157+
event_publish(event_handle_t handle, const string tag, const event_params_t *params)
158+
{
159+
EventPublisher *pub = dynamic_cast<EventPublisher *>(handle);
160+
161+
for(it=s_publishers.begin(); it != s_publishers.end(); ++it) {
162+
if (it->second == handle) {
163+
pub->event_publish(tag, params);
164+
break;
165+
}
166+
}
167+
}
168+
169+
170+
171+
172+
173+

common/events.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,21 @@ typedef events_base* event_handle_t;
2828
* The YANG module name for the event source. All events published with the handle
2929
* returned by this call is tagged with this source, transparently. The receiver
3030
* could subscribe with this source as filter.
31+
*
32+
* block_millisecs -
33+
* Block either until publisher connects successfully or timeout
34+
* whichever earlier.
35+
* 0 - No blocling.
36+
* -1 - Block until connected.
37+
* N - Count in milli seconds to wait.
38+
* NOTE: The connection is to eventd service, which could be down.
3139
* Return
3240
* Non NULL handle
3341
* NULL on failure
3442
*/
3543

36-
event_handle_t events_init_publisher(std::string &event_source);
44+
event_handle_t events_init_publisher(std::string event_source,
45+
int block_millisecs);
3746

3847
/*
3948
* De-init/free the publisher
@@ -52,6 +61,11 @@ void events_deinit_publisher(event_handle_t &handle);
5261
*/
5362
typedef std::map<std::string, std::string> event_params_t;
5463

64+
/*
65+
* timestamp param name
66+
*/
67+
const string event_ts("timestamp");
68+
5569
/*
5670
* Publish an event
5771
*
@@ -79,9 +93,11 @@ typedef std::map<std::string, std::string> event_params_t;
7993
* params -
8094
* Params associated with event; This may or may not contain
8195
* timestamp. In the absence, the timestamp is added, transparently.
96+
* The timestamp should be per rfc3339
97+
* e.g. "2022-08-17T02:39:21.286611Z"
8298
*
8399
*/
84-
void event_publish(event_handle_t handle, const std::string &event_tag,
100+
void event_publish(event_handle_t handle, const std::string event_tag,
85101
const event_params_t *params=NULL);
86102

87103

common/events_common.h

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
using namespace std;
1616
using namespace chrono;
1717

18+
#define RET_ON_ERR(res, ...) {\
19+
if (!(res)) \
20+
SWSS_LOG_ERROR(__VA_ARGS__); }
21+
goto out;
22+
1823
#define ERR_CHECK(res, ...) {\
1924
if (!(res)) \
2025
SWSS_LOG_ERROR(__VA_ARGS__); }
@@ -44,21 +49,6 @@ string get_config(const string key);
4449

4550
const string get_timestamp();
4651

47-
/*
48-
* events are published as two part zmq message.
49-
* First part only has the event source, so receivers could
50-
* filter by source.
51-
*
52-
* Second part contains event as defined below.
53-
*
54-
* The callers would only see, event_str_t
55-
*/
56-
typedef struct {
57-
event_str_t event;
58-
uint32_t runtime_id;
59-
uint32_t sequence;
60-
} internal_event_t;
61-
6252
const string serialize(const map_str_str_t & data);
6353

6454
void deserialize(const string& s, map_str_str_t & data);

0 commit comments

Comments
 (0)