SOME/IP-SD -- 协议英文原文讲解3

发布于:2025-02-27 ⋅ 阅读:(16) ⋅ 点赞:(0)

前言
SOME/IP协议越来越多的用于汽车电子行业中,关于协议详细完全的中文资料却没有,所以我将结合工作经验并对照英文原版协议做一系列的文章。基本分三大块:

1. SOME/IP协议讲解

2. SOME/IP-SD协议讲解

3. python/C++举例调试讲解
 


5.1.2.4 Options Format
Options are used to transport additional information to the entries. This includes for
instance the information how a service instance is reachable (IP-Address, Transport
Protocol, Port Number).
配置是 entries的 附加信息 ,说明了连接的方式:IP、传输协议、port
[PRS_SOMEIPSD_00273]
Upstream requirements: RS_SOMEIPSD_00006
In order to identify the option type every option shall start with:
• Length [uint16]: Specifies the length of the option in Bytes.
• Type [uint8]: Specifying the type of the option.
• Discardable Flag [1 bit]: Specifies, if this option can be discarded.
设置为1 :接收端不支持此option
---》体现了设计的灵活性---》比如 client的两个订阅包 向不同的IP订阅不同的 serviceID 有很多options是相同的 有个别的不同。则这个包组好后可以共用发送,只需要把差异点标记为discard 并同时修改options_index就行。
• Bit 1 to bit 7 are reserved and shall be 0.

[PRS_SOMEIPSD_00275]
Upstream requirements: RS_SOMEIPSD_00006
The discardable flag shall be set to 1 if the option can be discarded by a receiving
ECU that does not support this option.
丢弃标志位设置为1 表示 对方可丢弃 可不丢弃,具体是如果不支持就丢弃,否则解析使用。


下图是我截图实车报文中的一条offer报文展示 -- 为了保密 做了打码

 

5.1.2.4.1 Configuration Option
The configuration option is used to transport arbitrary configuration strings. This allows
to encode additional information like the name of a service or its configuration. In additional, the configuration option allows to offer non-SOME/IP services with SOME/IP-SD.
offfer的报文中 option 可以传输 string 信息 ,这个信息是是SOME/IP-SD格式 

[PRS_SOMEIPSD_00276]
Upstream requirements: RS_SOMEIPSD_00006
The format of the Configuration Option shall be as follows:
传输 string , type要设置为1 ,
按照下面格式 序列化

• Length [uint16]: Shall be set to the total number of bytes occupied by the configuration option, excluding the 16 bit length field and the 8 bit type flag.
• Type [uint8]: Shall be set to 0x01.
• Discardable Flag [1 bit]: Shall be set to 1 if the Option can be discarded by the
receiver.
• Bit 1 to bit 7 are reserved and shall be 0.
• ConfigurationString [dynamic length]: Shall carry the configuration string.

[PRS_SOMEIPSD_00277]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00009
The Configuration Option shall specify a set of name-value-pairs based on the DNS
TXT and DNS-SD format. [2] [3]
建议:可以像这样 设置一个键值对
DNS-Based Service Discovery
https://www.rfc-editor.org/rfc/rfc6763


[PRS_SOMEIPSD_00278]
Upstream requirements: RS_SOMEIPSD_00006
The format of the configuration string shall start with a single byte length field that describes the number of bytes following this length field. After the length field a character
sequence with the specified length shall follow.
string的长度段 只占一个字节  且描述的长度不包含自己本身,
=----- 我觉得有点多余 option中的长度指定可以推断出这个长度

[PRS_SOMEIPSD_00279]
Upstream requirements: RS_SOMEIPSD_00006
After each character sequence another length field and a following character sequence are expected until a length field shall be set to 0x00.
它描述了一种数据格式,其中包含一个字符序列(character sequence),后面跟着一个长度字段(length field),然后又是另一个字符序列,直到长度字段被设置为 0x00 为止。
简单举例:(不是真正的例子,真正使用格式是key=value)
05 48 65 6C 6C 6F 04 57 6F 72 6C 64 00
解析过程:
第一个长度字段:05,表示接下来的字符序列长度为 5。
字符序列:48 65 6C 6C 6F → 解码为 "Hello"。

第二个长度字段:04,表示接下来的字符序列长度为 4。
字符序列:57 6F 72 6C 64 → 解码为 "World"。

第三个长度字段:00,表示结束。
最终解析结果为:"Hello" 和 "World"。

[PRS_SOMEIPSD_00280]
Upstream requirements: RS_SOMEIPSD_00006
After a length field is set to 0x00 no characters shall follow.
上条有讲

[PRS_SOMEIPSD_00281]
Upstream requirements: RS_SOMEIPSD_00006
A character sequence shall encode a key and optionally a value.
[PRS_SOMEIPSD_00282]
Upstream requirements: RS_SOMEIPSD_00006
The character sequences shall contain an equal character ("=", 0x3D) to divide key
and value.
字符序列应该是 key=value的格式
字符串必须包含一个 = 号分割 键值
key=value
当然 是两者都存在的情况

[PRS_SOMEIPSD_00283]
Upstream requirements: RS_SOMEIPSD_00006
The key shall not include an equal character and shall be at least one non-whitespace
character. The characters of "Key" shall be printable US-ASCII values (0x20-0x7E),
excluding ’=’ (0x3D).
Key 是编码的核心部分,不能包含=,必须是可见字符,至少包含一个非空白字符

[PRS_SOMEIPSD_00284]
Upstream requirements: RS_SOMEIPSD_00006
The "=" shall not be the first character of the sequence.
key不能为空 即=不能在最前面 

[PRS_SOMEIPSD_00285]
Upstream requirements: RS_SOMEIPSD_00006
For a character sequence without an ’=’ that key shall be interpreted as present.
如果字串中 没有 = 号 则认为整个都是key

[PRS_SOMEIPSD_00286]
Upstream requirements: RS_SOMEIPSD_00006
For a character sequence ending on an ’=’ that key shall be interpreted as present
with empty value.
如果字串结尾是 = 号,表示只有key ,没有value

[PRS_SOMEIPSD_00287]
Upstream requirements: RS_SOMEIPSD_00006
Multiple entries with the same key in a single Configuration Option shall be supported.
多条 entry 公用一个 key ’Configuration Option‘ 是允许的。

5.1.2.4.2 Load Balancing Option
The Load Balancing option is used to prioritize different instances of the same service, so that a client can choose the service instance dynamically. This option will be
referenced by Offer Service entries.
负载平衡选项:
offer报文中 同一个serviceID 有多个 instance,
offer报文中可能会携带 负载平衡选项(优先级选项),值越小 优先级越大。还有个权重选项 值越大权重越大
具体报文格式 见下面
client端 可以根据优先级来订阅需要的实例
---- 一般不会有这种情况 客户会在矩阵表中 指定好订阅关系

[PRS_SOMEIPSD_00542]
Upstream requirements: RS_SOMEIPSD_00011
The Load Balancing Option shall carry a Priority and Weight like the DNS-SRV
records, which shall be used for load balancing different service instances. [4]
[PRS_SOMEIPSD_00711]
Upstream requirements: RS_SOMEIPSD_00011
When looking for all service instances of a service (Service Instance set to 0xFFFF),
the client shall choose the service instance with highest priority that also matches client
specific criteria.
Note: Client specific criteria may be applied by the client application when choosing
one of the offered service instances. They are not defined in this specification, and
could e.g. restrict the range of appropriate instance IDs.
这个报文 是针对 find报文 设置为0xffff
表示查找这个服务的所有Instance,
如果有多个 怎么选择 这个选择的策略 协议中没有定义,需要客户在应用层字节设计

当然可以选择订阅一个 多个 或全部

[PRS_SOMEIPSD_00712]
Upstream requirements: RS_SOMEIPSD_00011
When having more than one service instance with highest priority (lowest value in
Priority field) the service instance shall be chosen randomly based on the weights
of the service instances. The probability of choosing a service instance shall be the
weight of a service instance divided by the sum of the weights of all considered service
instances.
[PRS_SOMEIPSD_00713]
Upstream requirements: RS_SOMEIPSD_00011
If an Offer Service entry references no Load Balancing option and several service
instances are offered, the client shall handle the service instances without Load Balancing option as though they had the lowest priority.
如果offer中没有携带 instance 优先级选项,则client端 就不能依赖优先级来选择订阅了,要依赖自己的策略了。
---- 一般不会有这种情况 客户会在矩阵表中 指定好订阅关系

[PRS_SOMEIPSD_00714]
Upstream requirements: RS_SOMEIPSD_00011
When looking for a specific service instances of a service (Service Instance set to
any value other than 0xFFFF), the evaluation of the Load Balancing Option does not
apply.    
如果想要find 确定的实例 就要把instanceID 设置为指定的值,此时 “负载平衡”选项就不起作用了

具体options格式如下
[PRS_SOMEIPSD_00544]
Upstream requirements: RS_SOMEIPSD_00011
The Format of the Load Balancing Option shall be as follows:
• Length [uint16]: Shall be set to 0x0005.
• Type [uint8]: Shall be set to 0x02.
• Discardable Flag [1 bit]: Shall be set to 1 if the Option can be discarded by the
receiver.
• Bit 1 to bit 7 are reserved and shall be 0.
• Priority [uint16]: Carries the Priority of this instance. Lower value means higher
priority.
• Weight [uint16]: Carries the Weight of this instance. Large value means higher
probability to be chosen.

 

5.1.2.4.3 IPv4 Endpoint Option
The IPv4 Endpoint Option is used by a SOME/IP-SD instance to signal the relevant
endpoint(s). Endpoints include the local IP address, the transport layer protocol (e.g.
UDP or TCP), and the port number of the sender. These ports are used for the events
and notification events as well.
ipv4端口选项---》端口 协议 和IP
用于 基于这些 发送event 和 method

[PRS_SOMEIPSD_00305]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
The IPv4 Endpoint Option shall use the Type 0x04.
Type 设置为 0x04

[PRS_SOMEIPSD_00306]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
The IPv4 Endpoint Option shall specify the IPv4-Address, the transport layer protocol
(ISO/OSI layer 4) used, and its Port Number.
IPv4 Endpoint Option 用于指定 IPv4地址 

[PRS_SOMEIPSD_00307] ipv4 配置 如下 简单容易理解
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
The Format of the IPv4 Endpoint Option shall be as follows:
• Length [uint16]: Shall be set to 0x0009.
• Type [uint8]: Shall be set to 0x04.
• Discardable Flag [1 bit]: Shall be set to 0.
• Bit 1 to bit 7 are reserved and shall be 0.
• IPv4-Address [uint32]: Shall transport the unicast IP-Address as four Bytes.
• Reserved [uint8]: Shall be set to 0x00.
• Transport Protocol (L4-Proto) [uint8]: Shall be set to the transport layer protocol
(ISO/OSI layer 4) based on the IANA/IETF types (0x06: TCP, 0x11: UDP).
• Transport Protocol Port Number (L4-Port) [uint16]: Shall be set to the port of the
layer 4 protocol.


[PRS_SOMEIPSD_00310]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
The server shall use the IPv4 Endpoint Option with Offer Service entries to signal the
endpoints it serves the service instance on. That is up to one UDP endpoint and up to
one TCP endpoint.
服务器在提供服务实例时,使用 IPv4 Endpoint Option 来通知客户端其服务实例的端点信息时,每个服务实例最多可以提供一个 UDP 端点和一个 TCP 端点。

[PRS_SOMEIPSD_00380]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
The endpoints the server referenced with an Offer Service entry shall also be used as
source of events. That is source IP address and source port numbers for the transport
protocols in the endpoint option.


[PRS_SOMEIPSD_00807]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
The client shall use the IPv4 Endpoint Option with Subscribe Eventgroup entries to
signal the IP address and the UDP and/or TCP port numbers, on which it is ready to
receive the events.
Note: The client is ready to receive the events, if sockets are already opened, and any
security associations required by the network security protocols (IPsec, MACsec, or
other security protocols) are already established and fully operational.
client 根据配置把sokcet建立好,配置好mac通过 路由规则 防火墙规则,就可以正常接收事件了

[PRS_SOMEIPSD_00835]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
Different provided service instances of the same service on the same ECU shall use
different endpoints, so that they can be differentiated by the endpoints. Different services may share the same endpoints.
端点:IP+PORT
不同的serviceID可以使用相同的 端点
相同serviceID不同实例 不用使用相同端点

5.1.2.4.4 IPv6 Endpoint Option
功能和ipv4 一样
用在offer/sub 报文中
The IPv6 Endpoint Option is used by a SOME/IP-SD instance to signal the relevant
endpoint(s). Endpoints include the local IP address, the transport layer protocol (e.g.
UDP or TCP), and the port number of the sender. These ports are used for the events
and notification events as well.
[PRS_SOMEIPSD_00314]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
The IPv6 Endpoint Option shall use the Type 0x06.
[PRS_SOMEIPSD_00315]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
The Format of the IPv6 Endpoint Option shall be as follows:
格式如下 容易理解
• Length [uint16]: Shall be set to 0x0015.
• Type [uint8]: Shall be set to 0x06.
• Discardable Flag [1 bit]: Shall be set to 0.
• Bit 1 to bit 7 are reserved and shall be 0.
• IPv6-Address [uint128]: Shall transport the unicast IP-Address as 16 Bytes.
• Reserved [uint8]: Shall be set to 0x00.
• Transport Protocol (L4-Proto) [uint8]: Shall be set to the transport layer protocol
(ISO/OSI layer 4) based on the IANA/IETF types (0x06: TCP, 0x11: UDP).
• Transport Protocol Port Number (L4-Port) [uint16]: Shall be set to the transport
layer port(e.g. 30490).

SOME/IP-SD IPv6 Endpoint Option shall be as shown in Figure 5.10

[PRS_SOMEIPSD_00319]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
The server shall use the IPv6 Endpoint Option with Offer Service entries to signal the
endpoints the services is available on. That is upto one UDP endpoint and upto one
TCP endpoint.
服务器在提供服务实例时,使用 IPv6 Endpoint Option 来通知客户端其服务实例的端点信息时,每个服务实例最多可以提供一个 UDP 端点和一个 TCP 端点。

[PRS_SOMEIPSD_00320]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
The endpoints the server referenced with an Offer Service entry shall also be used as
source of events. That is source IP address and source port numbers for the transport
protocols in the endpoint option.
[PRS_SOMEIPSD_00321]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
The client shall use the IPv6 Endpoint Option with Subscribe Eventgroup entries to
signal the IP address and the UDP and/or TCP port numbers, on which it is ready to
receive the events.
options中指定 源IP和Port 表示event是从哪里发出的
Note:
• The client is ready to receive the events, if sockets are already opened, and any
security associations required by the network security protocols (IPsec, MACsec
or other security protocols) are already established and fully operational
• Security association status monitoring and its implications towards Service discovery shall apply for all Service Instances using secured ports.
在这段描述中,提到的安全条件是确保客户端能够在安全的网络环境下接收事件,并且这些安全措施需要在客户端和网络之间的通信中生效。我们可以进一步细分这些安全条件:

### 1. **安全关联(Security Association,SA)**
   - **定义**:安全关联是指两端通信(如客户端和服务器)之间的安全配置,它包括加密、身份验证等信息,确保通信的机密性、完整性和可用性。 
   - **协议支持**:SA通常涉及到网络安全协议,例如 **IPsec**(用于IP层的加密和认证)、**MACsec**(用于数据链路层的加密和完整性校验)等。
   - **作用**:这些协议可以保护通信中的数据,防止被篡改、监听或伪造。

### 2. **网络安全协议**:
   - **IPsec**:IPsec是用于IPv6(及IPv4)网络中,通过加密和认证保护IP通信的协议。它通过两种模式工作:
     - **传输模式(Transport Mode)**:仅加密传输层数据,不加密IP头部。
     - **隧道模式(Tunnel Mode)**:加密整个IP数据包,包括头部和数据部分,通常用于VPN。
   - **MACsec**:MACsec用于局域网中的数据链路层加密,提供防止数据篡改、重放攻击的能力。它通过在以太网帧上加密来保护数据。

### 3. **客户端准备接收事件的条件**:
   - 客户端首先需要打开相关的**socket**。这些socket是用来接收事件数据的通信端口。
   - 然后,客户端还必须确保所需要的安全协议(如IPsec、MACsec等)的**安全关联已经建立并且完全工作**。如果这些安全措施没有正确配置或未建立,客户端就无法保证通信的安全性,进而无法确保事件数据的正确接收。

### 4. **安全关联的监控与服务发现**:
   - **安全关联状态监控**:客户端和网络系统需要实时监控安全关联的状态,确保所有的安全配置和加密通道在需要时都能正常工作。如果安全关联出现问题,可能会影响事件接收的安全性。
   - **服务发现**:在基于安全端口的服务实例中,服务发现过程需要考虑安全关联的状态。如果安全关联不可用或不完全,客户端可能无法发现或与服务实例建立安全的连接。

### 5. **应用场景**:
   - 在一些高安全要求的网络环境中,例如金融、电力、政府等领域,数据通信必须经过加密保护,这时就会使用到这些安全协议。通过确保安全关联已建立,客户端可以避免潜在的安全威胁,如数据泄露或身份冒充。

### 总结:
- 客户端准备好接收事件并非仅仅依赖于端口和IP的配置,还需要网络安全协议的支持,以确保在安全的环境下传输数据。
- 安全协议的实施包括加密、身份验证等,而安全关联的监控则确保这些协议在实际通信过程中始终有效。


[PRS_SOMEIPSD_00836]
Upstream requirements: RS_SOMEIPSD_00006, RS_SOMEIPSD_00010
Different service instances of the same service on the same ECU shall use different
endpoints, so that they can be distinguished by the endpoints. Different services may
share the same endpoints.
端点:IP+PORT
不同的serviceID可以使用相同的 端点
相同serviceID不同实例 不用使用相同端点